What's the difference in encoding (ModRM:r/m, ModRM:reg) vs (ModRM:reg, ModRM:r/m)? Specifically say an instruction like CMPXCHG vs DIVPD. I thought the register and address was always encoded in the first byte and then the SIB and displacement in the second byte if needed? Here's my code:
编码(ModRM:r/m, ModRM:reg)和(ModRM:reg, ModRM:r/m)的区别是什么?具体地说,像CMPXCHG和DIVPD这样的指令。我以为寄存器和地址总是用第一个字节进行编码,然后用第二个字节进行SIB和位移,如果需要的话?这是我的代码:
static void WriteRegisterToMemory(ICollection<Byte> bytes, IRegisterToMemoryInstruction instruction, Byte rex)
{
IAddress address = instruction.Address;
Byte register = instruction.Register;
if (address.NeedsRex)
{
rex |= 0x40;
if (address.RexB)
rex |= 1;
if (address.RexX)
rex |= 1 << 1;
}
if (register > 7)
rex |= 0x44; // REX.R
if (rex != 0)
bytes.Add(rex);
bytes.AddRange(instruction.Opcode);
Byte modRM = (Byte)((register % 8) << 3);
modRM |= address.GetModRMAddressByte();
bytes.Add(modRM);
address.WriteScaledIndexByteAndDisplacement(bytes);
}
So like these two instructions are encoded exactly the same with just different opcodes? (ADDs on page 457 of the intel x64 manual)
就像这两条指令用不同的操作码编码一样?(增加英特尔x64手册第457页)
Op/En Operand 1 Operand 2
RM ModRM:reg (r, w) ModRM:r/m (r)
MR ModRM:r/m (r, w) ModRM:reg (r)
1 个解决方案
#1
1
There isn't really any difference w.r.t. encoding, the difference is in which one is the source and which one is the destination. Most instructions have the r/m
as source, except things like cmpxchg
, bts
, xadd
, xchg
is ambiguous about it (it's symmetric), ALU ops have an r/m, r
form and an r/m, imm
form, and obviously mov's to memory. So in encoding those instructions (even if both operands are registers), be careful "which way around" they are, or they might end up with their operands swapped. But that's all, there is in the end no difference in how they are encoded.
wr编码没有什么区别,区别在于哪一个是源,哪一个是目标。大多数指令都有r/m作为源,除了cmpxchg、bts、xadd、xchg是不明确的(它是对称的),ALU ops有r/m、r/m、imm形式,显然mov的是内存。因此,在编码这些指令(即使两个操作数都是寄存器)时,要注意它们的“方向”,否则它们最终可能会交换它们的操作数。但这就是全部,它们的编码方式最终没有差别。
#1
1
There isn't really any difference w.r.t. encoding, the difference is in which one is the source and which one is the destination. Most instructions have the r/m
as source, except things like cmpxchg
, bts
, xadd
, xchg
is ambiguous about it (it's symmetric), ALU ops have an r/m, r
form and an r/m, imm
form, and obviously mov's to memory. So in encoding those instructions (even if both operands are registers), be careful "which way around" they are, or they might end up with their operands swapped. But that's all, there is in the end no difference in how they are encoded.
wr编码没有什么区别,区别在于哪一个是源,哪一个是目标。大多数指令都有r/m作为源,除了cmpxchg、bts、xadd、xchg是不明确的(它是对称的),ALU ops有r/m、r/m、imm形式,显然mov的是内存。因此,在编码这些指令(即使两个操作数都是寄存器)时,要注意它们的“方向”,否则它们最终可能会交换它们的操作数。但这就是全部,它们的编码方式最终没有差别。