Finally starting to get the hang of Volume 2 of the Intel docs. However, one piece is confusing me currently. Looking at the ADD
instruction, there are these two different opcodes which have the same operand signature:
opcode call description
80 /0 ib ADD r/m8, imm8 Add imm8 to r/m8
REX + 80 /0 ib ADD r/m8*, imm8 Add sign-extended imm8 to r/m64
I understand the first one, but not the second one. If we did this:
add cl, 1
We get:
80 c1 01
The first one is like this:
80 /0 ib
That makes sense.
The second one, with the REX
prefix, would look like this:
0b01000000 80 /0 ib
...from my understanding, with byte value 01000000
(0100
being the magic first 4 bits of REX). 01000000
is 40
in hex.
I would expect to pass in an r8
as per the instruction call signature, such as ah
, like this:
add ah, 1
And get out:
40 80 /0 01
Where /0
you lookup in the table for the register, which would be c4
for AH
, so you'd have:
40 80 c4 01
But I don't, in this case NASM gives me:
80 c4 01
Why doesn't it have the REX
prefix???
I see there is the asterisk *
in ADD r/m8*, imm8
, which says:
* In 64-bit mode, r/m8 can not be encoded to access the following byte registers if a REX prefix is used: AH, BH, CH, DH.
So that tells me I can't use AH
, I think. But if I try some other 8-bit register (AX, BX, CX, DX, SP, BP, SI, DI, CS, SS, ES, DS, IP), like:
add sp, 1
I get:
66 83 c4 01
Which corresponds to:
<prefix> 83 /0 ib
I don't have any idea yet why they added that 66
prefix, but that is probably a separate question. If you know, feel free to answer that too.
Trying other registers gives me similar results.
What am I doing wrong. How do I generate something that invokes the rule REX + 80 /0 ib
?
question from:
https://stackoverflow.com/questions/65947932/what-does-add-r-m8-imm8-mean-in-the-intel-x86-docs-how-do-i-invoke-the-rex