diff --git a/X86Disassembler/X86/Handlers/ArithmeticUnary/NegRm8Handler.cs b/X86Disassembler/X86/Handlers/ArithmeticUnary/NegRm8Handler.cs new file mode 100644 index 0000000..3e5c3c0 --- /dev/null +++ b/X86Disassembler/X86/Handlers/ArithmeticUnary/NegRm8Handler.cs @@ -0,0 +1,68 @@ +using X86Disassembler.X86.Operands; + +namespace X86Disassembler.X86.Handlers.ArithmeticUnary; + +/// +/// 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; + } + + // Read the ModR/M byte + // For NEG r/m8 (0xF6 /3): + // - The r/m field with mod specifies the operand (register or memory) + var (_, reg, _, operand) = ModRMDecoder.ReadModRM8(); + + // Set the structured operands + // NEG has only one operand + instruction.StructuredOperands = + [ + operand + ]; + + return true; + } +} diff --git a/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs b/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs index 9d69a35..a2c0378 100644 --- a/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs +++ b/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs @@ -90,8 +90,9 @@ public class InstructionHandlerFactory // NOT handler _handlers.Add(new NotRm32Handler(_decoder)); - // NEG handler - _handlers.Add(new NegRm32Handler(_decoder)); + // NEG handlers + _handlers.Add(new NegRm8Handler(_decoder)); // F6 /3 - NEG r/m8 + _handlers.Add(new NegRm32Handler(_decoder)); // F7 /3 - NEG r/m32 // MUL handler _handlers.Add(new MulRm32Handler(_decoder));