I have learnt a few assembly languages (MIPS/x86/ARM/MIX) and I have a question about the call stack. Each of those languages uses a different method for subroutine linkage, and the way I learnt them was not conducive to recursion. Granted, if I continue reading my copy of The Art of Computer Programming Knuth claims that he will explain how to do it, however the way he writes I will have to understand everything before that point which will be awhile. I am impatient.
As such I would like to know, in a generic language, how does one implement proper subroutine linkage? So far I have pseudo-code like this:
;To call a subroutine [FP++]=PC+2; Push PC+2, (could be FP-- if stack grows negatively) SP=FP; Update the stack pointer PC=Function Address;Call the subroutine ;In the subroutine: ;To allocate a local variable [SP++]=R1; Save R1, so it can be used as a local ;To reference it [FP+1]=R1; Should be the same as above ;To return R1=[SP--]; Load R1 and update stack pointer PC=[--FP]; I think this is a mistake, because now FP is ruined for the calling function?
My question is how do you set the frame pointer to what it was in the calling function when you return? Especially since wikipedia seems to say that even before the frame pointer will be the function arguments, it seems like it would be impossible to maintain the frame pointer.