x = Math.sin( 3.14 ); |
The method invocation will allocate (= reserve memory) the parameter variables and the local variables of the method on the program stack:
In fact:
|
|
Notice that:
|
This is what we will be doing...
However.... things is a little bit more complicated that just storing parameters and local variables....
Because we must also store other things !!!!
(That's why the key to understanding how to store parameters and local variables using the program stack is: organization)
f: push {lr} // Save return address on STACK !!! .... pop {pc} // Use return address on stack to return. |
So when a non-leaf function begins execution, it must first push its return address on the program stack !!!
|
The stack will contain a complex object with different parts
We call this object: a stack frame or a "procedure activation record"
(More detail on this object later)
|
We will use a register to point to the stack frame to allow us to access the parameters and local variables.
This register is typically called the frame pointer register (or FP) because it points to a stack frame object.
|
And these events (with a fixed ordering) will therefore construct a structure on the program stack that always has the same specific structure
Therefore:
|
|
So the structure of the data stored in the stack when we use it to pass parameters and store local variables is always as follows:
To access the parameters of the subroutine/method, we use positive offsets from the FP (Frame Pointer) register
To access the local variables of the subroutine/method, we use negative offsets from the FP (Frame Pointer) register
|