2025-04-12 19:18:52 +03:00
|
|
|
namespace X86Disassembler.X86;
|
|
|
|
|
2025-04-15 02:42:47 +03:00
|
|
|
using Operands;
|
2025-04-14 22:08:50 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Handles decoding of ModR/M bytes in x86 instructions
|
|
|
|
/// </summary>
|
|
|
|
public class ModRMDecoder
|
|
|
|
{
|
|
|
|
// The instruction decoder that owns this ModRM decoder
|
|
|
|
private readonly InstructionDecoder _decoder;
|
2025-04-16 19:11:36 +03:00
|
|
|
|
|
|
|
// The SIB decoder for handling SIB bytes
|
|
|
|
private readonly SIBDecoder _sibDecoder;
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the ModRMDecoder class
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="decoder">The instruction decoder that owns this ModRM decoder</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
public ModRMDecoder(InstructionDecoder decoder)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
|
|
|
_decoder = decoder;
|
2025-04-16 19:11:36 +03:00
|
|
|
_sibDecoder = new SIBDecoder(decoder);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-15 00:14:28 +03:00
|
|
|
|
2025-04-16 19:11:36 +03:00
|
|
|
// These methods have been moved to the RegisterMapper class
|
2025-04-15 02:29:32 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
/// <summary>
|
2025-04-14 22:08:50 +03:00
|
|
|
/// Decodes a ModR/M byte to get the operand
|
2025-04-12 19:18:52 +03:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="mod">The mod field (2 bits)</param>
|
2025-04-13 23:06:52 +03:00
|
|
|
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
|
2025-04-12 19:18:52 +03:00
|
|
|
/// <param name="is64Bit">True if the operand is 64-bit</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
/// <returns>The operand object</returns>
|
|
|
|
public Operand DecodeModRM(byte mod, RegisterIndex rmIndex, bool is64Bit)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-16 18:30:17 +03:00
|
|
|
return DecodeModRMInternal(mod, rmIndex, is64Bit ? 64 : 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Decodes a ModR/M byte to get an 8-bit operand
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mod">The mod field (2 bits)</param>
|
|
|
|
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
|
|
|
|
/// <returns>The 8-bit operand object</returns>
|
|
|
|
public Operand DecodeModRM8(byte mod, RegisterIndex rmIndex)
|
|
|
|
{
|
|
|
|
return DecodeModRMInternal(mod, rmIndex, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Internal implementation for decoding a ModR/M byte to get an operand with specific size
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mod">The mod field (2 bits)</param>
|
|
|
|
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
|
|
|
|
/// <param name="operandSize">The size of the operand in bits (8, 16, 32, or 64)</param>
|
|
|
|
/// <returns>The operand object</returns>
|
|
|
|
private Operand DecodeModRMInternal(byte mod, RegisterIndex rmIndex, int operandSize)
|
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
switch (mod)
|
|
|
|
{
|
|
|
|
case 0: // [reg] or disp32
|
2025-04-13 23:06:52 +03:00
|
|
|
// Special case: [EBP] is encoded as disp32 with no base register
|
2025-04-15 02:29:32 +03:00
|
|
|
if (rmIndex == RegisterIndex.Bp) // disp32 (was EBP/BP)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
if (_decoder.CanReadUInt())
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
uint disp32 = _decoder.ReadUInt32();
|
2025-04-14 22:08:50 +03:00
|
|
|
return OperandFactory.CreateDirectMemoryOperand(disp32, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Fallback for incomplete data
|
|
|
|
return OperandFactory.CreateDirectMemoryOperand(0, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-13 23:06:52 +03:00
|
|
|
// Special case: [ESP] is encoded with SIB byte
|
2025-04-15 00:14:28 +03:00
|
|
|
if (rmIndex == RegisterIndex.Sp) // SIB (was ESP/SP)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
|
|
|
// Handle SIB byte
|
2025-04-14 01:15:26 +03:00
|
|
|
if (_decoder.CanReadByte())
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
byte sib = _decoder.ReadByte();
|
2025-04-16 18:30:17 +03:00
|
|
|
return DecodeSIB(sib, 0, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Fallback for incomplete data
|
2025-04-15 00:14:28 +03:00
|
|
|
return OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Sp, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
|
|
|
// Regular case: [reg]
|
2025-04-14 22:08:50 +03:00
|
|
|
return OperandFactory.CreateBaseRegisterMemoryOperand(rmIndex, operandSize);
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
case 1: // [reg + disp8]
|
2025-04-15 00:14:28 +03:00
|
|
|
if (rmIndex == RegisterIndex.Sp) // SIB + disp8 (ESP/SP)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
|
|
|
// Handle SIB byte
|
2025-04-14 01:15:26 +03:00
|
|
|
if (_decoder.CanReadByte())
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
byte sib = _decoder.ReadByte();
|
2025-04-14 22:08:50 +03:00
|
|
|
sbyte disp8 = (sbyte)(_decoder.CanReadByte() ? _decoder.ReadByte() : 0);
|
2025-04-16 18:30:17 +03:00
|
|
|
return DecodeSIB(sib, (uint)disp8, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Fallback for incomplete data
|
2025-04-15 00:14:28 +03:00
|
|
|
return OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Sp, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
if (_decoder.CanReadByte())
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
sbyte disp8 = (sbyte)_decoder.ReadByte();
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-15 00:14:28 +03:00
|
|
|
// For EBP (BP), always create a displacement memory operand, even if displacement is 0
|
|
|
|
// This is because [EBP] with no displacement is encoded as [EBP+0]
|
|
|
|
if (disp8 == 0 && rmIndex != RegisterIndex.Bp)
|
2025-04-13 23:06:52 +03:00
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
return OperandFactory.CreateBaseRegisterMemoryOperand(rmIndex, operandSize);
|
2025-04-13 23:06:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
return OperandFactory.CreateDisplacementMemoryOperand(rmIndex, disp8, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Fallback for incomplete data
|
|
|
|
return OperandFactory.CreateBaseRegisterMemoryOperand(rmIndex, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
case 2: // [reg + disp32]
|
2025-04-15 00:14:28 +03:00
|
|
|
if (rmIndex == RegisterIndex.Sp) // SIB + disp32 (ESP/SP)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
|
|
|
// Handle SIB byte
|
2025-04-14 01:15:26 +03:00
|
|
|
if (_decoder.CanReadUInt())
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
byte sib = _decoder.ReadByte();
|
|
|
|
uint disp32 = _decoder.ReadUInt32();
|
2025-04-16 18:30:17 +03:00
|
|
|
return DecodeSIB(sib, disp32, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Fallback for incomplete data
|
2025-04-15 00:14:28 +03:00
|
|
|
return OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Sp, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
if (_decoder.CanReadUInt())
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
uint disp32 = _decoder.ReadUInt32();
|
|
|
|
|
2025-04-15 00:14:28 +03:00
|
|
|
// For EBP (BP), always create a displacement memory operand, even if displacement is 0
|
|
|
|
// This is because [EBP] with no displacement is encoded as [EBP+disp]
|
|
|
|
if (rmIndex == RegisterIndex.Bp)
|
|
|
|
{
|
|
|
|
return OperandFactory.CreateDisplacementMemoryOperand(rmIndex, (int)disp32, operandSize);
|
|
|
|
}
|
|
|
|
|
2025-04-13 23:06:52 +03:00
|
|
|
// Only show displacement if it's not zero
|
|
|
|
if (disp32 == 0)
|
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
return OperandFactory.CreateBaseRegisterMemoryOperand(rmIndex, operandSize);
|
2025-04-13 23:06:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
return OperandFactory.CreateDisplacementMemoryOperand(rmIndex, (int)disp32, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Fallback for incomplete data
|
|
|
|
return OperandFactory.CreateBaseRegisterMemoryOperand(rmIndex, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-13 23:06:52 +03:00
|
|
|
case 3: // reg (direct register access)
|
2025-04-14 22:08:50 +03:00
|
|
|
return OperandFactory.CreateRegisterOperand(rmIndex, operandSize);
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
default:
|
2025-04-14 22:08:50 +03:00
|
|
|
// Fallback for invalid mod value
|
|
|
|
return OperandFactory.CreateRegisterOperand(RegisterIndex.A, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-15 00:14:28 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Peaks a ModR/M byte and returns the raw field values, without advancing position
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A tuple containing the raw mod, reg, and rm fields from the ModR/M byte</returns>
|
2025-04-15 02:42:47 +03:00
|
|
|
public byte PeakModRMReg()
|
2025-04-15 00:14:28 +03:00
|
|
|
{
|
|
|
|
if (!_decoder.CanReadByte())
|
|
|
|
{
|
2025-04-15 02:42:47 +03:00
|
|
|
return 0;
|
2025-04-15 00:14:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
byte modRM = _decoder.PeakByte();
|
|
|
|
|
|
|
|
// Extract fields from ModR/M byte
|
2025-04-16 19:07:32 +03:00
|
|
|
byte regIndex = (byte)((modRM & Constants.REG_MASK) >> 3); // Middle 3 bits (bits 3-5)
|
2025-04-15 00:14:28 +03:00
|
|
|
|
2025-04-15 02:42:47 +03:00
|
|
|
return regIndex;
|
2025-04-15 00:14:28 +03:00
|
|
|
}
|
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
/// <summary>
|
2025-04-15 02:29:32 +03:00
|
|
|
/// Reads and decodes a ModR/M byte for standard 32-bit operands
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
|
|
|
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM()
|
|
|
|
{
|
2025-04-16 01:10:33 +03:00
|
|
|
return ReadModRMInternal(false);
|
2025-04-15 02:29:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Reads and decodes a ModR/M byte for 64-bit operands
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
|
|
|
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM64()
|
|
|
|
{
|
2025-04-16 01:10:33 +03:00
|
|
|
return ReadModRMInternal(true);
|
2025-04-15 02:29:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Reads and decodes a ModR/M byte for 8-bit operands
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
2025-04-16 01:10:33 +03:00
|
|
|
public (byte mod, RegisterIndex8 reg, RegisterIndex8 rm, Operand operand) ReadModRM8()
|
2025-04-15 02:29:32 +03:00
|
|
|
{
|
2025-04-16 01:10:33 +03:00
|
|
|
return ReadModRM8Internal();
|
2025-04-15 02:29:32 +03:00
|
|
|
}
|
|
|
|
|
2025-04-16 18:30:17 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Reads and decodes a ModR/M byte for 16-bit operands
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
|
|
|
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM16()
|
|
|
|
{
|
|
|
|
var (mod, reg, rm, operand) = ReadModRMInternal(false);
|
|
|
|
|
|
|
|
// Create a new operand with 16-bit size using the appropriate factory method
|
|
|
|
if (operand is RegisterOperand registerOperand)
|
|
|
|
{
|
|
|
|
// For register operands, create a new 16-bit register operand
|
|
|
|
operand = OperandFactory.CreateRegisterOperand(registerOperand.Register, 16);
|
|
|
|
}
|
|
|
|
else if (operand is MemoryOperand)
|
|
|
|
{
|
|
|
|
// For memory operands, create a new 16-bit memory operand with the same properties
|
|
|
|
// This depends on the specific type of memory operand
|
|
|
|
if (operand is DirectMemoryOperand directMemory)
|
|
|
|
{
|
|
|
|
operand = OperandFactory.CreateDirectMemoryOperand16(directMemory.Address);
|
|
|
|
}
|
|
|
|
else if (operand is BaseRegisterMemoryOperand baseRegMemory)
|
|
|
|
{
|
|
|
|
operand = OperandFactory.CreateBaseRegisterMemoryOperand16(baseRegMemory.BaseRegister);
|
|
|
|
}
|
|
|
|
else if (operand is DisplacementMemoryOperand dispMemory)
|
|
|
|
{
|
|
|
|
operand = OperandFactory.CreateDisplacementMemoryOperand16(dispMemory.BaseRegister, dispMemory.Displacement);
|
|
|
|
}
|
|
|
|
else if (operand is ScaledIndexMemoryOperand scaledMemory)
|
|
|
|
{
|
|
|
|
operand = OperandFactory.CreateScaledIndexMemoryOperand16(scaledMemory.IndexRegister, scaledMemory.Scale, scaledMemory.BaseRegister, scaledMemory.Displacement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (mod, reg, rm, operand);
|
|
|
|
}
|
|
|
|
|
2025-04-15 02:29:32 +03:00
|
|
|
/// <summary>
|
2025-04-16 01:10:33 +03:00
|
|
|
/// Internal implementation for reading and decoding a ModR/M byte for standard 32-bit or 64-bit operands
|
2025-04-12 19:18:52 +03:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="is64Bit">True if the operand is 64-bit</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
2025-04-16 01:10:33 +03:00
|
|
|
private (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRMInternal(bool is64Bit)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-14 01:15:26 +03:00
|
|
|
if (!_decoder.CanReadByte())
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-16 01:10:33 +03:00
|
|
|
return (0, RegisterIndex.A, RegisterIndex.A, OperandFactory.CreateRegisterOperand(RegisterIndex.A, is64Bit ? 64 : 32));
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
|
|
|
byte modRM = _decoder.ReadByte();
|
|
|
|
|
2025-04-13 23:06:52 +03:00
|
|
|
// Extract fields from ModR/M byte
|
2025-04-16 19:07:32 +03:00
|
|
|
byte mod = (byte)((modRM & Constants.MOD_MASK) >> 6);
|
|
|
|
byte regIndex = (byte)((modRM & Constants.REG_MASK) >> 3);
|
|
|
|
byte rmIndex = (byte)(modRM & Constants.RM_MASK);
|
2025-04-15 00:14:28 +03:00
|
|
|
|
|
|
|
// Map the ModR/M register indices to RegisterIndex enum values
|
2025-04-16 19:11:36 +03:00
|
|
|
RegisterIndex reg = RegisterMapper.MapModRMToRegisterIndex(regIndex);
|
|
|
|
RegisterIndex rm = RegisterMapper.MapModRMToRegisterIndex(rmIndex);
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-15 02:29:32 +03:00
|
|
|
// Create the operand based on the mod and rm fields
|
2025-04-14 22:08:50 +03:00
|
|
|
Operand operand = DecodeModRM(mod, rm, is64Bit);
|
2025-04-16 01:10:33 +03:00
|
|
|
|
|
|
|
return (mod, reg, rm, operand);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Internal implementation for reading and decoding a ModR/M byte for 8-bit operands
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
|
|
|
private (byte mod, RegisterIndex8 reg, RegisterIndex8 rm, Operand operand) ReadModRM8Internal()
|
|
|
|
{
|
|
|
|
if (!_decoder.CanReadByte())
|
|
|
|
{
|
|
|
|
return (0, RegisterIndex8.AL, RegisterIndex8.AL, OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL));
|
|
|
|
}
|
|
|
|
|
|
|
|
byte modRM = _decoder.ReadByte();
|
|
|
|
|
|
|
|
// Extract fields from ModR/M byte
|
2025-04-16 19:07:32 +03:00
|
|
|
byte mod = (byte)((modRM & Constants.MOD_MASK) >> 6);
|
|
|
|
byte regIndex = (byte)((modRM & Constants.REG_MASK) >> 3);
|
|
|
|
byte rmIndex = (byte)(modRM & Constants.RM_MASK);
|
2025-04-16 01:10:33 +03:00
|
|
|
|
|
|
|
// Map the ModR/M register indices to RegisterIndex8 enum values
|
2025-04-16 19:11:36 +03:00
|
|
|
RegisterIndex8 reg = RegisterMapper.MapModRMToRegisterIndex8(regIndex);
|
|
|
|
RegisterIndex8 rm = RegisterMapper.MapModRMToRegisterIndex8(rmIndex);
|
2025-04-16 01:10:33 +03:00
|
|
|
|
|
|
|
// Create the operand based on the mod and rm fields
|
|
|
|
Operand operand;
|
2025-04-15 02:29:32 +03:00
|
|
|
|
2025-04-16 01:10:33 +03:00
|
|
|
if (mod == 3) // Register operand
|
2025-04-15 02:29:32 +03:00
|
|
|
{
|
2025-04-16 01:10:33 +03:00
|
|
|
// For register operands, create an 8-bit register operand
|
|
|
|
operand = OperandFactory.CreateRegisterOperand8(rm);
|
|
|
|
}
|
|
|
|
else // Memory operand
|
|
|
|
{
|
|
|
|
// For memory operands, we need to map the RegisterIndex8 to RegisterIndex for base registers
|
2025-04-16 18:42:15 +03:00
|
|
|
// The rmIndex is the raw value from the ModR/M byte, not the mapped RegisterIndex8
|
|
|
|
// This is important because we need to check if it's 4 (ESP) for SIB byte
|
2025-04-16 19:11:36 +03:00
|
|
|
RegisterIndex rmRegIndex = RegisterMapper.MapModRMToRegisterIndex(rmIndex);
|
2025-04-16 18:30:17 +03:00
|
|
|
|
|
|
|
// Use the DecodeModRM8 method to get an 8-bit memory operand
|
|
|
|
operand = DecodeModRM8(mod, rmRegIndex);
|
2025-04-15 02:29:32 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
return (mod, reg, rm, operand);
|
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Decodes a SIB byte
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sib">The SIB byte</param>
|
|
|
|
/// <param name="displacement">The displacement value</param>
|
2025-04-16 18:30:17 +03:00
|
|
|
/// <param name="operandSize">The size of the operand in bits (8, 16, 32, or 64)</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
/// <returns>The decoded SIB operand</returns>
|
2025-04-16 18:30:17 +03:00
|
|
|
private Operand DecodeSIB(byte sib, uint displacement, int operandSize)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-16 19:11:36 +03:00
|
|
|
// Delegate to the SIBDecoder
|
|
|
|
return _sibDecoder.DecodeSIB(sib, displacement, operandSize);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-13 23:06:52 +03:00
|
|
|
|
2025-04-12 19:18:52 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the register name based on the register index and size
|
|
|
|
/// </summary>
|
2025-04-13 23:06:52 +03:00
|
|
|
/// <param name="regIndex">The register index as RegisterIndex enum</param>
|
2025-04-16 01:10:33 +03:00
|
|
|
/// <param name="size">The register size (16 or 32 bits)</param>
|
2025-04-12 19:18:52 +03:00
|
|
|
/// <returns>The register name</returns>
|
2025-04-13 23:06:52 +03:00
|
|
|
public static string GetRegisterName(RegisterIndex regIndex, int size)
|
2025-04-12 19:18:52 +03:00
|
|
|
{
|
2025-04-16 19:11:36 +03:00
|
|
|
return RegisterMapper.GetRegisterName(regIndex, size);
|
2025-04-12 19:18:52 +03:00
|
|
|
}
|
2025-04-16 01:10:33 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the 8-bit register name based on the RegisterIndex8 enum value
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="regIndex8">The register index as RegisterIndex8 enum</param>
|
|
|
|
/// <returns>The 8-bit register name</returns>
|
|
|
|
public static string GetRegisterName(RegisterIndex8 regIndex8)
|
|
|
|
{
|
2025-04-16 19:11:36 +03:00
|
|
|
return RegisterMapper.GetRegisterName(regIndex8);
|
2025-04-16 01:10:33 +03:00
|
|
|
}
|
2025-04-14 01:15:26 +03:00
|
|
|
}
|