Dimenus wrote
Apologies. I'm trying to understand why we specify the number of local elements when declaring the function header and then push them on to the stack when the vm code pushes/initializes them on the stack anyway right below the header.
Ex:
In SimpleFunction, I would expect to have two values immediately pushed onto the stack, but we push four.
function SimpleFunction.test 2
push local 0
push local 1
add
not
push argument 0
add
push argument 1
sub
The local 1 and local 2 pushes are part of an expression evaluation and will not normally be seen in functions. They are referencing local variables that have not yet been set by
let statements.
This code is testing that both local variables were initialized to 0 by the function entry code.
The equivalent Jack code is:
function int test(int a, int b)
{
var int i, j;
return ((~(i+j))+a)-b;
}
The expression could be rewritten as (a-b)+(~(i+j)) and it will return the same value. In that case the VM code would be:
function SimpleFunction.test 2
push argument 0
push argument 1
sub
push local 0
push local 1
add
not
add
return
--Mark