In
assembly language
In computing, assembly language (alternatively assembler language or symbolic machine code), often referred to simply as assembly and commonly abbreviated as ASM or asm, is any low-level programming language with a very strong correspondence bet ...
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
programmer
A programmer, computer programmer or coder is an author of computer source code someone with skill in computer programming.
The professional titles Software development, ''software developer'' and Software engineering, ''software engineer' ...
s, and
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
s of many higher-level
languages
Language is a structured system of communication that consists of grammar and vocabulary. It is the primary means by which humans convey meaning, both in spoken and signed forms, and may also be conveyed through writing. Human language is ch ...
. They are fairly rigid, having the same form in each function.
Function prologue and epilogue also sometimes contain code for
buffer overflow protection.
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
x86 assembly language is a family of Low-level programming language, low-level programming languages that are used to produce object code for the x86 class of processors. These languages provide backward compatibility with CPUs dating back to th ...
function prologue as produced by the
GCC
push ebp
mov ebp, esp
sub esp, N
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:
enter N, 0
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 function
In computer programming, a nested function (or nested procedure or subroutine) is a named function that is defined within another, enclosing, block and is lexically scoped within the enclosing block meaning it is only callable by name within t ...
s, 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.
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 uses
enter
). 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:
mov esp, ebp
pop ebp
ret
Like the prologue, the
x86
x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set computer (CISC) instruction set architectures initially developed by Intel, based on the 8086 microprocessor and its 8-bit-external-bus variant, the 8088. Th ...
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 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