Memory Allocation On Stack

Pankaj Kohli pankaj208@gmail.com
Sat Jul 21 19:02:00 GMT 2007


What about this one ? Three PUSHes + sub 0x10,%esp. That makes 28 bytes.
Stack is not aligned on 16-byte boundary in this case.


(gdb) list 8
3       int main(int argc, char **argv) {
4               int a, b, c;
5
6               a = 1;
7               b = 2;
8               c = a + b;
9
10              return 0;
11      }
12
(gdb) disassemble main
Dump of assembler code for function main:
0x08048344 <main+0>:    lea    0x4(%esp),%ecx
0x08048348 <main+4>:    and    $0xfffffff0,%esp
0x0804834b <main+7>:    pushl  0xfffffffc(%ecx)
0x0804834e <main+10>:   push   %ebp
0x0804834f <main+11>:   mov    %esp,%ebp
0x08048351 <main+13>:   push   %ecx
0x08048352 <main+14>:   sub    $0x10,%esp
0x08048355 <main+17>:   movl   $0x1,0xfffffff0(%ebp)
0x0804835c <main+24>:   movl   $0x2,0xfffffff4(%ebp)
0x08048363 <main+31>:   mov    0xfffffff4(%ebp),%eax
0x08048366 <main+34>:   add    0xfffffff0(%ebp),%eax
0x08048369 <main+37>:   mov    %eax,0xfffffff8(%ebp)
0x0804836c <main+40>:   mov    $0x0,%eax
0x08048371 <main+45>:   add    $0x10,%esp
0x08048374 <main+48>:   pop    %ecx
0x08048375 <main+49>:   pop    %ebp
0x08048376 <main+50>:   lea    0xfffffffc(%ecx),%esp
0x08048379 <main+53>:   ret
End of assembler dump.



On 7/20/07, Andrew Haley <aph-gcc@littlepinkcloud.com> wrote:
> Pankaj Kohli writes:
>  > Yeah, that was me who asked it on kerneltrap :)
>  > If it is trying to align ESP on a 16-byte boundary, that seems fine
>  > for a single integer variable or anything less than 16 bytes, but why
>  > is it allocating 116 bytes for 100 byte buffer ? That doesn't fit on a
>  > 16-byte boundary.
>
> Where's the mystery?
>
>  > Dump of assembler code for function main:
>  > 0x080483a4 : lea 0x4(%esp),%ecx
>  > 0x080483a8 : and $0xfffffff0,%esp
>
> sp = sp & -16 // sp is 16-aligned
>
>  > 0x080483ab : pushl 0xfffffffc(%ecx)
>
> sp -= 4  // sp is 4-aligned
>
>  > 0x080483ae : push %ebp
>
> sp -= 4  // sp is 8-aligned
>
>  > 0x080483af : mov %esp,%ebp
>  > 0x080483b1 : push %ecx
>
> sp -= 4  // sp is 4-aligned
>
>  > 0x080483b2 : sub $0x74,%esp
>
> sp -= 116 // sp is 16-aligned
>
> Andrew.
>


-- 
- Pankaj

----------------------------------------------------------------------------------------------------------------------
There are only 10 kind of people in the world, those who can count in
binary, and those who cannot.



More information about the Gcc-help mailing list