0

It seems that, while a 32-bit program can push/pop either 2 or 4 bytes to the stack, a 64-bit program has no choice but to push/pop 8 bytes at a time. Is there really no way to push/pop a 4-byte value in 64-bit mode? If not, is there a reason why the architecture disallows this?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user1636349
  • 458
  • 1
  • 4
  • 21
  • 1
    In 64-bit mode, you can push/pop 2 bytes, but there's no reason you would ever want to. It's just a result of the way instruction encoding works. You cannot push/pop 4 bytes in 64-bit mode. – prl Oct 06 '21 at 18:58
  • OK. I thought I might be able to emulate this using "subl 4,%rsp; movl %eax,,(%rsp)" instead of "pushl %eax", but this gives me a segfault. Still trying to figure out why. – user1636349 Oct 06 '21 at 19:17
  • 1
    Probably because later code expects the stack to be aligned? (Which may be why there's no instruction to do it.) – prl Oct 06 '21 at 19:21
  • @prl: I think the encoding reason is that there's never a way for a REX.W=0 prefix to reduce the operand-size from 64 to 32, for opcodes that default to 32. (Including push/pop, but also call, ret, and also non-stack ops like indirect `jmp eax`, or also direct `jmp rel32`). – Peter Cordes Oct 06 '21 at 20:21
  • @Peter, yes, that's right; what I meant was that if there were any value in having 32-bit push, they would have figured out a way to encode it. (For example, they could have required rex.w for all 64-bit pushes. That would not have been an improvement.) – prl Oct 07 '21 at 00:55
  • And yet you can push/pop 2 bytes at a time, so why not 4? Unless the 2 byte push takes up 8 bytes on the stack... – user1636349 Oct 07 '21 at 08:29

0 Answers0