2

I need some help understanding why GCC is doing

main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp   # ???
    subl    $48, %esp    # ???
    movl    $8, 16(%esp)        
    movl    $4, 20(%esp)

Why does it first subtract 16 and then subtract 48 again? Wouldn't it be easier to do subl $64, %esp?

miho
  • 11,765
  • 7
  • 42
  • 85

1 Answers1

6
andl    $-16, %esp   # ???

The above line is not subtracting 16 from esp but to align it to 16 byte boundary.
While the following one is to subtract, mostly for reserving some space on the stack.

subl    $48, %esp    # ???
starrify
  • 14,307
  • 5
  • 33
  • 50
  • It's `AND` not `ADD`, oh my god. Now I got it. Thanks! – miho Apr 26 '14 at 11:06
  • Why would it align it to 16 bytes? It's 32bit, not 64, the abi does not mandate that, I think that you can avoid that by setting -mpreferred-stack-boundary=2 (that would align the stack to 4 bytes unless a bigger alignment is needed) – Marco Apr 26 '14 at 11:31
  • 1
    @Marco "Why would it align it to 16 bytes?" That's because the default value of `-mpreferred-stack-boundary` is 4. Nothing to do with the architecture, also as you've pointed out, the ABI didn't specified about it. I think it's set for optimizations with SIMD instruction families. – starrify Apr 26 '14 at 12:37
  • what does this mean "align it to 16 byte boundary", I looked up mpreferred stack boundary and it looks like it's to set the frame size, but I don't think thats it since it then subl's 16 – Steve's a D Apr 01 '17 at 19:30
  • @SteveG `what does this mean "align it to 16 byte boundary"`: http://stackoverflow.com/questions/10224564/what-does-alignment-to-16-byte-boundary-mean-in-x86 `since it then subl's 16`: I don't know what you mean by `subl's 16` – starrify Apr 03 '17 at 06:31