Reputation: 11
int main(){
int i;
}
I did $ebp-$esp
in gdb after calling the function and it gave me 4. But what about old ebp
? Can I consider this function needs 8 bytes on the stack? 4 bytes for an int
and 4 bytes for an old ebp
.
note- I built it by using -mpreferred-stack-boundary=2 -march=i386
Upvotes: 1
Views: 2391
Reputation: 18492
How much memory that function requires is going to depend entirely upon the compiler you're using. This is the disassembly of the above code using gcc 4.6.2 with the compile command:
gcc -mpreferred-stack-boundary=2 -march=i386 -m32 -o test test.c
0x08048394 <+0>: push ebp
0x08048395 <+1>: mov ebp,esp
0x08048397 <+3>: pop ebp
0x08048398 <+4>: ret
What's fairly obvious is that i
is ignored, since it's not used at all. Therefore you have 4 bytes on the stack for the return address pushed by call
, and another 4 for preserving ebp
.
As you'd expect, if you enabled slight optimisations with the -O1
compile flag it becomes:
0x08048394 <+0>: ret
Since there's no need for a stack frame at all, main
does nothing.
Upvotes: 0
Reputation: 55392
Assuming you're compiling without optimisations, you're not counting the old ebp, because esp gets copied to ebp after the old ebp is pushed on the stack. I think what you're seeing is gcc saving ecx although offhand I'm not sure why it does.
Upvotes: 1
Reputation:
Depends entirely on how the compiler decides to optimize the function. Some optimizing compilers might build this function as something along the lines of "main: ret
", causing it to use no stack space at all.
Upvotes: 1