Understanding the problem Standard input sequence

Standard input sequence :

_function: push ebp ;store the old base pointer mov ebp, esp ;make the base pointer point to the current ;stack location – at the top of the stack is the ;old ebp, followed by the return address and then ;the parameters. sub esp, x ;x is the size, in bytes, of all ;"automatic variables" in the function 

at the top of the stack is the old ebp, and then the return address and then the parameters.

Old ebp exists because push ebp ,

but why is there a return address and parameters?

UPDATE

Standard exit sequence

 mov esp, ebp ;reset the stack to "clean" away the local variables pop ebp ;restore the original base pointer ret ;return from the function 

What does ret actually do? I think esp should already have reached the return address in the pop ebp line

+4
source share
2 answers

In the standard x86 standard convention, before calling a function, this parameter is first pushed onto the stack.

And call op means "push the next address onto the stack, then go to the function", so the return address is also on the stack.

This means that before push ebp stack looks like this:

  ... param2 param1 param0 return_address <- esp 

After calling push ebp it becomes

  ... param2 param1 param0 return_address ebp <- esp 

Finally, mov ebp, esp saves this esp to ebp , so you can reference the return address and all input parameters relative to ebp and free the stack for local use.

+4
source

All of this is part of the ABI. By convention, the caller creates a stack frame that contains parameters, etc., then calls a function (during which the return address is also pushed onto the stack). The called function allocates additional stack space for local variables and can refer to parameters and local variables through one common pointer and offset.

0
source

Source: https://habr.com/ru/post/1305452/


All Articles