Introduction to x86 Assembly
ebp ; Pointer to the start of the current stack-frame.
esp ; Pointer to the end of the stack
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.
- Pushes current base pointer onto the stack, so it can be restored later.
- Assigns the value of base pointer 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.
mov ebp, esp
sub esp, N
- 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.
leave is a shortcut for :
mov esp, ebp
Arguments order :
If there are more than six parameters, then the program’s stack is used to pass in additional parameters to the function.
The program’s stack is used to pass all the parameters to the function.
Example with a third arguments function :