using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
///
/// Handler for AND EAX, imm32 instruction (0x25)
///
public class AndEaxImmHandler : InstructionHandler
{
///
/// Initializes a new instance of the AndEaxImmHandler class
///
/// The instruction decoder that owns this handler
public AndEaxImmHandler(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)
{
// AND EAX, imm32 is encoded as 0x25 without 0x66 prefix
if (opcode != 0x25)
{
return false;
}
// Only handle when the operand size prefix is NOT present
return !Decoder.HasOperandSizePrefix();
}
///
/// Decodes an AND EAX, imm32 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 instruction type
instruction.Type = InstructionType.And;
// Create the destination register operand (EAX)
var destinationOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32);
// Read immediate value
if (!Decoder.CanReadUInt())
{
return false;
}
// Read immediate value
uint imm32 = Decoder.ReadUInt32();
// Create the source immediate operand
var sourceOperand = OperandFactory.CreateImmediateOperand(imm32, 32);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}
}