pop temp zero

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

pop temp zero

pawdwanlearner
ok so im compiling pong game and found that the ball.setDestination does not recieve the object gives a out of segment error. Im finding that pop temp 0 is not in the right place. so im having trouble with trying to find when to push a constant 0 and pop temp 0. According to the video in the part 2 of the course your suppose to do this when the type of function is void and returns no value. I tried to override this by simply pushing and popping a zero then pop the next value but that did not work out the above function is missing the object that is stored in this 1 any tips would be appreciated. Almost there so exciting !! ready for chapter 12
Reply | Threaded
Open this post in threaded view
|

Re: pop temp zero

cadet1620
Administrator
You only write the "push constant 0" in "return <no-expression>" commands. This 0 takes the place of the expression result.  Returns without expressions should only be in functions and methods that return void. (Constructors must have their class as their return type.)

You do not need to check the function return type unless you want to print a warning if the wrong kind of return is used.


You only write the "pop temp 0" command for do commands, and you write it after every function call, regardless of function return type, including functions that return void.

(You can not check the function return type because the call may be to a function in another class.)


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

Re: pop temp zero

pawdwanlearner
Hmm the only thing is that the do command is used to make a function call. So are you saying i should put pop temp 0 there and after the function call

so like this

do obj.Varname()

push constant 0
pop temp 0
pop temp 0

what about let brand = obj.Varname()
Reply | Threaded
Open this post in threaded view
|

Re: pop temp zero

cadet1620
Administrator
Function call in an expression never does pop temp 0. After the call returns the return value is on the top of the stack so that it can be stored or used in further computation.
// var Thing obj;
// var String brand;
// let brand = obj.Varname()
    push local 0    // 'this' for Varname
    ...
    @Thing.Varname
    0;JMP
($RET$123)          // return value is at top of stack
    pop local 1     // store return value in brand

Function call in a do statement always does pop temp 0 after the call returns to get rid of the return value that is being ignored. Without the pop temp 0, the return value would remain on the stack and the SP would be one greater than it should be.
// var Thing obj;
// do obj.Varname()
    push local 0    // 'this' for Varname
    ...
    @Thing.Varname
    0;JMP
($RET$456)          // return value is at top of stack
    pop temp 0      // throw away return value
Reply | Threaded
Open this post in threaded view
|

Re: pop temp zero

pawdwanlearner
Got it and Done !! Even the complex arrays works now . I have a done on the compiler  Thanks again sir
Reply | Threaded
Open this post in threaded view
|

Re: pop temp zero

Ichiro
In reply to this post by cadet1620
I found this thread when it turned out my VM of ConvertToBin failed because it lacked 'pop temp 0'. I understood that without 'pop temp 0' SP would be one greater than it should be.

I tried to figure out what would go wrong on the global stack when executing the ConvertToBin program without 'pop temp 0', but could not spot an issue caused by making SP one greater. Would you give me an example of an inconvenience that would happen by making SP one greater?

Best Regards,
Ichiro
Reply | Threaded
Open this post in threaded view
|

Re: pop temp zero

WBahn
Administrator
If you have something sitting on the top of the stack that should be removed and isn't, then all of your subsequent references to anything on the stack will be point to the wrong data. That's seldom a good thing.
Reply | Threaded
Open this post in threaded view
|

Re: pop temp zero

Ichiro
Thank you for your reply.
I understand that if SP remains one greater than it should be, all of my subsequent references to anything on the stack will be wrong. For example, when the call command is executed, the LCL address is created for the caller and that LCL address would point to the wrong place without 'pop temp 0'.
Reply | Threaded
Open this post in threaded view
|

Re: pop temp zero

WBahn
Administrator
The LCL would point to a different place, but it wouldn't really be wrong. The call frame for the new function call would simply be located one memory location further into memory than necessary. But it would be a valid frame.

But let's say that you have the following values at the top of the stack (starting with the topmost): 12, 13, 14. Now you call a function that does something but returns nothing, then you perform an add operation. The result should be that the top of the stack now looks like: 25, 14. But fail to pop the 0 off the stack to account for the void function call, you will start with 0, 12, 13, 14 and end up with 12, 13, 14 instead.

Or if, instead of adding you simply return from the current function, you would return 0 instead of returning 12.

 
Reply | Threaded
Open this post in threaded view
|

Re: pop temp zero

Ichiro
I see. The LCL would point to a different place, but a value pointed by the LCL will be a local variable. That is why "it wouldn't really be wrong."
The examples when things go wrong is straightforward and now the reason why 'pop temp 0' is necessary is very clear to me.

Thanks,
Ichiro