calling method

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

calling method

q1tiaN
in the .vm file , I saw "pop temp 0" behind every "call" command, but it doesn't show in the examples from book,
Reply | Threaded
Open this post in threaded view
|

Re: calling method

ivant
The VM spec requires that each function should pop its arguments and push a single return value. If the caller isn't interested in this value, it should still pop it off the stack.

This mainly happens with void Jack functions or methods. From the Jack program's perspective, they don't return any value. So when they are translated to the VM, the Jack compiler pushes a 0 on the stack to comply with the VM spec.

[EDIT]
The code in the picture calls Math.multiply, which pops the two arguments from the stack and replaces them with their product. It then calls BankAccount.commission, which uses the 2 parameters on the stack (the second one is the product from the previous call) and replaces them with the commission. This is then used in the subsequent sub command.
[/EDIT]
Reply | Threaded
Open this post in threaded view
|

Re: calling method

q1tiaN
thank you very much . I got that , the "do command" doesn't need a return value , so just abandon it.
Reply | Threaded
Open this post in threaded view
|

Re: calling method

cadet1620
Administrator
I've sometimes thought that it would have been a nice touch to make the arguments to the push and pop VM commands optional.

pop with no segment/index would be pop and discard; just decrement SP -- perfect for the Jack do command.

push with no arguments could be "dup"; increment SP duplicating the top-of-stack value.  I don't see any particular use for this operation in Jack, however. (It would be handy if Jack had switch/case statements...)

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: calling method

ivant
cadet1620 wrote
push with no arguments could be "dup"; increment SP duplicating the top-of-stack value.  I don't see any particular use for this operation in Jack, however. (It would be handy if Jack had switch/case statements...)
Sounds interesting. How is it helping with that?
Reply | Threaded
Open this post in threaded view
|

Re: calling method

cadet1620
Administrator
ivant wrote
Sounds interesting. How is it [dup] helping with that [switch/case statement]?
Source:
switch (i+j) {
    case 1:
        do Output.printString("one");
        break;
    case TWO:
        do Output.printString("two");
        break;
    (more cases)
...
        break;
}
do Output.println();
       
Code generated by my compiler:
/// 11:             switch (i+j) {
push local 0 // i           <===== evaluate control expression
push local 3 // j
add
/// 12:                 case 1:
pop temp 0                  <===== dup top of stack
push temp 0                 <
push temp 0                 <
push constant 1
eq
not
if-goto L5
/// 13:                     do Output.printString("one");
push constant 3
call String.new 1
...
call Output.printString 1
pop temp 0
/// 14:                     break;
goto L4
/// 15:                 case TWO:
label L5
pop temp 0
push temp 0
push temp 0
push local 1 // TWO
eq
not
if-goto L6
/// 16:                     do Output.printString("two");
...
/// 35:                     break;
goto L12
label L14
goto L4
/// 36:             }
/// 37:
label L4
pop temp 0                  <===== discard control value
/// 38:             do Output.println();
call Output.println 0