1

I've disassembled some C/C++ codes and realized the stack boundary is specified at beginning of a procedure, somehow like this :

1. push ebp
2. mov ebp , esp
3. sub esp , <power> ; <power> is specified by mpreferred-stack-boundary=2^power

The above code is used to create stack frames, But What I need to know is why this subtract is used, Cause the stack grows down (by using push for local variables) and such the subtract causes :

---------------------------- ^
by sub esp,<power> <---- esp |
.                            |
.                            |
.                            |
---------------------------- |
esp <---- esp                |
---------------------------- |
ret address                  |
---------------------------- |

The above picture shows that.So when in the rest of code if you have such the following codes:

push var1

It should be taken at the top of it so it looks like this :

-----------------------------
var1 (4 byte)                
-----------------------------^
by sub esp,<power> <---- esp |
.                            |
.                            |
.                            |
---------------------------- |
esp <---- esp                |
---------------------------- |
ret address                  |
---------------------------- |

So the space between esp and var1 is going to be free without any use ? that's what I want to know.

1 Answer 1

3

So the space between esp and var1 is going to be free without any use ? that's what I want to know.

That stack space is used for the function's local variables (also known as "stack variables").

Here's a nice animated GIF that shows a function's stack frame being created and used to store local variables:

GIF

2
  • So thanks.I've completely forgotten that variables are put in stacks by something like this(for instance): mov [ebp-4] , var1 Feb 11, 2015 at 20:03
  • Also arguments for called functions are pushed onto the stack.
    – Igor Skochinsky
    Feb 12, 2015 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.