A
stack frame is an area of storage
associated with the activation of a function. A stack frame that is allocated as
a function is entered, and it is deallocated when the function exits. Automatic
(auto)
variables are compiler-generated temporaries that are located in the stack
frame. It is organized as follows:
|
second parameter |
ESP+8 |
|
first parameter |
ESP+4 |
|
caller’s return
address |
ESP+0 |
|
saved frame pointer |
ESP-4 |
|
local variables and
temporaries |
ESP-8
ESP -n |
|
saved registers (if
any) |
|
Usually, it is the EBP
register that points to the stack frame. This register is used to address all
local variables and arguments. When possible, and when the user has specified
the –O (optimization) flag, lcc will not build a full stack frame, but just a
minimal one, consisting only of the saved return address and the registers used
by the procedure.
Lcc-win32 always saves
the registers EBX, EBP, ESI and EDI. Under no circumstances when writing
assembly modules should the values stored in these registers be lost.
A
prolog is the code executed immediately upon a function entry, i.e., the code
that builds the stack frame for the function and saves the registers that the
function will use. An epilog is the code executed to deallocate the stack frame,
and restore the saved values of the registers.
The
prolog sequence of lcc-win32 is:
push %ebp
; save the frame pointer
movl %esp,%ebp
; move the stack pointer to the frame pointer
subl <n>,%esp ; allocate space in the
stack for the function’s variables
push %esi
; save registers
push %edi
push %ebx
Symmetrically,
the epilog sequence looks like this :
pop %ebx
; restore the saved registers
pop %edi
pop %esi
movl %ebp,%esp
; restore the stack
popl %ebp
; restore the old frame pointer
ret
;return to caller. Caller responsible for cleaning the stack.