There are two simple C source files. The first is mainswap.c:
void swap(int *x, int *y);
int main()
{
int a, b;
a = 5;
b = 44;
swap(&a, &b);
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
The other is mainfoobar.c:
int bar(int x, int y)
{
int z = x + y;
return z;
}
int foo(int a, int b)
{
return bar(a, b);
}
int main(void)
{
foo(2, 3);
return 0;
}
I got the relocatable object files of both. And I found that the gcc does something about alignment for the stack frame of function main
in mainswap.c
, meanwhile gcc does not do explicitly for the function main
in mainfoobar.c
.
The main of mainswap.c:
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $5, 24(%esp)
movl $44, 28(%esp)
leal 28(%esp), %eax
movl %eax, 4(%esp)
leal 24(%esp), %eax
movl %eax, (%esp)
call swap
movl $0, %eax
leave
ret
The main of mainfoobar.c:
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $3, 4(%esp)
movl $2, (%esp)
call foo
movl $0, %eax
leave
ret
I know the intention of andl $-16, %esp
and subl $32, %esp
. The same gcc version, the same options, the same computer, the only difference is the C source files. My question is why does gcc treat the two main functions differently, what is behind this phenomenon?
In addition, the version of gcc I used is:
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright © 2011 Free Software Foundation, Inc.
By the way, in function main
of mainswap.c, I think that subl $16, %esp
will also satisfy the alignment and the demand of use, why does gcc waste 16 bytes?