Procedure Stack Frames

 

The necessary data for the operation of a procedure is saved on a stack. There is a set of records for every (still executing) procedure, which is put on the stack in the order the procedures are called. This set of records is called the procedure's frame. The frame at the top of the stack is called the active frame and is for the currently executing procedure. This set of records includes information necessary for proper execution, including;

 

 

 

The stack pointer (%esp) is used to keep track of the top of the stack. The frame pointer (%ebp) is used to track the start of the active frame.

 

During the execution of some procedure the stack will look like this, where Callee is the currently executing procedure (active frame) and Caller is the procedure that called Callee. Certain registers are the responsibility of the Caller to protect prior to making a call, if it uses those regeisters after the call. These are referred to as the caller save registers. The other registers are the responsibility of the Callee to protect if it uses those registers during the procedure. These are referred to as the callee save registers.

 

Caller save registers:

 

 

Callee save registers:

 

 

The stack looks like this during a call to Callee:

 

Bottom of stack (at high memory, usually)

 

Address

Contents

 

 

.

\

 

.

| Earlier frames for previous

 

.

| procedures that have not

 

.

/ finished executing

 

.

\

 

.

|

 

Caller save registers

|

 

.

|

4n+4(%ebp)

Argument n

|

4n(%ebp)

Argument n-1

| Caller's frame

 

.

|

12(%ebp)

Argument 2

|

8(%ebp)

Argument 1

|

4(%ebp)

Return address

/

%ebp

Caller's frame pointer (%ebp)

\ Frame pointer points here

 

Callee save registers

|

 

.

| Callee's frame (active)

 

Local and temp variables

|

%esp

.

| Stack pointer points here

 

Top of stack (stack grows down)

 

Before Callee makes a call to another procedure, it will add arguments and a return address to the stack.

 

The Caller's process for setting up the stack prior to a call is:

 

 

Then the Callee must set up the its frame as follows:

 

 

After the Callee has finished the procedure, it must clean up its frame and restore the Caller's frame like this:

 

 

When control returns to the Caller, it needs to clean up after the call like this: