1

I was looking how to encode the mov instruction when both operands are registers. Looking at the manual, the encoding uses the, 1-bit, 'D' field. It changes whether the source or the destination is specified by the 'REG' field. But, as both operands are registers, I can just change the order in the encoding between the 'R/M' field and 'REG' field according to the value of the 'D' field, making:

mov ax, ax = 1000100111000000 = 1000101111000000

mov ax, bx = 1000100111001000 = 1000101111000001

I confirmed the equivalence using an online x86-16 disassembler. I did not, though, execute both on my computer to test if they are indeed equivalent.

So. Is there any actual difference between the two ways of encoding? Is it just a matter of preference/style? And, if within scope and possible, is there a preferred way?

  • Yes, they are equivalent. – ecm Jun 28 '23 at 05:24
  • @emc, thank you for answering/commenting. Apparently, the x86-16 encoding is very complex and inefficient for the programmer. But it may be a trade. – Gabriel Silva Schilive Jun 28 '23 at 05:30
  • Both directions are supported in order to allow the R/M operand to be either the source or the destination. For two-register operation this causes such a redundancy. A86 is well known for having a specific pattern of encoding choices in redundant cases, which was intended to find unregistered users of that assembler. – ecm Jun 28 '23 at 08:05
  • 1
    there are 2 ways to encode reg-reg operations: [Encoding ADC EAX, ECX - 2 different ways to encode? (arch x86)](https://stackoverflow.com/q/22217436/995714), [Why does nasm use 0x89 when it assembles a MOV instruction between registers?](https://stackoverflow.com/q/19467610/995714) – phuclv Jun 28 '23 at 10:39

0 Answers0