namespace X86Disassembler.X86.Handlers.Or;
///
/// Handler for OR r/m8, r8 instruction (0x08)
///
public class OrRm8R8Handler : InstructionHandler
{
///
/// Initializes a new instance of the OrRm8R8Handler class
///
/// The buffer containing the code to decode
/// The instruction decoder that owns this handler
/// The length of the buffer
public OrRm8R8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
///
/// Checks if this handler can decode the given opcode
///
/// The opcode to check
/// True if this handler can decode the opcode
public override bool CanHandle(byte opcode)
{
return opcode == 0x08;
}
///
/// Decodes an OR r/m8, r8 instruction
///
/// The opcode of the instruction
/// The instruction object to populate
/// True if the instruction was successfully decoded
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "or";
// Read the ModR/M byte
int position = Decoder.GetPosition();
if (position >= Length)
{
instruction.Operands = "??";
return true;
}
byte modRM = CodeBuffer[position];
// Check if the next byte is a valid ModR/M byte or potentially another opcode
// For the specific case of 0x83, it's a different instruction (ADD r/m32, imm8)
if (modRM == 0x83)
{
// This is likely the start of another instruction, not a ModR/M byte
instruction.Operands = "??";
return true;
}
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// The register operand is in the reg field (8-bit register)
string regOperand = ModRMDecoder.GetRegisterName(reg, 8);
// Handle the r/m operand based on mod field
string rmOperand;
if (mod == 3) // Register-to-register
{
// Direct register addressing
rmOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
else // Memory addressing
{
// Replace "dword ptr" with "byte ptr" for 8-bit operands
rmOperand = destOperand.Replace("dword ptr", "byte ptr");
}
// Set the operands (r/m8, r8 format)
instruction.Operands = $"{rmOperand}, {regOperand}";
return true;
}
}