Return from the callee function

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

Return from the callee function

hemasaad
when we encounter the Return in the callee function we need to restore the frame of the caller function and then return to the (return address) by "goto RET ". the point i don't understand is that RET is temporary location in memory has the return address. this return address related to the vm code for ex
-function main 2
-push constant 1
-call fn 1
-push contant 2

this code can be translated into assembly like
-(main)
-//code to push constant 0
-//code to push constant 0
-//code to push constant 1
-//I have return address initialized by 0 and increment with every vm command
-//when we reach the call command it will be 3
-//code to save the caller function frame  (return address = 3, LCL,THIS, THAT,.......)
-//code goto f
-(RA$3) // 3 refers to the return address over the vm code it will be unique I think that.

in the funtion fn we will reach at the end to encounter RETURN command and we restore all frame of the caller function then
-goto RET    
but the return address has the address of the next command in the vm code not in the asm code so i think we can write
-goto RA$returnaddress
i think it is logic but i can't implement it.I am so confused with this point any help will be great .
Reply | Threaded
Open this post in threaded view
|

Re: Return from the callee function

cadet1620
Administrator
The return address is an ASM label. The value that you push for "push return-address" in the call pseudocode is the ROM address of the label.
// call fn 1
    @RA$3
    D=A
    // code to do "push D"
    // remaining "call" code, ends with ...
    @fn
    0;JMP
(RA$3)
The "goto RET" in the return pseudocode is
// return
    // frame and return value code...
    @R15    // or R13 or R14, whatever you used for "RET"
    A=M
    0;JMP
The number in the return address labels does not need to be the VM command number, it just needs to be unique for each instance of a call command, the same way the labels used in "gt", "lt" and "eq" need to be unique for each instance.

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: Return from the callee function

hemasaad
thank you Mark for this simple explanation,
I wonder  if i initialize memory segments and also the test file initialize them.what is the finial value of memory segments.
Reply | Threaded
Open this post in threaded view
|

Re: Return from the callee function

cadet1620
Administrator
hemasaad wrote
if i initialize memory segments and also the test file initialize them.what is the finial value of memory segments.
The initialization done by the test file happens before your code starts running. Anything that your code initializes will overwrite the values set by the test file.

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

Re: Return from the callee function

Sai krishna
In reply to this post by hemasaad
I am just blown by this idea...May be I am too dumb for not getting this idea or the idea is so critical, brilliant to strike me. It is just brilliant. Thank you for this reply.
Reply | Threaded
Open this post in threaded view
|

Re: Return from the callee function

tuzmusic
In reply to this post by cadet1620
I just don't understand why you have to push that label onto the stack. Can't you just put the label after the call prep code (like in your example), and then jump to it in your return statement?
// call fn 1
  // remaining call code...
  @fn
  0;JMP
(RA$3)

// return statement:
  // frame and return value code...
  @RA$3
Reply | Threaded
Open this post in threaded view
|

Re: Return from the callee function

WBahn
Administrator
What if I call the same function from twelve different places in my program?