Dereferencing

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

Dereferencing

The_Larks
Hey all, for some reason I'm really stumbling with dereferencing in project 8. I'd prefer to figure it out on my own, so any hints would be appreciated. I fully understand dereferencing in C, eg, but the only example I can think of in nand2tetris is the code @SP, AM = M-1. I have played around with the VMUEmulator and the only way I can seem to mimic it is to load the address into a temp register, and then execute "A=M""A=M" (yes, 2 times!). I'm sure that's not the correct approach.

Please help!
Reply | Threaded
Open this post in threaded view
|

Re: Dereferencing

cadet1620
Administrator
Yes, @p, A=M, A=M is Hack for A=*p.

I grepped my code and didn't see anywhere I wrote that sequence, but I did write lots of D=*p and *p=D sequences.  (Stack operations probably use something like A=M, M=MopD so my simple grep didn't find them.)

If you are talking about handling "pop that n" type instructions, I also used one of the R13-15 temp registers to hold the destination address.

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

Re: Dereferencing

The_Larks
So, I figured it out:

1)

A=M
A=M

does in fact work.

2) My problem was I stored the address of the return address, rather than just the return address, so two dereferences were necessary...
Reply | Threaded
Open this post in threaded view
|

Re: Dereferencing

cadet1620
Administrator
I don't know the context where you are saving the return address's address, but if you are saving it across the function call, you will likely have problem with recursive functions.

If you are just storing it temporarily during your ASM code for call, that should not be a problem.

--Mark