In assembly language programming, the function prologue is a few lines of code 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 to the state they were in before the function was called.
The prologue and epilogue are not a part of the assembly language itself; they represent a convention used by assembly language programmers, and compilers of many higher-level languages. They are fairly rigid, having the same form in each function.
Sometimes, function prologue and epilogue contain also buffer overflow protection code.
A function prologue typically does the following actions if the architecture has a base pointer (also known as frame pointer) and a stack pointer:
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.
As an example, here′s a typical x86 assembly language function prologue as produced by the GCC
The N immediate value is the number of bytes reserved on the stack for local use.
The same result may be achieved by using the enter
instruction:
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. However, modern versions of these languages don′t use these instructions because they limit the nesting depth in some cases.
Function epilogue reverses the actions of the function prologue and returns control to the calling function. It typically does the following actions (this procedure may differ from one architecture to another):