Search code examples
assemblyx86nasmmachine-codeopcode

Difference between MOV r/m8,r8 and MOV r8,r/m8


By looking at intel volume of instructions, I found this:

1) 88 /r MOV r/m8,r8
2) 8A /r MOV r8,r/m8

When I write a line like this in NASM, and assemble it with the listing option:

mov al, bl

I get this in the listing:

88D8 mov al, bl

So obviously NASM chosed the first instruction of the two above, but isn't the second instruction an option two? if so, on what basis did NASM chosed the first?


Solution

  • These two encodings exist because a modr/m byte can only encode one memory operand. So to allow both mov r8,m8 and mov m8,r8, two encodings are needed. Of course, this way we can encode uses of mov with both operands being registers using either encoding and nasm just picks one at random. There is no special reason for the choice and I have seen assemblers make different choices.

    I have also heard about an assembler that watermarked binaries it assembles by choosing an instruction encoding in a specific way. This way the author of the assembler could track down and sue people who used his assembler without paying.