using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Lea;
///
/// Handler for LEA r32, m instruction (0x8D)
///
public class LeaR32MHandler : InstructionHandler
{
///
/// Initializes a new instance of the LeaR32MHandler class
///
/// The instruction decoder that owns this handler
public LeaR32MHandler(InstructionDecoder decoder)
: base(decoder)
{
}
///
/// 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)
{
// Only handle opcode 0x8D when the operand size prefix is NOT present
// This ensures 16-bit handlers get priority when the prefix is present
return opcode == 0x8D && !Decoder.HasOperandSizePrefix();
}
///
/// Decodes a LEA r32, m 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)
{
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, _, sourceOperand) = ModRMDecoder.ReadModRM();
// LEA only works with memory operands, not registers
if (mod == 3)
{
return false;
}
// Set the instruction type
instruction.Type = InstructionType.Lea;
// Create the destination register operand
var destinationOperand = OperandFactory.CreateRegisterOperand((RegisterIndex)reg, 32);
// For LEA, we don't care about the size of the memory operand
// as we're only interested in the effective address calculation
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}
}