|
I'm not sure if I'm understanding the protocol for call to and returns from functions so let me type out my understanding of the process and hopefully someone can correct any errors in my thinking. Even if no one reads this, it'll help me to write it out.
CALL F N:
we would already have pushed N number of arguments for the called function, say we pushed 1,2,3
push return-address = this is simply a label
push LCL = this is a pointer which holds the base memory address of the callers local seg
push ARG = a pointer to base memory address of callers argument seg
push THIS = a pointer to address in memory the caller calls 'this'
push THAT = a pointer to address in memory the caller calls 'that'
ARG = SP-N-5 = set called functions ARG to beginning of 1,2,3 (so a pointer to '1')
LCL = SP = set called functions local seg to current pointer because function will push k
zeros at beginning to hold these local variables where k is the # of local var
write goto F
write (return-address label we pushed above)
RETURN:
FRAME = LCL = FRAME is a temp variable so perhaps could be R5 and is now a pointer to SP
which is essentially location of return value from called function + 1
RET = *(FRAME-5) = make RET(a temp variable, say R6) our return address label...I'm assuming
the asterix means "value at this address"
*ARG = pop() = pop() gives us the return value of the called function, make ARG a pointer
to the memory address that holds this value
SP = ARG + 1 = this sets SP just above the return value of called function(so caller can pop
it off
THAT = *(FRAME-1) = this makes THAT a pointer to the same address as before the function call
THIS = *(FRAME-1) = this makes THIS a pointer to the same address as before the function call
ARG = *(FRAME-1) = this makes ARG a pointer to the same address as before the function call
LCL = *(FRAME-1) = this makes LCL a pointer to the same address as before the function cal
goto return label we set
Question: THIS and THAT are locations in the heap? Are LCL ARG always in the global stack?
Joseph
|