Summary: passing parameters and allocating
local variables using the program stack
Summary: a "how to guide" to pass parameters and store local variables
using the stack
Summary:
(a how to guide)
When the stack is
used to pass parameters and store local variables,
the method/function will
alwaysconstruct
(= organize) the
(1) parameters, (2) local variable, (3) FP and
(4) return address in the following
data structure
as follows:
This data structure is
called a
(subroutine) activation record or
stack frame
These are the
prescribed action
(= recipe) that
a method (program) must do
in order for a method/function
to use the stack
to
pass parameters and store local variables:
The calling method will
push the
parameters on the
stack
and the
call the
method
Example:
load parameter 3 into r0
push {r0}
load parameter 2 into r0
push {r0}
load parameter 1 into r0
push {r0}
bl Method/Function
This will result in the following stack:
When the called function returns, the
pushed parameters will
still be on the stack !!!
The
calling functionmustde-allocate the
pushed parameters from the
stack
Example:
add sp, sp, #12 // De-allocate 12 bytes from the stack
When the called method/functionstarts running, the
stack will only
contain the
parameters:
The called method/function must
first
use this specific sequence of
assembler instruction to
build/complete
the stack frame (activation record) structure
:
push {lr} // Save the shared LR reg (contains return addr)
push {fp} // Save the shared FP reg (contains the base addr of stack frame)
mov fp, sp // Establish the base address in the FP register
sub sp, sp, #N // Allocate N bytes for local variables
This sequence of instruction is
called the
prelude of
a method and will
build this
data structure
(it's called an activation record or
stack frame):
After the prelude, you can
write the instructions of the
method (in assembler code)
The instructions can access
the parameters and
local variable using
some offset from
the base address in the FP register:
The offset of the
1st parameter is
8
(from the base address in FP)
The offset of the
2st parameter is
12
(from the base address in FP)
Etc.
The offset of the
1st local variable is
-8
(from the base address in FP)
The offset of the
2st local variable is
-4
(from the base address in FP)
Etc.
When the method/function want to
return (to its caller), the
method/function must use
this specific sequence of
assembler instructions
to break down the
stack frame and return:
put the return value in the return location (usually r0)
mov sp, fp // De-allocates the local variables
pop {fp} // Restore the old FP value
pop {pc} // Return to the caller
This sequence of instruction is
called the
postlude of
a method
Return value of a function
Note:return value
Passing the
return value is
independent from
passing parameters
Because the
return value
can be contained
in a register,
we can always
pass the
return value using
a register !!!
We will typically use
register r0
to pass
the return value back to
the caller method