Problems with passing parameters in registers

  • Passing parameters in registers will only work with 1 level of function calls

  • You can not use the registers again to call another function:

       int main( )          int f(int x, int y)       int g(int a, int b)   
       {                    {                         {
          f(x, y)               g(x, y)                  ...
       }                    }                         }
    

  • main( ) can pass parameters to f( ) in registers:

     main:                       f:
       pass x, y in r0, r1          uses r0, r1
       bl   f                       
                                    pass parameters in ???
                                    bl    g
    

    f( ) cannot use registers r0, r1 to pass parameters !

    (Because they are used to store its parameters)

The general way to pass parameters (and allocate local variables)

The general way to pass parameters and allocate local variables is using the runtime stack:

 

What happens when you pass parameters using the runtime stack

The (1) parameters. (2) return addresses and (3) local variables are stored in the program (runtime) stack:

 
         

We must be very careful and organize the data on the runtime (program) stack !!

Sequencing of events in function call + return with parameters and local variables
 

When can/do we pass the parameters to a function ?

 f1:                                           
                       +---->  f2:           
       ...             |            push {lr}  |
       ...             |            ...        |
       ...             |            ...        |
       bl f2 ----------+            ...        |
       ...   <--------------+       ...        |
       ...                  |       ...        V 
       ...                  +------ pop {pc}
   

 

 

Sequencing of events in function call + return with parameters and local variables
 

When can/do we pass the parameters to a function ?

 f1:                                           
                       +---->  f2:           
       ...             |            push {lr}  |
       ...             |            ...        |
       pass params     |            ...        |
       bl f2 ----------+            ...        |
       ...   <--------------+       ...        |
       ...                  |       ...        V 
       ...                  +------ pop {pc}
   

Answer:   it must happen before the bl f2 where we call the function !!!

(It's too late to pass parameters after the bl f2 instruction !)

Sequencing of events in function call + return with parameters and local variables
 

When can/do we create the local variables for the function f2 ?

 f1:                                           
                       +---->  f2:           
       ...             |            push {lr}  |
       ...             |            ...        |
       pass params     |            ...        |
       bl f2 ----------+            ...        |
       ...   <--------------+       ...        |
       ...                  |       ...        V 
       ...                  +------ pop {pc}
   

 

 

Sequencing of events in function call + return with parameters and local variables
 

When can/do we create the local variables for the function f2 ?

 f1:                                           
                       +---->  f2:           
       ...             |            push {lr}  |
       ...             |            ...        |
       pass params     |            alloc local vars
       bl f2 ----------+            ...        |
       ...   <--------------+       ...        |
       ...                  |       ...        V 
       ...                  +------ pop {pc}
   

Answer:   immediately after we saved the return address

(Because the local variables are created when a function starts executing)

Sequencing of events in function call + return with parameters and local variables
 

Due to the chronology of the events in the function invocation:

 f1:                                           
                       +---->  f2:           
       ...             |            push {lr}  |
       ...             |            push {fp}  |
       pass params     |            alloc local vars
       bl f2 ----------+            ...        |
       ...   <--------------+       ...        |
       ...                  |       ...        V 
       ...                  +------ pop {pc}
   

The order of of the items pushed/stored in the System Stack is:

  1. The parameters of the function              
  2. The return address (lr) of the function
  3. The FP (frame pointer) register of the caller (discussed soon)        
  4. The local variables of the function

The activation record or stack frame

Activation record or stack frame: ( click here)

  • Stack frame is the data structure that is created in the runtime stack containing:

    1. The parameters of the function              
    2. The return address (lr) of the function
    3. The FP (frame pointer) register of the caller (discussed soon)        
    4. The local variables of the function

  • Due to the order of insertion into the stack, the structure of an stack frame is as follows:

The activation record or stack frame

Activation record or stack frame: ( click here)

  • The stack frame is constructed:

    • Partly by the caller: the parameters
    • Partly by the callee: the rest of the stack frame

  • The function will access its parameters and local variables (in the stack frame) using:

    • The base address stored in the FP (frame pointer) register.

  • Important note:

    • The base address in register FP points to the "middle" of a stack frame !!

The stack frame as a data structure (= object)

  • A stack frame is a data structure (just like an object)

  • Fields in data structures (= objects) are accessed using a base address + an offset:

  • Base address of the stack frame:

    • Is stored in the special purpose register frame pointer (FP)

  • Local variables are accessed using negative offset values

  • Parameter variables are accessed using positive offset values