Function Prologue - Prologue

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 (the following actions may not be applicable to those architectures that are missing a base pointer or stack pointer) :

  • Pushes the old base pointer onto the stack, such that it can be restored later (by getting the new base pointer value which is set in the next step and is always pointed to this location).
  • Assigns the value of stack pointer (which is pointed to the saved base pointer and the top of the old stack frame) into base pointer such that a new stack frame will be created on top of the old stack frame (i.e. the top of the old stack frame will become the base of the new stack frame).
  • 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 variables (i.e. 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′s a typical IA-32 assembly language function prologue as produced by the GCC:

pushl %ebp movl %esp,%ebp subl $N,%esp

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

Read more about this topic:  Function Prologue

Famous quotes containing the word prologue:

    A popular Government, without popular information, or the means of acquiring it, is but a Prologue to a Farce or a Tragedy.
    James Madison (1751–1836)