Prologue
A function prologue typically does the following actions if the architecture has a base pointer (also known as frame pointer) and a stack pointer: *Pushes current base pointer onto the stack, so it can be restored later. *Value of base pointer is set to the address of stack pointer (which is pointed to the top of the stack) so that the base pointer will point to the top of the stack. *Moves the stack pointer further by decreasing or increasing its value, depending on whether the stack grows down or up. On x86, the stack pointer is decreased to make room for the function's local variables. 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 is a typical x86 assembly language function prologue as produced by the GCCenter
instruction:
enter
instruction. These prologues push several base/frame pointers to allow for nested functions, as required by languages such as Epilogue
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): *Drop the stack pointer to the current base pointer, so room reserved in the prologue for local variables is freed. *Pops the base pointer off the stack, so it is restored to its value before the prologue. *Returns to the calling function, by popping the previous frame's program counter off the stack and jumping to it. The given epilogue will reverse the effects of either of the above prologues (either the full one, or the one which usesenter
). Under certain calling conventions it is the callee's responsibility to clean the arguments off the stack, so the epilogue can also include the step of moving the stack pointer down or up.
For example, these three steps may be accomplished in 32-bit x86 assembly language by the following instructions:
leave
instruction performs the mov
and pop
instructions, as outlined above.
A function may 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).
Further reading
* {{cite web, url=http://jdebp.info/FGA/function-perilogues.html, title=The gen on function perilogues, work=Frequently Given Answers, author-first=Jonathan, author-last=de Boyne Pollard, date=2010 Subroutines Assembly languages