There is something I don't understand with controlling the SP. As I understand it, we want to move the pointer by incrementing its address, controlled by the A-register in the hack language. So for example, if I want to perform the following command:
push local 2
@SP //go to SP
D=A //store the current address of SP
M=D // store the address of SP in the memory of R13
A=A+2 //go to the base address of local(A)+2
D=M // Store the value at local 2
A=M //go to the address of SP
M=D //Store the value from local 2
A=A+1 //increment the SP pointer so that it points above the top value
To me, this makes sense. Have I got it all wrong?
Sorry for the convoluted post.
That puts the value 1 in the A register (because LCL is just a convenient name for 1, just like SP is a convenient name of 0).
If then go
you are trying to use a command that doesn't exist.
There are 28 defined commands in the Hack Assembly Language. Something like "A+1" is NOT a mathematical expression. It is a mnemonic -- a memory aid. They could have use "Add1toA" as the mnemonic. Or they could have called it "BobsFavoriteCommand". In all three cases, all the Assembler does is replace the mnemonic with a particular 16-bit pattern in the .hack file. There is NO relation to the mnemonic and what the command does beyond what we choose to imply when we choose the mnemonic.
But let's assume that there was and that A+2 was a legal mnemonic that added 2 and the value stored in the A register. All that would happen is that A would not be equal to 3.
At the end you have
This will first put 0 in the A register and then it will add 1 to 0 and put the result into the A register, so all this has accomplished is to get the value 1 into the A register. The is functionally identical to doing
except it takes two clock cycles instead of one.
In order to access a value stored in memory, you need to have the address of the RAM cell you want in the A register and then you need to use M, which is short for RAM[A].
So what you are trying to accomplish with your last two commands is the following result: