1

I heard the x86 instruction push <value> does two things: increase the stack's size for the value's size, and put the value on the stack. I understand this to be identical to

subl $4, %esp
movl $1, (%esp)

Looking in some GCC compiled code to assembly, the compiler manually increased the stack and then moved the values to the stack, as

subl $12, %esp
movl $1, 8(%esp)
movl $2, 4(%esp)
movl $3, (%esp)

Which method is better for pushing to the stack? Manually increasing the stack's size (sub) and then moving it to the stack (mov)? Or is push better?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Schilive
  • 187
  • 7
  • 1
    Modern GCC's `-mtune=generic` defaults to using push/pop, unlike older GCC, for reasons explained in the linked duplicates. If you can reuse the space across multiple calls, there can be some advantage in total uops (at the cost of code size in bytes) to just storing new args with `mov` instead of popping the old args and pushing new ones, despite needing a stack-sync uop. Or needing to waste EBP as a frame pointer. – Peter Cordes May 31 '23 at 03:23

0 Answers0