Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly: why do we subtract esp if the push command already does the subtraction?

According to disassembler, there are some functions look like

sub esp, 0x20
push some registers
do some stuffs
pop the registers
add esp, 0x20
ret

At first I thought it was to allocate memory for local variables, but I realize there are other functions look like just simply

push some registers
do stuffs
pop some registers
ret

which does not subtract esp manually.

I have done some searching and apparently the push command already automatically subtract esp for the size of the value pushed; if this is the case, then why in the first function we still need to manually subtract esp at the beginning of the function? Or in other words, what exactly is the difference in purpose between the two functions?

like image 790
cr001 Avatar asked Dec 06 '25 03:12

cr001


1 Answers

I'ts a reservation (allocation) of stack space for local variables. Also, some versions of ABI may require specific stack pointer alignment upon function entry

like image 116
Serge Avatar answered Dec 08 '25 22:12

Serge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!