stack frame align to different size?
It's a simple test. A similar topic has been discussed in Stack allocation, padding, and alignment but when i compile the following two source code i have conflicting results.
#include <stdio.h>
//first source code
void haha()
{
printf("HI");
}
int main()
{
int b = 2;
b = b << 1;
haha();
//printf("n = %d",b);
return 0;
}
#include <stdio.h>
//second source code
void haha3()
{
int c,a,b;
c=a+b;
}
void haha2()
{
int c,a,b;
c=a+b;
haha3();
}
int main()
{
int b = 2;
b = b << 1;
haha2();
//printf("n = %d",b);
return 0;
}
For the first source code i got the assembly code:
haha:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $.LC0, %eax
movl %eax, (%esp)
call printf
leave
ret
.size haha, .-haha
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $2, 12(%esp)
sall 12(%esp)
call haha
movl $0, %eax
leave
ret
For the second source code i got the assembly code:
haha3:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl -4(%ebp), %eax
movl -8(%ebp), %edx
leal (%edx,%eax), %eax
movl %eax, -12(%ebp)
leave
ret
.size haha3, .-haha3
.globl haha2
.type haha2, @function
haha2:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl -4(%ebp), %eax
movl -8(%ebp), %edx
leal (%edx,%eax), %eax
movl %eax, -12(%ebp)
call haha3
leave
ret
.size haha2, .-haha2
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl $2, -4(%ebp)
sall -4(%ebp)
call haha2
movl $0, %eax
leave
ret
My question is why the stack frame of the first code aligns to 24(subl $24, %esp) and the second aligns to 16(subl $16, %esp)? It's said in Stack allocation, padding, and alignment the SSEx family of instructions REQUIRES packed 128-bit vectors to be aligned to 16 bytes. So, I expect the stack frame of the first code has a subl $32, %esp and subl $16, %esp for the second one, or subl $24, %esp for the first and subl $8, %esp for the second because of the leave and ret reserved 8 bytes as it said. However, the fact is the first code use 'subl $24, %esp' and the second use 'subl $16, %esp' Thank you for your interest.
链接地址: http://www.djcxy.com/p/80342.html上一篇: 了解C程序的汇编语言输出
下一篇: 堆栈帧对齐到不同的大小?