How much space is allocated by subtracting from% esp in a function call?

C ++ assembly ATT

I have the following build code:

push %ebp mov %esp, %ebp sub $0x28, %esp (...) 

My tutorial claims that by subtracting 0x28 from% esp (as part of stacking), 12 bytes are allocated for variables. Why does subtracting 40 decimal from the stack allocate 12 bytes?

+6
source share
2 answers

This allocates 40 bytes on the stack. However, non-local variables are used for it, so I assume that the rest are used for alignment and arguments for a future function call.

Since function arguments are also passed on the stack, there must be space for anyone that this function wants to pass to another. This space can be allocated when making a call using push , but quite often allocate space once at the beginning of the function and simply use mov to place data at a position later. If your function uses 12 bytes for local variables, this leaves up to 28 function arguments that will be used later.

Can also be highlighted a bit for alignment. In addition to aligning the variables mentioned by Jerry, many systems expect the stack pointer to be aligned with a specific value, so you need to save this if you intend to make a function call. On 32-bit systems, this is often 8 bytes, but in this case there may also be 16.

+5
source

I suspect that you may have read the book incorrectly, but if you did not, I really like it, as if the book was wrong.

Subtracting 40 from the stack pointer allocates 40 bytes. This may not always be correct 1, but any deviation from it will usually be quite small.


  • For example, if you allocate an 8-byte object in 32-bit code, it may allocate some extra space (12 bytes in total) so that it can provide an 8-byte object with 8-byte alignment. Similarly, in 32-bit code, you can usually set the stack pointer in (at least) 32-bit increments, so a function that has one local char variable will usually subtract at least 4 from the stack pointer to make room for of this.
+3
source

All Articles