using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Neg;
///
/// Handler for NEG r/m8 instruction (0xF6 /3)
///
public class NegRm8Handler : InstructionHandler
{
///
/// Initializes a new instance of the NegRm8Handler class
///
/// The instruction decoder that owns this handler
public NegRm8Handler(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)
{
if (opcode != 0xF6)
return false;
// Check if the reg field of the ModR/M byte is 3 (NEG)
if (!Decoder.CanReadByte())
return false;
var reg = ModRMDecoder.PeakModRMReg();
return reg == 3; // 3 = NEG
}
///
/// Decodes a NEG r/m8 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.Neg;
if (!Decoder.CanReadByte())
{
return false;
}
var (_, _, _, operand) = ModRMDecoder.ReadModRM8();
// Set the structured operands
// NEG has only one operand
instruction.StructuredOperands =
[
operand
];
return true;
}
}