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.

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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