Using the following C code
void func() {
int a=1,b=2,c=3;
}
Compiling using gcc -S -O -o- myfile.c
I get the output
.file "myfile.c"
.intel_syntax noprefix
.text
.globl func
.type func, @function
func:
push ebp
mov ebp, esp
sub esp, 16
mov DWORD PTR [ebp-4], 1
mov DWORD PTR [ebp-8], 2
mov DWORD PTR [ebp-12], 3
mov DWORD PTR [ebp-16], 4
mov DWORD PTR [ebp-20], 5
leave
ret
.size func, .-func
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
.section .note.GNU-stack,"",@progbits
Here I would expect the third line after func:
to be sub esp,12
instead of sub esp,16
. I played with different numbers of automatic variables in the function and found that it grows in increments of 16 bytes. Why does this happen? Does this happen on all platforms, or is it platform specific?
I'm currently running an Intel Mac with OSX, compiling through an Ubuntu (32-bit) VirtualBox guest using GCC开发者_运维技巧.
From GCC man page, (bold emphasis mine):
-mpreferred-stack-boundary=num
Attempt to keep the stack boundary aligned to a 2 raised to num byte boundary. If
-mpreferred-stack-boundary
is not specified, the default is 4 (16 bytes or 128 bits).
That is very strange output.
Are you sure the c file wasn't:
void func() {
int a=1,b=2,c=3,d=4,e=5;
}
?
Otherwise why the lines
mov DWORD PTR [ebp-16], 4
mov DWORD PTR [ebp-20], 5
x86_64 ABI requires the stack to be 16-byte aligned
Stack allocation, padding, and alignment
For 32-bit OS X then it does that because of SSE and some other reasons
Why does the Mac ABI require 16-byte stack alignment for x86-32?
精彩评论