namespace X86Disassembler.X86.Handlers.Or; /// /// Handler for OR r/m8, r8 instruction (0x08) /// public class OrRm8R8Handler : InstructionHandler { // 8-bit register names private static readonly string[] RegisterNames8 = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" }; /// /// Initializes a new instance of the OrRm8R8Handler class /// /// The buffer containing the code to decode /// The instruction decoder that owns this handler /// The length of the buffer public OrRm8R8Handler(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) { return opcode == 0x08; } /// /// Decodes an OR r/m8, r8 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 = "or"; // Read the ModR/M byte int position = Decoder.GetPosition(); if (position >= Length) { instruction.Operands = "??"; return true; } byte modRM = CodeBuffer[position++]; Decoder.SetPosition(position); // Extract fields from ModR/M byte byte mod = (byte)((modRM & 0xC0) >> 6); // Top 2 bits byte reg = (byte)((modRM & 0x38) >> 3); // Middle 3 bits byte rm = (byte)(modRM & 0x07); // Bottom 3 bits // The register operand is in the reg field (8-bit register) string regOperand = RegisterNames8[reg]; // Handle the r/m operand based on mod field string rmOperand; if (mod == 3) // Register-to-register { // Direct register addressing rmOperand = RegisterNames8[rm]; } else // Memory addressing { // Use ModRMDecoder for memory addressing, but we need to adjust for 8-bit operands var modRMDecoder = new ModRMDecoder(CodeBuffer, Decoder, Length); string memOperand = modRMDecoder.DecodeModRM(mod, rm, false); // false = not 64-bit // Replace "dword ptr" with "byte ptr" for 8-bit operands rmOperand = memOperand.Replace("dword ptr", "byte ptr"); } // Set the operands (r/m8, r8 format) instruction.Operands = $"{rmOperand}, {regOperand}"; return true; } }