namespace X86Disassembler.X86.Handlers.Sub; /// /// Handler for SUB r/m8, imm8 instruction (0x80 /5) /// public class SubImmFromRm8Handler : InstructionHandler { /// /// Initializes a new instance of the SubImmFromRm8Handler class /// /// The buffer containing the code to decode /// The instruction decoder that owns this handler /// The length of the buffer public SubImmFromRm8Handler(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) { if (opcode != 0x80) return false; // Check if the reg field of the ModR/M byte is 5 (SUB) int position = Decoder.GetPosition(); if (position >= Length) return false; byte modRM = CodeBuffer[position]; byte reg = (byte) ((modRM & 0x38) >> 3); return reg == 5; // 5 = SUB } /// /// Decodes a SUB r/m8, imm8 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 = "sub"; // Extract the fields from the ModR/M byte var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); // Read the immediate byte var position = Decoder.GetPosition(); if (position >= Length) { return false; } byte imm8 = Decoder.ReadByte(); // Set the instruction information // For mod == 3, the operand is a register if (mod == 3) { string rmRegName = ModRMDecoder.GetRegisterName(rm, 8); instruction.Operands = $"{rmRegName}, 0x{imm8:X2}"; } else // Memory operand { // Get the memory operand string instruction.Operands = $"byte ptr {destOperand}, 0x{imm8:X2}"; } return true; } }