Function prologue

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):
*Pushes the current base pointer onto the stack, so it can be retrieved later.
*Replaces the base (or frame) pointer with the current stack pointer, so the base pointer now points to the beginning of the new frame.
*Moves the stack pointer further along the stack to make room in the current stack frame for the function's local variables.

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): pushl %ebp movl %esp, %ebp subl $"n", %espWhere "n" is the size of the local variables, in bytes, and "l" means long, which occupies 4 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

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):
*Replaces the stack pointer with the current base (or frame) pointer, so the stack pointer is restored to its value before the prologue.
*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.

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 retThe 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).

ee also

*Call stack
*Assembly language
*x86 assembly language

External links

* [http://www.cs.ucla.edu/~kohler/class/04f-aos/ref/i386/ENTER.htm 80386 Programmer's Reference Manual - ENTER Instruction]
* [http://www.cs.ucla.edu/~kohler/class/04f-aos/ref/i386/LEAVE.htm 80386 Programmer's Reference Manual - LEAVE Instruction]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Gran Turismo 5 Prologue — Infobox VG title = Gran Turismo 5 Prologue caption = European box art developer = Polyphony Digital publisher = Sony Computer Entertainment distributor = Polyphony Digital designer = Kazunori Yamauchi series = Gran Turismo released =PlayStation 3 …   Wikipedia

  • The Friar's Prologue and Tale — The Friar s Tale is one of The Canterbury Tales by Geoffrey Chaucer, told by Hubert the friar.The tale is a satirical and somewhat bitter attack on the profession of summoner mdash;an official in ecclesiastical courts who summons people to attend …   Wikipedia

  • Subroutine — In computer science, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. The syntax of many programming… …   Wikipedia

  • Call stack — In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run time stack, or machine stack, and… …   Wikipedia

  • Algorithmic efficiency — In computer science, efficiency is used to describe properties of an algorithm relating to how much of various types of resources it consumes. Algorithmic efficiency can be thought of as analogous to engineering productivity for a repeating or… …   Wikipedia

  • covenant — covenantal /kuv euh nan tl/, adj. /kuv euh neuhnt/, n. 1. an agreement, usually formal, between two or more persons to do or not do something specified. 2. Law. an incidental clause in such an agreement. 3. Eccles. a solemn agreement between the… …   Universalium

  • biblical literature — Introduction       four bodies of written works: the Old Testament writings according to the Hebrew canon; intertestamental works, including the Old Testament Apocrypha; the New Testament writings; and the New Testament Apocrypha.       The Old… …   Universalium

  • Islamic arts — Visual, literary, and performing arts of the populations that adopted Islam from the 7th century. Islamic visual arts are decorative, colourful, and, in religious art, nonrepresentational; the characteristic Islamic decoration is the arabesque.… …   Universalium

  • Fonction zêta de Riemann — La fonction zêta de Riemann ζ(s) dans le plan complexe. La couleur d un point s code la valeur de ζ(s) : des couleurs vives indiquent des valeurs proches de 0 et la nuance indique l argument de la valeur. Le point blanc pour s = 1… …   Wikipédia en Français

  • Fonction Zeta de Riemann — Fonction zêta de Riemann En mathématiques, la fonction ζ de Riemann est une fonction analytique complexe qui est apparue essentiellement dans la théorie des nombres premiers. La position de ses zéros complexes est liée à la répartition des… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”