beef3667 wrote:
Is there a way to store the stack pointer, push your values and such onto the stack and then access them from your previous reference to the stack pointer?
I believe this is what I just described. The stack pointer always points to the next memory location where a value will be 'pushed'. By transferring S to X, you can read values at an arbitrary distance from the top of the stack ($0101,X will give you the last value pushed, $0102,X will give you the 2nd-last value, $0103,X for the 3rd-last value, etc).
Quote:
It seems this would be easier than trying to calculate the offsets from the top of the stack as then the reference (in this case not the actual stack pointer but the previous address of it before you pushed all your values onto the stack) wouldn't keep changing. Any new items added would then just get a new offset value and the old offset values would not need to be recalculated.
As long as a function has a fixed number of 'parameters', the "base address" for each parameter will always be the same. In my example, param1 will always be at $0105,X and param2 will always be at $0106,X (if X/A are not pushed onto the stack at the beginning, then the parameters will be at $0103,X and $0104,X, respectively).