Push/Pop LCL, ARG

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

Push/Pop LCL, ARG

ElliotW
I've been having some issues getting this to work, and I just want to make sure:  
When we pop or push a value to/from LCL/ARG/THIS/THAT, are we using the address or the literal value in these registers?
Reply | Threaded
Open this post in threaded view
|

Re: Push/Pop LCL, ARG

WBahn
Administrator
Just sketch it out on paper and see what needs to happen.

Say you have

push local 7

This means you want to take the value current stored at index 7 of the local memory segment and place it on top of the stack.

So let's say that the relevant part of our memory map is

M[0] = 293
M[1] = 271
M[2] = 261
M[3] = 2468
M[4] = 2306

M[260] = 11
M[261] = 13
M[262] = 14
M[263] = 15
M[264] = 16
M[265] = 17
M[266] = 18
M[267] = 19
M[268] = 520
M[269] = 521
M[270] = 511
M[271] = 513
M[272] = 514
M[273] = 515
M[274] = 516
M[275] = 517
M[276] = 518
M[277] = 519
M[278] = 520
M[279] = 521

M[290] = 131
M[291] = 132
M[292] = 133
M[293] = 134
M[294] = 135
M[295] = 136

What changes need to be made as a result of executing that push statement?
Reply | Threaded
Open this post in threaded view
|

Re: Push/Pop LCL, ARG

ElliotW
In reply to this post by ElliotW
And as an extension, when we save these registers in the frame, are we saving the addresses they're pointing to or the value the address is storing? I think this might be where my confusion lies.
Reply | Threaded
Open this post in threaded view
|

Re: Push/Pop LCL, ARG

WBahn
Administrator
Well, what makes sense?

If my current local segment starts at address 300 and the value stored in memory location 300 is 42, what information is it that I need to remember so that I can restore the location of my local segment after returning from a function call? Do I need the 300, or the 42?
Reply | Threaded
Open this post in threaded view
|

Re: Push/Pop LCL, ARG

ElliotW
In reply to this post by WBahn
So you would go to the address of (Memory(RAM[1]) + 7) then get the memory of this RAM address and add it to the stack?  
And does this differ to when you create the frame, where you would save the address of the current LCL/ARG/etc position?
Reply | Threaded
Open this post in threaded view
|

Re: Push/Pop LCL, ARG

WBahn
Administrator
I think you probably meant the right thing, but that's not what you wrote.

You wrote: (Memory(RAM[1]) + 7)

If RAM[1] has the value 270 in it, then this reduces to

(Memory(270) + 7)

(I'm using different arbitrary numbers because I can't see my earlier post while I'm writing this.)

Since Memory(270) and RAM[270] are identical ways of saying the same thing, let's stick to the RAM[] notation.

If RAM[270] has 4038 in it, then this ultimately reduces to 4045.

What you WANT is first recognize that

RAM[1] is the address of the start of the LCL segment.

If you want local 7, then it is 7 places beyond the start of the local segment, so the address of local 7 is

RAM[1] + 7

If we want the value stored in local 7, that's

RAM[RAM[1] + 7]

or, using your earlier notation

Memory(RAM[1] + 7)

Do you see the distinction between this and

(Memory(RAM[1]) + 7)
Reply | Threaded
Open this post in threaded view
|

Re: Push/Pop LCL, ARG

ElliotW
That's definitely a far clearer way of describing it.  
I found out the root of my issue was in the stack frames - in the translator, I tried to push the registers to reuse code, however this gave the memory instead of the addresses. I've modified it and the program now works, though the translator's code is not as clean as I hoped.
Thanks for the help