pop temp zero

classic Classic list List threaded Threaded
5 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