3
votes

Encoding the x86_64 instruction mov rcx,rdx (using https://defuse.ca/online-x86-assembler.htm) outputs 48 89 D1.

Checking the op-code with this reference shows how that byte sequence encodes the instruction.

However two rows down in that table (op-code 8B) is a very similar mov instruction, but with the order of the operands flipped.
In fact I'm able to encode the same instruction using 48 8b ca (verified by decompiling).

Why do both op-codes exist? Do they differ by more than I was able to work out? When would one be picked over the other?

1
They exist to allow for a memory operand to be either source or destination. If both are registers, you get two encodings. Which is picked depends on assembler, some even allow you to specify (e.g. the .s suffix for gas).Jester

1 Answers

4
votes

A modr/m byte can only encode up to one memory operand. All instructions that support memory operands in either source or destination are thus encoded twice, once with source being possibly a memory operand and once with destination possibly being a memory operand. Of course this means that you can encode mnemonics where both operands are registers twice.