What is the best way to handle having the return address of a calling function on top of any parameters that have been placed in the stack when you need to access parameters 'below' it in the stack? I am using the S12 Motorola/Freescale processor. (The S12 has: D register with 16 bits consisting of A and B registers each of which has 8 bits. X and Y index registers each 16 bits, a program counter and a stack pointer) A sample of the code is:
MAIN ORG $1500 ;Set the start of the program
LDD #SomeValue ;Load the D register with a value
PSHD ;Push D onto the stack
JSR ROUTINE ;Go to the subroutine - pushes the PC to the SP
END_MAIN END
ROUTINE
PULD ;Pull the top of the stack into the D register
;D now holds the address for returning to the
;main function.
PSHD ;Push the return address back onto the stack
END_ROUTINE RTS ;Return to Main routine
The problem is that the top of the stack holds the address for the next instruction which makes it difficult to maneuver around. For example, if I needed a parameter that is underneath the the address, I would have to either manually adjust the SP (which seems rather hacky) or I will have to pull the top of the stack off and store it in a register which takes up space. A twist on the last approach is to store the return address in a variable, unfortunately, the variables declared here are global in scope, which does not feel ideal.
ReturnAddress EQU $2000 ;Declare variable at address $2000
STD ReturnAddress ;Store the D register's contents in variable
Is there another option which I am not seeing here?
Thanks to some input from Jester, it is fairly simple to be able to observe what is happening in the stack and to use parameters contained therein. I wrote a simple program that demonstrates passing parameters by value and reference.