How is the stack frame built in a function call ?

(1) The stack frame is empty right before we call a function:

Initial state: the stack frame for the function call f( ) is empty

How is the stack frame built in a function call ?

(2) The caller passes the parameters by pushing them on the stack:

The stack frame first contains the parameters of the function f( )

How is the stack frame built in a function call ?

(3) The bl instruction will jump to the function:

The stack frame is unchanged

How is the stack frame built in a function call ?

(4) The function f will save the the return address on the stack:

The stack frame now has parameters + return address

How is the stack frame built in a function call ?

(5) The function f must also save the the base address in FP in the stack:

The stack frame now has parameters + return address + old FP

How is the stack frame built in a function call ?

(6) The function f now safely establish the base address of the stack frame in FP

FP points to the "middle" of the (unfinished) stack frame

How is the stack frame built in a function call ?

(7) The function f will create its local variables using the stack:

The stack frame now has parameters + return address + old FP + local variables

How to allocate local variables on the runtime stack
 

  • Naive way to allocate n integer variables on the runtime stack:

        push {r0}
        push {r0} 
        ...            // n push operations
        push {r0}
    

  • A quicker (and better) way to allocate n integer variables on the runtime stack:

          sub  sp, sp, #4*n              
    

    Example: allocate 3 int local variables using the runtime stack:

       sub  sp, sp, #12
    

The prelude code of a subroutine with parameters and local variables on stack

Previously we saw that a subroutine always begins with these 4 assembler instructions:

The prelude code of a subroutine with parameters and local variables on stack

Using the sub instruction to allocate local variables:

The prelude code of a subroutine with parameters and local variables on stack
 

The prelude code of a subroutine with parameters and local variables on stack:

     push   {lr}         // Save return address
     push   {fp}         // Save caller's FP
     mov    fp, sp       // Set up base address in FP
     sub    sp, sp, #n   // Allocate n/4 int local variables  

The prelude code always appear as the first 4 assembler instruction in (non-leaf) subroutines that must uses the runtime stack to store their local variables and parameters

Summary: the structure of the stack frame

The stack frame is the data structure used to store the parameters and local variables of an active function

The stack frame always has the following structure:

The parameters and local variables are access using FP as base address

The figure gives the offsets that you need to use to access the parameters and local variables