|
So I am on project 8 and things seem like they are going smoothly. I have implemented all but the "return" code generation and have tested them in my CPUemulator and checking memory segments manually and they seem to work properly. But I am not a c++ person and the concept of pointers is confusing. I know it's the "value pointed to by the variable." But I am having trouble comprehending ARG and *ARG. Right now I think of it as, say ARG=M[2]=400 and *ARG=M[400]. But if that's the case, wouldn't that be exactly similar to the previous command, FRAME=LCL? We are setting M[FRAME]=M[LCL].
See, here's how I'm implementing the code generation:
//*ARG=pop value pointed to by ARG=TOS
@SP
AM=M-1 //sp--
D=M //POP! D=TOS
@ARG
M=D //M[ARG]=M[2]=D=TOS=POP
//SP=ARG+1
@ARG
D=M+1 //D=M[ARG]+1
@SP
M=D //M[SP]=M[ARG]+1
The concept of SP=ARG+1 is starting to gel. We are setting the SP to the address after TOS (top of stack), which happens to have function returned ARG sitting on it. The pointer labeling keeps confusing me though. I'll be honest and say now that the previous command, *ARG=pop() is making less sense now. Are we setting M[ARG]=TOS?
|