huskeyw wrote
If I take a address
@R0
and then I should be able to do M=M+M but that does not work (its illegal.. )
No, you shouldn't be able to do that. Look at the hardware. To add two things together, you have to get the two things to the inputs of the ALU. However, one input of the ALU is ALWAYS the contents of the D register. The other is either the contents of the A register, or the contents of the memory location whose address is in the A register.
Your logic has to reflect the capabilities of the hardware. The Hack hardware is extremely limited (though even so it is capable of doing things that most "real" hardware is not capable of). Furthermore, the Hack is quite on par with many low-end embedded microcontrollers where cost is everything; the low end PIC lines from Microchip, such as the PIC 16C54, had members that had 33 instructions (which included controlling several peripherals), a single register and just 18 bytes of RAM. That's it.
So I do D=M (which should assine D to the Value in the memory location) and then do a M = A+D
I think you mean the right thing, but you are saying it backwards.
If assigns the value in the memory location to the D register -- the value in D is changed, the value in M is not.
If you do M = A + D, then you are changing the value stored in the memory location whose address is in A and setting it to the sum of the value in D and the value in A, which is an ADDRESS.
Think about whether you really want to do that.
You probably want to do
M = D + M
or perhaps
D = D + M
depending on your overall algorithm.
@R0
M=A+D
I should get the memory address is now assigned the value of A+D
A has a value of 0 (that's what the instruction @R0 did -- set A to 0); don't lose sight of that.
Try taking a piece of paper and put four columns on it -- Instruction, D, A, and M. Then track the contents of those three memory locations as each instruction is executed. Any time A changes, M changes (though, of course, the actual value could end up being the same if those two memory locations just happen to have the same values stored in them).