kawakami wrote
Thank you very much for your support.
According to your advice, I got these chunk of code for "add".
@SP
D=M
@SP
M=M-1
@SP
M=M+D
Am I on the same page?
So let's desk check it and see what happens.
Let's say that SP is 503. That means that the two values to be added are in RAM[501] and RAM[502]. Let's just put some junk values in there.
RAM[0] = 503
RAM[501] = 42
RAM[502] = 58
When we are done, we want
RAM[0] = 502
RAM[501] = 100
We don't care what ends up in RAM[502] since it is no longer on the stack.
So now let's execute your code line by line:
@SP // A = 0, D = ? , M = 502, RAM[0] = 502, RAM[501] = 42, RAM[502] = 58
D=M // A = 0, D = 502, M = 502, RAM[0] = 502, RAM[501] = 42, RAM[502] = 58
@SP // A = 0, D = 502, M = 502, RAM[0] = 502, RAM[501] = 42, RAM[502] = 58
M=M-1 // A = 0, D = 502, M = 502, RAM[0] = 501, RAM[501] = 42, RAM[502] = 58
@SP // A = 0, D = 502, M = 502, RAM[0] = 501, RAM[501] = 42, RAM[502] = 58
M=M+D // A = 0, D = 502, M = 502, RAM[0] = 1003, RAM[501] = 42, RAM[502] = 58