Opentopia Directory Encyclopedia Tools

Function prologue

Encyclopedia : F : FU : FUN : Function prologue



 

In assembly language programming, the function prologue is a few lines of code which appear at the beginning of a function, which prepare the stack and registers for use within the function. Similarly, the function epilogue appears at the end of the function, and restores the stack and registers back to the state they were in before the function was called.

The prologue and epilogue are not a part of the assembly language itself - rather, they represent a convention used by assembly language programmers, and compilers of many higher-level languages. Both of them are fairly rigid, having the same form in each function.

Prologue

The function prologue typically does the following actions (Note this procedure may differ from one architecture to another): Note that several possible prologues can be written, resulting in slightly different stack configuration. These differences are acceptable, as long as the programmer or compiler uses the stack in the correct way inside the function.

For example, these three steps may be accomplished in 32-bit x86 assembly language by the following instructions (using AT&T syntax):

push %ebp
mov  %esp, %ebp
sub  $n, %esp
Where n is the size of the local variables, in bytes. The above sequence is typical of the output produced by the GCC compiler.

A slightly different prologue is built-in to the x86 processor, and can be called with the enter instruction:

enter $n, $0
This instruction is not used by GCC because it sets up the stack frame slightly differently - it swaps the last two instructions of the prologue.

The difference in the stack setup is that under the GCC method (the 3-line prologue above), the base pointer points to the last argument, and local variables are accessed by negative offsets on the base pointer. Under the enter method, the base pointer points to the last local variable, and all local and argument variables are accessed by positive offsets. (The only difference is offsets from the base pointer).

Even more complex prologues can be obtained using different values (other than 0) for the second operand of the enter instruction. These prologues push several base/frame pointers to allow for nested functions, as required by languages such as Pascal.

Epilogue

The function epilogue reverses the actions of the function prologue and returns control to the calling function. It typically does the following actions (Note this procedure may differ from one architecture to another): Note that the given epilogue will reverse the effects of either of the above prologues (either the full one, or the one which uses enter).

For example, these three steps may be accomplished in 32-bit x86 assembly language by the following instructions (using AT&T syntax):

mov  %ebp, %esp
pop  %ebp
ret
Like the prologue, the x86 processor contains a built-in instruction which performs part of the epilogue. The following code is equivalent to the above code:
leave
ret
The leave instruction simply performs the mov and pop instructions, as outlined above.

It is not uncommon for a function to contain multiple epilogues. Every function exit point must either jump to a common epilogue at the end, or contain its own epilogue. Therefore, programmers or compilers often use the combination of leave and ret to exit the function at any point. (For example, a C compiler would substitute a return statement with a leave/ret sequence).

See also

External links

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.


Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: