Note that there are some explanatory texts on larger screens.

plurals
  1. POstack frame align to different size?
    primarykey
    data
    text
    <p>It's a simple test. A similar topic has been discussed in <a href="https://stackoverflow.com/questions/1061818/stack-allocation-padding-and-alignment">Stack allocation, padding, and alignment</a> but when i compile the following two source code i have conflicting results.</p> <pre><code> #include &lt;stdio.h&gt; //first source code void haha() { printf("HI"); } int main() { int b = 2; b = b &lt;&lt; 1; haha(); //printf("n = %d",b); return 0; } </code></pre> <hr> <pre><code> #include &lt;stdio.h&gt; //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 &lt;&lt; 1; haha2(); //printf("n = %d",b); return 0; } </code></pre> <p>For the first source code i got the assembly code:</p> <pre><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 </code></pre> <p>For the second source code i got the assembly code:</p> <pre><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 </code></pre> <p>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 <a href="https://stackoverflow.com/questions/1061818/stack-allocation-padding-and-alignment">Stack allocation, padding, and alignment</a> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload