15.08.2013 Views

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

eturn interprets the top of the stack as the result,<br />

it jumps to the return address,<br />

deletes the current frame<br />

and puts the result back on top of the remaining stack.<br />

[proc 2 26,<br />

con 0, arg 2, leq, cjp 5,<br />

con 1, return,<br />

con 1, arg 2, sub, arg 1,<br />

call 0, arg 1, mul,<br />

return,<br />

con 2, con 10, call 0,<br />

halt]<br />

100<br />

32 0<br />

10 -1<br />

2 -2<br />

we push argument 1 (in this case 10), multiply the top two numbers, and push the result<br />

to the stack<br />

[proc 2 26,<br />

con 0, arg 2, leq, cjp 5,<br />

con 1, return,<br />

con 1, arg 2, sub, arg 1,<br />

call 0, arg 1, mul,<br />

return,<br />

con 2, con 10, call 0,<br />

halt]<br />

100<br />

return interprets the top of the stack as the result,<br />

it jumps to the return address (32 this time),<br />

deletes the current frame<br />

and puts the result back on top of the remaining stack (which is empty here).<br />

[proc 2 26,<br />

con 0, arg 2, leq, cjp 5,<br />

con 1, return,<br />

con 1, arg 2, sub, arg 1,<br />

call 0, arg 1, mul,<br />

return,<br />

con 2, con 10, call 0,<br />

halt]<br />

we are finally done; the result is on the top of the stack. Note that the stack below has<br />

not changed.<br />

What have we seen?<br />

100<br />

c○: Michael Kohlhase 310<br />

The four new VMP commands allow us to model recursive functions.<br />

proc a l contains information about the number a of arguments and the length l of the<br />

procedure<br />

arg i pushes the i th argument from the current frame to the stack.<br />

(Note that arguments are stored in reverse order on the stack)<br />

call p pushes the current program address (opens a new frame), and jumps to the program<br />

address p<br />

177

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!