So while doing a course on nand2tetris I got stuck in this question.
Basically this question is related to building a virtual machine. The virtual machine is quite similar to JVM.
There are 2 main parts to consider here:
call foo 2
: this instruction tells vm to call funcion foo
that takes 2 arguments ( which should be pushed on top of the stack before this call ). function foo 4
: this is NOT the first instruction in function foo
, but still it has an effect on SP as this means that functionfoo
has 4 local variables. And these variables must be located somewhere. Where? On top of the stack. This means that before first 'real' instruction of foo
gets executed, we must push 4 values onto the stack. What values? Well - according do vm specification it should be 0
's, resulting in local variables being initiated to 0. This also means that we increase SP for every local variable ( SP + 4)This leads to conclusion that SP, after calling foo
but before executing 1st instruction of this function will have value of 314.