Pnick0509 wrote
Hello. I've really only gotten to this point by a lot of trial and error and reading the textbook. Things have been difficult to understand so far but now its seeming impossible. When I asked my professor about this they only gave me "add" as an example but I'm not sure what it even means. I've been trying to figure it out for a few hours now trying to decipher it by trial and error to no avail.
@SP
AM=M-1
D=M
A=A-1
M=D+M
My understanding is as follows:
>@SP loads the stack pointer into register M. The stack pointer is pointing the the position after the top value.
>AM=M-1 sets Registers A and M to point to the top value in the stack
After this point I am really uncertain about anything. I don't understand why we decrement A but then don't do anything with it. There are clearly some intersections I am just not seeing. Any help would be greatly appreciated.
Oh, but you DO do something with A.
What happens with
D=M
If A has the address of the value that is on top of the stack in it, what value gets placed into D?
Desk check it.
Make up some numbers and walk through and see what happens using pen and paper by tracking what happens as a result of each instruction. For instance:
D = 3287
A = 9826
R[0]: 368 // SP
R[365]: 100
R[366]: 12
R[367]: 42
R[368]: 97
R[369]: 817
Those are just numbers I made up, except that I know that the value in R0 is the stack pointer so I picked a random value between 256 and 2047 (making sure it was large enough to have at least a few value on the stack) and then put some random values in the memory locations near the top of the stack.
Now see what happens to these registers and RAM locations as each instruction is executed and what the end result is. Can you describe that that result is in terms of the values initially present at the top of the stack?