0

I am a student studying computer systems for the first time (w/ Computer Systems: A Programmer's Perspective). We are working on assembly and I am starting to understand command suffixes for x86_64 such as using leaq in something like:

leaq (%rsp, %rdx), %rax

However, I am failing to understand using a suffix for pop. For example, using the same logic, it would make sense to me that we'd use popl for something like:

popl %edi

But, in the text and other examples online, I just see:

pop %edi

What is the difference? is popl even valid? Just looking for a little more insight. Anything helps, thank you.

scottbaker
  • 153
  • 1
  • 6
  • 2
    It's not valid. Consult the instruction set reference to see the allowed forms. The suffix can be omitted if the size can be deduced from the arguments. `lea (%rsp, %rdx), %rax` (without the suffix) is valid and is the same instruction. Also `popq %rdi` and `pop %rdi` is valid. `pop %edi` is not valid at all in 64 bit code, suffix or no suffix. – Jester Sep 16 '18 at 17:07

1 Answers1

2

What you can do in asm is limited by what the hardware can do. Implicit vs. explicit operand-size in the source (suffix or not) doesn't change the machine code it will assemble to.

So the right question to ask is whether the hardware can do a 32-bit push in 64-bit mode? No, it can't, therefore no asm source syntax exists that will get it to do exactly what you were trying to do with one instruction.

That's why your assembler won't accept pop %edi or popl %edi. Those are exactly equivalent because the 32-bit register implies DWORD (l) operand-size. The examples you saw of popl or pop %edi are for 32-bit mode, where EDI is the full register instead of the low half of RDI, and that instruction is encodable.

You only need a size suffix when it's ambiguous, mov $1, (%rdi). Your assembler will give an error for that instead of guessing one of b/w/l/q.

But push is a bit special: push $1 will default to a 64-bit push, even though pushw $1 is possible. How many bytes does the push instruction push onto the stack when I don't specify the operand size?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • you might want to explain that in 32 and 16 bit modes, push r32 is of course possible. – fuz Sep 17 '18 at 08:35