Showing posts with label virtual machine. Show all posts
Showing posts with label virtual machine. Show all posts

Sunday, March 26, 2017

Nand2Tetris 2 : Function Implementation Contract

https://www.coursera.org/learn/nand2tetris2/lecture/R5080/unit-2-3-functions-abstraction
For each function call during run-time, the implementation has to ...
• Pass parameters from the calling function to the called function;
• Determine the return address within the caller's code;
• Save the caller's return address, stack and memory segments;
• Jump to execute the called function;

For each function return during run-time. the implementation has to...
• Return to the caller the value computed by the called function;
• Recycle the memory resources used by the called function;
• Reinstate the caller's stack and memory segments;
• Jump to the return address in the caller's code.

courtesy of http://www.onlineocr.net/. Note that bullet one above for the function (callee) means you HAVE to push a value onto the stack before returning..

VM Implementation of Call and Return



Saturday, March 18, 2017

Shimon's More Efficient Push - Nice

In the video, where he does push constant 17, he shows

@17
D=A
@SP
A=M
M=D
@SP
M=M+1

Notice that needs 3 A instructions. But, if you pay attention, during the demo, with BasicTest.vm, for  push constant 10, his implementation is :

@10
D=A
@SP
AM=M+1
A=A-1
M=D

Saved an instruction! Nice. Ha - did you think Stuxnet was easy to build my man? Truly, since he scrolls down in his ASM file, he's given you the implementation of "pop argument 2" and so it should be okay to put that down here - but that would be mean :)

Not that they're hard to implement, but he's given you add and sub as well :) Both cute, and using 5 instructions each. Booleans are super easy. For the conditionals, you'll have to use the jumps to conditionally write the appropriate value to the stack.