Having issues with return address in project 8

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

Having issues with return address in project 8

goodsoop21
This post was updated on .
When I first run my code the return address (Let's say RET_4) is set to 54 when converted to hack. I realize that the value at return address 54 is 0 when it should be 4. How can I fix this? I tried manually placing the value into the return address but this messes up the stack pointer. By the time the code has finished, the top of the stack(RAM[256]) ends up with the return address 54, not resulting value that came from the called function.
Reply | Threaded
Open this post in threaded view
|

Re: Having issues with return address in project 8

WBahn
Administrator
There's not enough information to get a sense of what is going on.

When you say that RET_4 is set to 54, are you saying that your VM Translator produces a label just before the instruction that ends up in ROM address 54?

Why should the value at return address be 4 and not 0? What is the instruction that is supposed to be at that address?

Reply | Threaded
Open this post in threaded view
|

Re: Having issues with return address in project 8

goodsoop21
Let me break it down so I can explain the issue better. When I push the @RET_3 on to 256( to get back where "call main 0" left off), @53 is pushed onto the top of the stack. When the rest of my code finishes the CPU emulator is stuck going back and forth between @53 and 0;JMP. But 53(@RET_3) stays on the top of the stack when the return value should be on top of the stack instead. Isn't RAM[53] supposed to hold the return value? If that is the case I'm not sure why it's not putting the return value into RAM[53].

This is the whole vm code that should be excuted.

Sys.vm
function init 0
set sp 256
call main 0
end
//
main.vm
function main 2
push constant 2
pop local 0            
push constant 5
pop local 1            
push local 0
push local 1
call add 2            
return
//
function add 2
push argument 0
push argument 1
add
return

Reply | Threaded
Open this post in threaded view
|

Re: Having issues with return address in project 8

WBahn
Administrator
set sp 256

is not VM code. There is no 'set' command in the VM language.

Similarly, there is no 'end' command.

Your bootstrap code that the VM Translator generates needs to put in assembler code that makes this happen.

I don't know what RET_3 is, just like I didn't know what RET_4 was in your original post. Assuming that 53 is the INSTRUCTION address in ROM that contains the first instruction after main() finishes, then that gets pushed onto the stack as part of the function call process.

When your function returns, it needs to REPLACE the value at the beginning of the callee's stack frame (which is *ARG, or the first argument -- however if there were no arguments, then it will happen to be the return value). Since it might be the return value, we need to first place that value someplace safe so that we can come back to it to make the actual jump back to it happen.

We then restore the caller's segment pointers, adjust the stack pointer to reflect that there is one more thing on the stack (the return value), and do an unconditional jump back.

If the RA is 53, then that has NO relation to what is in RAM[53], because instruction are in ROM, not RAM.