From bb695cf3bb30379f55b37b93fa68d32e6833cc4d Mon Sep 17 00:00:00 2001 From: bird_egop Date: Sat, 12 Apr 2025 23:03:07 +0300 Subject: [PATCH] move handlers, remove bases --- .../X86/Handlers/DataTransferHandler.cs | 334 ------------------ .../Handlers/Group1/AdcImmToRm32Handler.cs | 4 +- .../Group1/AdcImmToRm32SignExtendedHandler.cs | 4 +- .../Handlers/Group1/AddImmToRm32Handler.cs | 4 +- .../Group1/AddImmToRm32SignExtendedHandler.cs | 4 +- .../X86/Handlers/Group1/AddImmToRm8Handler.cs | 4 +- .../Handlers/Group1/AndImmWithRm32Handler.cs | 4 +- .../AndImmWithRm32SignExtendedHandler.cs | 4 +- .../Handlers/Group1/CmpImmWithRm32Handler.cs | 4 +- .../CmpImmWithRm32SignExtendedHandler.cs | 4 +- .../X86/Handlers/Group1/Group1BaseHandler.cs | 44 --- .../X86/Handlers/Group1/OrImmToRm32Handler.cs | 4 +- .../Group1/OrImmToRm32SignExtendedHandler.cs | 4 +- .../X86/Handlers/Group1/OrImmToRm8Handler.cs | 4 +- .../Handlers/Group1/SbbImmFromRm32Handler.cs | 4 +- .../SbbImmFromRm32SignExtendedHandler.cs | 4 +- .../Handlers/Group1/SubImmFromRm32Handler.cs | 4 +- .../SubImmFromRm32SignExtendedHandler.cs | 4 +- .../X86/Handlers/Group3/DivRm32Handler.cs | 4 +- .../X86/Handlers/Group3/Group3BaseHandler.cs | 44 --- .../X86/Handlers/Group3/IdivRm32Handler.cs | 4 +- .../X86/Handlers/Group3/ImulRm32Handler.cs | 4 +- .../X86/Handlers/Group3/MulRm32Handler.cs | 4 +- .../X86/Handlers/Group3/NegRm32Handler.cs | 4 +- .../X86/Handlers/Group3/NotRm32Handler.cs | 4 +- .../X86/Handlers/InstructionHandler.cs | 22 ++ .../Handlers/Test/TestImmWithRm32Handler.cs | 6 +- .../Handlers/Test/TestImmWithRm8Handler.cs | 6 +- .../X86/Handlers/Xor/XorImmWithRm32Handler.cs | 6 +- .../Xor/XorImmWithRm32SignExtendedHandler.cs | 6 +- 30 files changed, 74 insertions(+), 482 deletions(-) delete mode 100644 X86Disassembler/X86/Handlers/DataTransferHandler.cs delete mode 100644 X86Disassembler/X86/Handlers/Group1/Group1BaseHandler.cs delete mode 100644 X86Disassembler/X86/Handlers/Group3/Group3BaseHandler.cs diff --git a/X86Disassembler/X86/Handlers/DataTransferHandler.cs b/X86Disassembler/X86/Handlers/DataTransferHandler.cs deleted file mode 100644 index b62f610..0000000 --- a/X86Disassembler/X86/Handlers/DataTransferHandler.cs +++ /dev/null @@ -1,334 +0,0 @@ -namespace X86Disassembler.X86.Handlers; - -/// -/// Handler for data transfer instructions (MOV, PUSH, POP, etc.) -/// -public class DataTransferHandler : InstructionHandler -{ - /// - /// Initializes a new instance of the DataTransferHandler class - /// - /// The buffer containing the code to decode - /// The instruction decoder that owns this handler - /// The length of the buffer - public DataTransferHandler(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) - { - // MOV instructions - if ((opcode >= 0x88 && opcode <= 0x8B) || // MOV r/m, r and MOV r, r/m - (opcode >= 0xB0 && opcode <= 0xB7) || // MOV r8, imm8 - (opcode >= 0xB8 && opcode <= 0xBF) || // MOV r32, imm32 - opcode == 0xA0 || opcode == 0xA1 || // MOV AL/EAX, moffs - opcode == 0xA2 || opcode == 0xA3) // MOV moffs, AL/EAX - { - return true; - } - - // PUSH instructions - if ((opcode >= 0x50 && opcode <= 0x57) || // PUSH r32 - opcode == 0x68 || opcode == 0x6A) // PUSH imm32/imm8 - { - return true; - } - - // POP instructions - if (opcode >= 0x58 && opcode <= 0x5F) // POP r32 - { - return true; - } - - // XCHG instructions - if (opcode >= 0x90 && opcode <= 0x97) // XCHG EAX, r32 - { - return true; - } - - return false; - } - - /// - /// Decodes a data transfer 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 based on the opcode - instruction.Mnemonic = OpcodeMap.GetMnemonic(opcode); - - // Handle different types of data transfer instructions - if (opcode >= 0x88 && opcode <= 0x8B) // MOV r/m, r and MOV r, r/m - { - return DecodeMOVRegMem(opcode, instruction); - } - else if (opcode >= 0xB0 && opcode <= 0xB7) // MOV r8, imm8 - { - return DecodeMOVRegImm8(opcode, instruction); - } - else if (opcode >= 0xB8 && opcode <= 0xBF) // MOV r32, imm32 - { - return DecodeMOVRegImm32(opcode, instruction); - } - else if (opcode == 0xA0 || opcode == 0xA1) // MOV AL/EAX, moffs - { - return DecodeMOVAccMem(opcode, instruction); - } - else if (opcode == 0xA2 || opcode == 0xA3) // MOV moffs, AL/EAX - { - return DecodeMOVMemAcc(opcode, instruction); - } - else if (opcode >= 0x50 && opcode <= 0x57) // PUSH r32 - { - return DecodePUSHReg(opcode, instruction); - } - else if (opcode == 0x68) // PUSH imm32 - { - return DecodePUSHImm32(instruction); - } - else if (opcode == 0x6A) // PUSH imm8 - { - return DecodePUSHImm8(instruction); - } - else if (opcode >= 0x58 && opcode <= 0x5F) // POP r32 - { - return DecodePOPReg(opcode, instruction); - } - else if (opcode >= 0x90 && opcode <= 0x97) // XCHG EAX, r32 - { - return DecodeXCHGEAXReg(opcode, instruction); - } - - return false; - } - - /// - /// Decodes a MOV instruction with register and memory operands - /// - private bool DecodeMOVRegMem(byte opcode, Instruction instruction) - { - int position = Decoder.GetPosition(); - - if (position >= Length) - { - return false; - } - - // Read the ModR/M byte - var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM(); - - // Determine direction (0 = r/m to reg, 1 = reg to r/m) - bool direction = (opcode & 0x02) != 0; - - // Determine operand size (0 = 8-bit, 1 = 32-bit) - bool operandSize32 = (opcode & 0x01) != 0; - - // Get register name based on size - string regName = ModRMDecoder.GetRegisterName(reg, operandSize32 ? 32 : 8); - - // For mod == 3, both operands are registers - if (mod == 3) - { - string rmRegName = ModRMDecoder.GetRegisterName(rm, operandSize32 ? 32 : 8); - instruction.Operands = direction ? $"{rmRegName}, {regName}" : $"{regName}, {rmRegName}"; - } - else // Memory operand - { - instruction.Operands = direction ? $"{memOperand}, {regName}" : $"{regName}, {memOperand}"; - } - - return true; - } - - /// - /// Decodes a MOV instruction with 8-bit register and immediate operand - /// - private bool DecodeMOVRegImm8(byte opcode, Instruction instruction) - { - int position = Decoder.GetPosition(); - - if (position >= Length) - { - return false; - } - - // Register is encoded in the low 3 bits of the opcode - int reg = opcode & 0x07; - string regName = ModRMDecoder.GetRegisterName(reg, 8); - - // Read the immediate value - byte imm8 = CodeBuffer[position]; - Decoder.SetPosition(position + 1); - - instruction.Operands = $"{regName}, 0x{imm8:X2}"; - return true; - } - - /// - /// Decodes a MOV instruction with 32-bit register and immediate operand - /// - private bool DecodeMOVRegImm32(byte opcode, Instruction instruction) - { - int position = Decoder.GetPosition(); - - if (position + 4 > Length) - { - return false; - } - - // Register is encoded in the low 3 bits of the opcode - int reg = opcode & 0x07; - string regName = ModRMDecoder.GetRegisterName(reg, 32); - - // Read the immediate value - uint imm32 = BitConverter.ToUInt32(CodeBuffer, position); - Decoder.SetPosition(position + 4); - - instruction.Operands = $"{regName}, 0x{imm32:X8}"; - return true; - } - - /// - /// Decodes a MOV instruction with accumulator (AL/EAX) and memory operand - /// - private bool DecodeMOVAccMem(byte opcode, Instruction instruction) - { - int position = Decoder.GetPosition(); - - if (position + 4 > Length) - { - return false; - } - - // Determine operand size (0xA0 = 8-bit, 0xA1 = 32-bit) - bool operandSize32 = opcode == 0xA1; - string regName = operandSize32 ? "eax" : "al"; - - // Read the memory offset - uint offset = BitConverter.ToUInt32(CodeBuffer, position); - Decoder.SetPosition(position + 4); - - instruction.Operands = $"{regName}, [0x{offset:X8}]"; - return true; - } - - /// - /// Decodes a MOV instruction with memory operand and accumulator (AL/EAX) - /// - private bool DecodeMOVMemAcc(byte opcode, Instruction instruction) - { - int position = Decoder.GetPosition(); - - if (position + 4 > Length) - { - return false; - } - - // Determine operand size (0xA2 = 8-bit, 0xA3 = 32-bit) - bool operandSize32 = opcode == 0xA3; - string regName = operandSize32 ? "eax" : "al"; - - // Read the memory offset - uint offset = BitConverter.ToUInt32(CodeBuffer, position); - Decoder.SetPosition(position + 4); - - instruction.Operands = $"[0x{offset:X8}], {regName}"; - return true; - } - - /// - /// Decodes a PUSH instruction with register operand - /// - private bool DecodePUSHReg(byte opcode, Instruction instruction) - { - // Register is encoded in the low 3 bits of the opcode - int reg = opcode & 0x07; - string regName = ModRMDecoder.GetRegisterName(reg, 32); - - instruction.Operands = regName; - return true; - } - - /// - /// Decodes a PUSH instruction with 32-bit immediate operand - /// - private bool DecodePUSHImm32(Instruction instruction) - { - int position = Decoder.GetPosition(); - - if (position + 4 > Length) - { - return false; - } - - // Read the immediate value - uint imm32 = BitConverter.ToUInt32(CodeBuffer, position); - Decoder.SetPosition(position + 4); - - instruction.Operands = $"0x{imm32:X8}"; - return true; - } - - /// - /// Decodes a PUSH instruction with 8-bit immediate operand - /// - private bool DecodePUSHImm8(Instruction instruction) - { - int position = Decoder.GetPosition(); - - if (position >= Length) - { - return false; - } - - // Read the immediate value - byte imm8 = CodeBuffer[position]; - Decoder.SetPosition(position + 1); - - instruction.Operands = $"0x{imm8:X2}"; - return true; - } - - /// - /// Decodes a POP instruction with register operand - /// - private bool DecodePOPReg(byte opcode, Instruction instruction) - { - // Register is encoded in the low 3 bits of the opcode - int reg = opcode & 0x07; - string regName = ModRMDecoder.GetRegisterName(reg, 32); - - instruction.Operands = regName; - return true; - } - - /// - /// Decodes an XCHG instruction with EAX and register operands - /// - private bool DecodeXCHGEAXReg(byte opcode, Instruction instruction) - { - // Special case for NOP (XCHG EAX, EAX) - if (opcode == 0x90) - { - instruction.Mnemonic = "nop"; - instruction.Operands = ""; - return true; - } - - // Register is encoded in the low 3 bits of the opcode - int reg = opcode & 0x07; - string regName = ModRMDecoder.GetRegisterName(reg, 32); - - instruction.Operands = $"eax, {regName}"; - return true; - } -} diff --git a/X86Disassembler/X86/Handlers/Group1/AdcImmToRm32Handler.cs b/X86Disassembler/X86/Handlers/Group1/AdcImmToRm32Handler.cs index ce9a7f1..d79a229 100644 --- a/X86Disassembler/X86/Handlers/Group1/AdcImmToRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/AdcImmToRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for ADC r/m32, imm32 instruction (0x81 /2) /// -public class AdcImmToRm32Handler : Group1BaseHandler +public class AdcImmToRm32Handler : InstructionHandler { /// /// Initializes a new instance of the AdcImmToRm32Handler class @@ -65,7 +65,7 @@ public class AdcImmToRm32Handler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position + 3 >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/AdcImmToRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Group1/AdcImmToRm32SignExtendedHandler.cs index 5e4b318..f24bcb1 100644 --- a/X86Disassembler/X86/Handlers/Group1/AdcImmToRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Group1/AdcImmToRm32SignExtendedHandler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for ADC r/m32, imm8 (sign-extended) instruction (0x83 /2) /// -public class AdcImmToRm32SignExtendedHandler : Group1BaseHandler +public class AdcImmToRm32SignExtendedHandler : InstructionHandler { /// /// Initializes a new instance of the AdcImmToRm32SignExtendedHandler class @@ -65,7 +65,7 @@ public class AdcImmToRm32SignExtendedHandler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value (sign-extended from 8 to 32 bits) if (position >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/AddImmToRm32Handler.cs b/X86Disassembler/X86/Handlers/Group1/AddImmToRm32Handler.cs index aef61de..41f7be9 100644 --- a/X86Disassembler/X86/Handlers/Group1/AddImmToRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/AddImmToRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for ADD r/m32, imm32 instruction (0x81 /0) /// -public class AddImmToRm32Handler : Group1BaseHandler +public class AddImmToRm32Handler : InstructionHandler { /// /// Initializes a new instance of the AddImmToRm32Handler class @@ -65,7 +65,7 @@ public class AddImmToRm32Handler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position + 3 >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/AddImmToRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Group1/AddImmToRm32SignExtendedHandler.cs index d546f76..dc89f8c 100644 --- a/X86Disassembler/X86/Handlers/Group1/AddImmToRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Group1/AddImmToRm32SignExtendedHandler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for ADD r/m32, imm8 (sign-extended) instruction (0x83 /0) /// -public class AddImmToRm32SignExtendedHandler : Group1BaseHandler +public class AddImmToRm32SignExtendedHandler : InstructionHandler { /// /// Initializes a new instance of the AddImmToRm32SignExtendedHandler class @@ -65,7 +65,7 @@ public class AddImmToRm32SignExtendedHandler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/AddImmToRm8Handler.cs b/X86Disassembler/X86/Handlers/Group1/AddImmToRm8Handler.cs index fd22cd6..03f1e27 100644 --- a/X86Disassembler/X86/Handlers/Group1/AddImmToRm8Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/AddImmToRm8Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for ADD r/m8, imm8 instruction (0x80 /0) /// -public class AddImmToRm8Handler : Group1BaseHandler +public class AddImmToRm8Handler : InstructionHandler { /// /// Initializes a new instance of the AddImmToRm8Handler class @@ -73,7 +73,7 @@ public class AddImmToRm8Handler : Group1BaseHandler else { // Use ModR/M decoder for memory addressing - destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); } Decoder.SetPosition(position); diff --git a/X86Disassembler/X86/Handlers/Group1/AndImmWithRm32Handler.cs b/X86Disassembler/X86/Handlers/Group1/AndImmWithRm32Handler.cs index b58ef6d..175e3ba 100644 --- a/X86Disassembler/X86/Handlers/Group1/AndImmWithRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/AndImmWithRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for AND r/m32, imm32 instruction (0x81 /4) /// -public class AndImmWithRm32Handler : Group1BaseHandler +public class AndImmWithRm32Handler : InstructionHandler { /// /// Initializes a new instance of the AndImmWithRm32Handler class @@ -65,7 +65,7 @@ public class AndImmWithRm32Handler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position + 3 >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/AndImmWithRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Group1/AndImmWithRm32SignExtendedHandler.cs index f2c7c68..dd7ab1e 100644 --- a/X86Disassembler/X86/Handlers/Group1/AndImmWithRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Group1/AndImmWithRm32SignExtendedHandler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for AND r/m32, imm8 (sign-extended) instruction (0x83 /4) /// -public class AndImmWithRm32SignExtendedHandler : Group1BaseHandler +public class AndImmWithRm32SignExtendedHandler : InstructionHandler { /// /// Initializes a new instance of the AndImmWithRm32SignExtendedHandler class @@ -65,7 +65,7 @@ public class AndImmWithRm32SignExtendedHandler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value (sign-extended from 8 to 32 bits) if (position >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32Handler.cs b/X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32Handler.cs index 9e688ae..2423438 100644 --- a/X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for CMP r/m32, imm32 instruction (0x81 /7) /// -public class CmpImmWithRm32Handler : Group1BaseHandler +public class CmpImmWithRm32Handler : InstructionHandler { /// /// Initializes a new instance of the CmpImmWithRm32Handler class @@ -65,7 +65,7 @@ public class CmpImmWithRm32Handler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position + 3 >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32SignExtendedHandler.cs index b73d511..22134d6 100644 --- a/X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32SignExtendedHandler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for CMP r/m32, imm8 (sign-extended) instruction (0x83 /7) /// -public class CmpImmWithRm32SignExtendedHandler : Group1BaseHandler +public class CmpImmWithRm32SignExtendedHandler : InstructionHandler { /// /// Initializes a new instance of the CmpImmWithRm32SignExtendedHandler class @@ -65,7 +65,7 @@ public class CmpImmWithRm32SignExtendedHandler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/Group1BaseHandler.cs b/X86Disassembler/X86/Handlers/Group1/Group1BaseHandler.cs deleted file mode 100644 index 84dda24..0000000 --- a/X86Disassembler/X86/Handlers/Group1/Group1BaseHandler.cs +++ /dev/null @@ -1,44 +0,0 @@ -namespace X86Disassembler.X86.Handlers.Group1; - -/// -/// Base class for Group 1 instruction handlers (ADD, OR, ADC, SBB, AND, SUB, XOR, CMP) -/// -public abstract class Group1BaseHandler : InstructionHandler -{ - // ModR/M decoder - protected readonly ModRMDecoder _modRMDecoder; - - /// - /// Initializes a new instance of the Group1BaseHandler class - /// - /// The buffer containing the code to decode - /// The instruction decoder that owns this handler - /// The length of the buffer - protected Group1BaseHandler(byte[] codeBuffer, InstructionDecoder decoder, int length) - : base(codeBuffer, decoder, length) - { - _modRMDecoder = new ModRMDecoder(codeBuffer, decoder, length); - } - - /// - /// Gets the 32-bit register name for the given register index - /// - /// The register index - /// The register name - protected static string GetRegister32(byte reg) - { - string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" }; - return registerNames[reg & 0x07]; - } - - /// - /// Gets the 8-bit register name for the given register index - /// - /// The register index - /// The register name - protected static string GetRegister8(byte reg) - { - string[] registerNames = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" }; - return registerNames[reg & 0x07]; - } -} diff --git a/X86Disassembler/X86/Handlers/Group1/OrImmToRm32Handler.cs b/X86Disassembler/X86/Handlers/Group1/OrImmToRm32Handler.cs index 055d548..6682a0e 100644 --- a/X86Disassembler/X86/Handlers/Group1/OrImmToRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/OrImmToRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for OR r/m32, imm32 instruction (0x81 /1) /// -public class OrImmToRm32Handler : Group1BaseHandler +public class OrImmToRm32Handler : InstructionHandler { /// /// Initializes a new instance of the OrImmToRm32Handler class @@ -65,7 +65,7 @@ public class OrImmToRm32Handler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position + 3 >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/OrImmToRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Group1/OrImmToRm32SignExtendedHandler.cs index 7e9d204..964fd51 100644 --- a/X86Disassembler/X86/Handlers/Group1/OrImmToRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Group1/OrImmToRm32SignExtendedHandler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for OR r/m32, imm8 (sign-extended) instruction (0x83 /1) /// -public class OrImmToRm32SignExtendedHandler : Group1BaseHandler +public class OrImmToRm32SignExtendedHandler : InstructionHandler { /// /// Initializes a new instance of the OrImmToRm32SignExtendedHandler class @@ -65,7 +65,7 @@ public class OrImmToRm32SignExtendedHandler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value (sign-extended from 8 to 32 bits) if (position >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/OrImmToRm8Handler.cs b/X86Disassembler/X86/Handlers/Group1/OrImmToRm8Handler.cs index 36381c9..3b6378b 100644 --- a/X86Disassembler/X86/Handlers/Group1/OrImmToRm8Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/OrImmToRm8Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for OR r/m8, imm8 instruction (0x80 /1) /// -public class OrImmToRm8Handler : Group1BaseHandler +public class OrImmToRm8Handler : InstructionHandler { /// /// Initializes a new instance of the OrImmToRm8Handler class @@ -73,7 +73,7 @@ public class OrImmToRm8Handler : Group1BaseHandler else { // Use ModR/M decoder for memory addressing - destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); } Decoder.SetPosition(position); diff --git a/X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32Handler.cs b/X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32Handler.cs index c30f475..f8ef226 100644 --- a/X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for SBB r/m32, imm32 instruction (0x81 /3) /// -public class SbbImmFromRm32Handler : Group1BaseHandler +public class SbbImmFromRm32Handler : InstructionHandler { /// /// Initializes a new instance of the SbbImmFromRm32Handler class @@ -65,7 +65,7 @@ public class SbbImmFromRm32Handler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position + 3 >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32SignExtendedHandler.cs index 0dce041..4f41c44 100644 --- a/X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32SignExtendedHandler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for SBB r/m32, imm8 (sign-extended) instruction (0x83 /3) /// -public class SbbImmFromRm32SignExtendedHandler : Group1BaseHandler +public class SbbImmFromRm32SignExtendedHandler : InstructionHandler { /// /// Initializes a new instance of the SbbImmFromRm32SignExtendedHandler class @@ -65,7 +65,7 @@ public class SbbImmFromRm32SignExtendedHandler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value (sign-extended from 8 to 32 bits) if (position >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/SubImmFromRm32Handler.cs b/X86Disassembler/X86/Handlers/Group1/SubImmFromRm32Handler.cs index e696c53..b8ca06a 100644 --- a/X86Disassembler/X86/Handlers/Group1/SubImmFromRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group1/SubImmFromRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for SUB r/m32, imm32 instruction (0x81 /5) /// -public class SubImmFromRm32Handler : Group1BaseHandler +public class SubImmFromRm32Handler : InstructionHandler { /// /// Initializes a new instance of the SubImmFromRm32Handler class @@ -65,7 +65,7 @@ public class SubImmFromRm32Handler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position + 3 >= Length) diff --git a/X86Disassembler/X86/Handlers/Group1/SubImmFromRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Group1/SubImmFromRm32SignExtendedHandler.cs index 84ee45a..1e286a6 100644 --- a/X86Disassembler/X86/Handlers/Group1/SubImmFromRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Group1/SubImmFromRm32SignExtendedHandler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1; /// /// Handler for SUB r/m32, imm8 (sign-extended) instruction (0x83 /5) /// -public class SubImmFromRm32SignExtendedHandler : Group1BaseHandler +public class SubImmFromRm32SignExtendedHandler : InstructionHandler { /// /// Initializes a new instance of the SubImmFromRm32SignExtendedHandler class @@ -65,7 +65,7 @@ public class SubImmFromRm32SignExtendedHandler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position >= Length) diff --git a/X86Disassembler/X86/Handlers/Group3/DivRm32Handler.cs b/X86Disassembler/X86/Handlers/Group3/DivRm32Handler.cs index f3a340f..a8ec5fc 100644 --- a/X86Disassembler/X86/Handlers/Group3/DivRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group3/DivRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3; /// /// Handler for DIV r/m32 instruction (0xF7 /6) /// -public class DivRm32Handler : Group3BaseHandler +public class DivRm32Handler : InstructionHandler { /// /// Initializes a new instance of the DivRm32Handler class @@ -65,7 +65,7 @@ public class DivRm32Handler : Group3BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the operand - string operand = _modRMDecoder.DecodeModRM(mod, rm, false); + string operand = ModRMDecoder.DecodeModRM(mod, rm, false); // Set the operands instruction.Operands = operand; diff --git a/X86Disassembler/X86/Handlers/Group3/Group3BaseHandler.cs b/X86Disassembler/X86/Handlers/Group3/Group3BaseHandler.cs deleted file mode 100644 index bb80d72..0000000 --- a/X86Disassembler/X86/Handlers/Group3/Group3BaseHandler.cs +++ /dev/null @@ -1,44 +0,0 @@ -namespace X86Disassembler.X86.Handlers.Group3; - -/// -/// Base class for Group 3 instruction handlers (TEST, NOT, NEG, MUL, IMUL, DIV, IDIV) -/// -public abstract class Group3BaseHandler : InstructionHandler -{ - // ModR/M decoder - protected readonly ModRMDecoder _modRMDecoder; - - /// - /// Initializes a new instance of the Group3BaseHandler class - /// - /// The buffer containing the code to decode - /// The instruction decoder that owns this handler - /// The length of the buffer - protected Group3BaseHandler(byte[] codeBuffer, InstructionDecoder decoder, int length) - : base(codeBuffer, decoder, length) - { - _modRMDecoder = new ModRMDecoder(codeBuffer, decoder, length); - } - - /// - /// Gets the 32-bit register name for the given register index - /// - /// The register index - /// The register name - protected static string GetRegister32(byte reg) - { - string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" }; - return registerNames[reg & 0x07]; - } - - /// - /// Gets the 8-bit register name for the given register index - /// - /// The register index - /// The register name - protected static string GetRegister8(byte reg) - { - string[] registerNames = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" }; - return registerNames[reg & 0x07]; - } -} diff --git a/X86Disassembler/X86/Handlers/Group3/IdivRm32Handler.cs b/X86Disassembler/X86/Handlers/Group3/IdivRm32Handler.cs index b28a3ae..5b2cfce 100644 --- a/X86Disassembler/X86/Handlers/Group3/IdivRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group3/IdivRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3; /// /// Handler for IDIV r/m32 instruction (0xF7 /7) /// -public class IdivRm32Handler : Group3BaseHandler +public class IdivRm32Handler : InstructionHandler { /// /// Initializes a new instance of the IdivRm32Handler class @@ -65,7 +65,7 @@ public class IdivRm32Handler : Group3BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the operand - string operand = _modRMDecoder.DecodeModRM(mod, rm, false); + string operand = ModRMDecoder.DecodeModRM(mod, rm, false); // Set the operands instruction.Operands = operand; diff --git a/X86Disassembler/X86/Handlers/Group3/ImulRm32Handler.cs b/X86Disassembler/X86/Handlers/Group3/ImulRm32Handler.cs index 5eb508c..71a3828 100644 --- a/X86Disassembler/X86/Handlers/Group3/ImulRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group3/ImulRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3; /// /// Handler for IMUL r/m32 instruction (0xF7 /5) /// -public class ImulRm32Handler : Group3BaseHandler +public class ImulRm32Handler : InstructionHandler { /// /// Initializes a new instance of the ImulRm32Handler class @@ -65,7 +65,7 @@ public class ImulRm32Handler : Group3BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the operand - string operand = _modRMDecoder.DecodeModRM(mod, rm, false); + string operand = ModRMDecoder.DecodeModRM(mod, rm, false); // Set the operands instruction.Operands = operand; diff --git a/X86Disassembler/X86/Handlers/Group3/MulRm32Handler.cs b/X86Disassembler/X86/Handlers/Group3/MulRm32Handler.cs index a4cf90c..36c0d52 100644 --- a/X86Disassembler/X86/Handlers/Group3/MulRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group3/MulRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3; /// /// Handler for MUL r/m32 instruction (0xF7 /4) /// -public class MulRm32Handler : Group3BaseHandler +public class MulRm32Handler : InstructionHandler { /// /// Initializes a new instance of the MulRm32Handler class @@ -65,7 +65,7 @@ public class MulRm32Handler : Group3BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the operand - string operand = _modRMDecoder.DecodeModRM(mod, rm, false); + string operand = ModRMDecoder.DecodeModRM(mod, rm, false); // Set the operands instruction.Operands = operand; diff --git a/X86Disassembler/X86/Handlers/Group3/NegRm32Handler.cs b/X86Disassembler/X86/Handlers/Group3/NegRm32Handler.cs index caefa3c..5418668 100644 --- a/X86Disassembler/X86/Handlers/Group3/NegRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group3/NegRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3; /// /// Handler for NEG r/m32 instruction (0xF7 /3) /// -public class NegRm32Handler : Group3BaseHandler +public class NegRm32Handler : InstructionHandler { /// /// Initializes a new instance of the NegRm32Handler class @@ -65,7 +65,7 @@ public class NegRm32Handler : Group3BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the operand - string operand = _modRMDecoder.DecodeModRM(mod, rm, false); + string operand = ModRMDecoder.DecodeModRM(mod, rm, false); // Set the operands instruction.Operands = operand; diff --git a/X86Disassembler/X86/Handlers/Group3/NotRm32Handler.cs b/X86Disassembler/X86/Handlers/Group3/NotRm32Handler.cs index 15ae48c..f97afd0 100644 --- a/X86Disassembler/X86/Handlers/Group3/NotRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Group3/NotRm32Handler.cs @@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3; /// /// Handler for NOT r/m32 instruction (0xF7 /2) /// -public class NotRm32Handler : Group3BaseHandler +public class NotRm32Handler : InstructionHandler { /// /// Initializes a new instance of the NotRm32Handler class @@ -81,7 +81,7 @@ public class NotRm32Handler : Group3BaseHandler else { // Use the ModR/M decoder for memory addressing - operand = _modRMDecoder.DecodeModRM(mod, rm, false); + operand = ModRMDecoder.DecodeModRM(mod, rm, false); } // Set the operands diff --git a/X86Disassembler/X86/Handlers/InstructionHandler.cs b/X86Disassembler/X86/Handlers/InstructionHandler.cs index b79b8ab..b1b5bfe 100644 --- a/X86Disassembler/X86/Handlers/InstructionHandler.cs +++ b/X86Disassembler/X86/Handlers/InstructionHandler.cs @@ -45,4 +45,26 @@ public abstract class InstructionHandler : IInstructionHandler /// The instruction object to populate /// True if the instruction was successfully decoded public abstract bool Decode(byte opcode, Instruction instruction); + + /// + /// Gets the 32-bit register name for the given register index + /// + /// The register index + /// The register name + protected static string GetRegister32(byte reg) + { + string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" }; + return registerNames[reg & 0x07]; + } + + /// + /// Gets the 8-bit register name for the given register index + /// + /// The register index + /// The register name + protected static string GetRegister8(byte reg) + { + string[] registerNames = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" }; + return registerNames[reg & 0x07]; + } } diff --git a/X86Disassembler/X86/Handlers/Test/TestImmWithRm32Handler.cs b/X86Disassembler/X86/Handlers/Test/TestImmWithRm32Handler.cs index 3368ac3..906b8fc 100644 --- a/X86Disassembler/X86/Handlers/Test/TestImmWithRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Test/TestImmWithRm32Handler.cs @@ -1,11 +1,9 @@ -using X86Disassembler.X86.Handlers.Group3; - namespace X86Disassembler.X86.Handlers.Test; /// /// Handler for TEST r/m32, imm32 instruction (0xF7 /0) /// -public class TestImmWithRm32Handler : Group3BaseHandler +public class TestImmWithRm32Handler : InstructionHandler { /// /// Initializes a new instance of the TestImmWithRm32Handler class @@ -75,7 +73,7 @@ public class TestImmWithRm32Handler : Group3BaseHandler else { // Use the ModR/M decoder for memory addressing - destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); } // Read the immediate value diff --git a/X86Disassembler/X86/Handlers/Test/TestImmWithRm8Handler.cs b/X86Disassembler/X86/Handlers/Test/TestImmWithRm8Handler.cs index 20f65e7..9cbdc6a 100644 --- a/X86Disassembler/X86/Handlers/Test/TestImmWithRm8Handler.cs +++ b/X86Disassembler/X86/Handlers/Test/TestImmWithRm8Handler.cs @@ -1,11 +1,9 @@ -using X86Disassembler.X86.Handlers.Group3; - namespace X86Disassembler.X86.Handlers.Test; /// /// Handler for TEST r/m8, imm8 instruction (0xF6 /0) /// -public class TestImmWithRm8Handler : Group3BaseHandler +public class TestImmWithRm8Handler : InstructionHandler { /// /// Initializes a new instance of the TestImmWithRm8Handler class @@ -75,7 +73,7 @@ public class TestImmWithRm8Handler : Group3BaseHandler else { // Use the ModR/M decoder for memory addressing - destOperand = _modRMDecoder.DecodeModRM(mod, rm, true); + destOperand = ModRMDecoder.DecodeModRM(mod, rm, true); } // Read the immediate value diff --git a/X86Disassembler/X86/Handlers/Xor/XorImmWithRm32Handler.cs b/X86Disassembler/X86/Handlers/Xor/XorImmWithRm32Handler.cs index e1bfb7a..ba07024 100644 --- a/X86Disassembler/X86/Handlers/Xor/XorImmWithRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Xor/XorImmWithRm32Handler.cs @@ -1,11 +1,9 @@ -using X86Disassembler.X86.Handlers.Group1; - namespace X86Disassembler.X86.Handlers.Xor; /// /// Handler for XOR r/m32, imm32 instruction (0x81 /6) /// -public class XorImmWithRm32Handler : Group1BaseHandler +public class XorImmWithRm32Handler : InstructionHandler { /// /// Initializes a new instance of the XorImmWithRm32Handler class @@ -67,7 +65,7 @@ public class XorImmWithRm32Handler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value if (position + 3 >= Length) diff --git a/X86Disassembler/X86/Handlers/Xor/XorImmWithRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Xor/XorImmWithRm32SignExtendedHandler.cs index 7addad3..b1671b3 100644 --- a/X86Disassembler/X86/Handlers/Xor/XorImmWithRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Xor/XorImmWithRm32SignExtendedHandler.cs @@ -1,11 +1,9 @@ -using X86Disassembler.X86.Handlers.Group1; - namespace X86Disassembler.X86.Handlers.Xor; /// /// Handler for XOR r/m32, imm8 (sign-extended) instruction (0x83 /6) /// -public class XorImmWithRm32SignExtendedHandler : Group1BaseHandler +public class XorImmWithRm32SignExtendedHandler : InstructionHandler { /// /// Initializes a new instance of the XorImmWithRm32SignExtendedHandler class @@ -67,7 +65,7 @@ public class XorImmWithRm32SignExtendedHandler : Group1BaseHandler byte rm = (byte)(modRM & 0x07); // Decode the destination operand - string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); + string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false); // Read the immediate value (sign-extended from 8 to 32 bits) if (position >= Length)