Function Prologue - Epilogue

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):

  • 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

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):

movl %ebp,%esp popl %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 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).

Read more about this topic:  Function Prologue

Famous quotes containing the word epilogue:

    Above all, Vietnam was a war that asked everything of a few and nothing of most in America.
    Myra MacPherson, U.S. author. Long Time Passing: Vietnam and the Haunted Generation, epilogue (1984)

    Where there is no vision, the people perish.
    Bible: Hebrew Proverbs, 29:18.

    President John F. Kennedy quoted this passage on the eve of his assassination in Dallas, Texas; recorded in Theodore C. Sorenson’s biography, Kennedy, Epilogue (1965)