So far I have - out of convenience - used the temp registers i.e., R5,R6, etc. liberally when implementing the "pop segment i" command to save temporary variables. I can see that I will probably use the same strategy in implementing the gt and lt commands (to overcome issues that have arisen in
http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Implementations-of-gt-lt-and-eq-in-asm-td4032816.html). This means that a call like "pop argument 9" may edit the values in these temp registers.
Question: is this allowed? Or does our contract assume that a call to "pop argument 9" does not alter the contents of the temp registers?
For reference, here is my implementation of pop argument 9... notice the @R5 and @R6 lines which make use of temporary variables.
// pop argument 9
@SP
M=M-1
A=M
D=M
// save it for later in the temp, as we will need to put it in the right location in RAM
@R5
M=D
@2
D=M
@9
D=D+A
// So D is now the actual index of RAM where we want to place the value popped off the stack
@R6
M=D
// finally place the saved value
@R5
D=M
@R6
A=M
M=D