0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 08:18:36 +03:00

Refactored instruction decoder into smaller, more maintainable components using handler pattern

This commit is contained in:
bird_egop
2025-04-12 19:18:52 +03:00
parent 2e6e133159
commit dffc405c10
8 changed files with 1361 additions and 544 deletions

View File

@ -0,0 +1,281 @@
namespace X86Disassembler.X86.Handlers;
/// <summary>
/// Handler for control flow instructions (JMP, CALL, RET, etc.)
/// </summary>
public class ControlFlowHandler : InstructionHandler
{
// Condition codes for conditional jumps
private static readonly string[] ConditionCodes = {
"o", "no", "b", "ae", "e", "ne", "be", "a",
"s", "ns", "p", "np", "l", "ge", "le", "g"
};
/// <summary>
/// Initializes a new instance of the ControlFlowHandler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public ControlFlowHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// RET instruction
if (opcode == 0xC3 || opcode == 0xC2)
{
return true;
}
// CALL instruction
if (opcode == 0xE8)
{
return true;
}
// JMP instructions
if (opcode == 0xE9 || opcode == 0xEB)
{
return true;
}
// Conditional jumps
if (opcode >= 0x70 && opcode <= 0x7F)
{
return true;
}
// INT instructions
if (opcode == 0xCC || opcode == 0xCD)
{
return true;
}
// JECXZ instruction
if (opcode == 0xE3)
{
return true;
}
return false;
}
/// <summary>
/// Decodes a control flow instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic based on the opcode
instruction.Mnemonic = OpcodeMap.GetMnemonic(opcode);
// Handle different types of control flow instructions
if (opcode == 0xC3) // RET
{
// No operands for RET
instruction.Operands = string.Empty;
return true;
}
else if (opcode == 0xC2) // RET imm16
{
return DecodeRETImm16(instruction);
}
else if (opcode == 0xE8) // CALL rel32
{
return DecodeCALLRel32(instruction);
}
else if (opcode == 0xE9) // JMP rel32
{
return DecodeJMPRel32(instruction);
}
else if (opcode == 0xEB) // JMP rel8
{
return DecodeJMPRel8(instruction);
}
else if (opcode >= 0x70 && opcode <= 0x7F) // Conditional jumps
{
return DecodeConditionalJump(opcode, instruction);
}
else if (opcode == 0xCC) // INT3
{
// No operands for INT3
instruction.Operands = string.Empty;
return true;
}
else if (opcode == 0xCD) // INT imm8
{
return DecodeINTImm8(instruction);
}
else if (opcode == 0xE3) // JECXZ rel8
{
return DecodeJECXZRel8(instruction);
}
return false;
}
/// <summary>
/// Decodes a RET instruction with 16-bit immediate operand
/// </summary>
private bool DecodeRETImm16(Instruction instruction)
{
int position = Decoder.GetPosition();
if (position + 2 > Length)
{
return false;
}
// Read the immediate value
ushort imm16 = BitConverter.ToUInt16(CodeBuffer, position);
Decoder.SetPosition(position + 2);
instruction.Operands = $"0x{imm16:X4}";
return true;
}
/// <summary>
/// Decodes a CALL instruction with 32-bit relative offset
/// </summary>
private bool DecodeCALLRel32(Instruction instruction)
{
int position = Decoder.GetPosition();
if (position + 4 > Length)
{
return false;
}
// Read the relative offset
int offset = BitConverter.ToInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Calculate the target address (relative to the next instruction)
uint targetAddress = (uint)(position + offset);
instruction.Operands = $"0x{targetAddress:X8}";
return true;
}
/// <summary>
/// Decodes a JMP instruction with 32-bit relative offset
/// </summary>
private bool DecodeJMPRel32(Instruction instruction)
{
int position = Decoder.GetPosition();
if (position + 4 > Length)
{
return false;
}
// Read the relative offset
int offset = BitConverter.ToInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Calculate the target address (relative to the next instruction)
uint targetAddress = (uint)(position + offset);
instruction.Operands = $"0x{targetAddress:X8}";
return true;
}
/// <summary>
/// Decodes a JMP instruction with 8-bit relative offset
/// </summary>
private bool DecodeJMPRel8(Instruction instruction)
{
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the relative offset
sbyte offset = (sbyte)CodeBuffer[position];
Decoder.SetPosition(position + 1);
// Calculate the target address (relative to the next instruction)
uint targetAddress = (uint)(position + offset + 1); // +1 because the offset is relative to the next instruction
instruction.Operands = $"0x{targetAddress:X8}";
return true;
}
/// <summary>
/// Decodes a conditional jump instruction
/// </summary>
private bool DecodeConditionalJump(byte opcode, Instruction instruction)
{
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the relative offset
sbyte offset = (sbyte)CodeBuffer[position];
Decoder.SetPosition(position + 1);
// Calculate the target address (relative to the next instruction)
uint targetAddress = (uint)(position + offset + 1); // +1 because the offset is relative to the next instruction
instruction.Operands = $"0x{targetAddress:X8}";
return true;
}
/// <summary>
/// Decodes an INT instruction with 8-bit immediate operand
/// </summary>
private bool DecodeINTImm8(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;
}
/// <summary>
/// Decodes a JECXZ instruction with 8-bit relative offset
/// </summary>
private bool DecodeJECXZRel8(Instruction instruction)
{
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the relative offset
sbyte offset = (sbyte)CodeBuffer[position];
Decoder.SetPosition(position + 1);
// Calculate the target address (relative to the next instruction)
uint targetAddress = (uint)(position + offset + 1); // +1 because the offset is relative to the next instruction
instruction.Operands = $"0x{targetAddress:X8}";
return true;
}
}

View File

@ -0,0 +1,326 @@
namespace X86Disassembler.X86.Handlers;
/// <summary>
/// Handler for data transfer instructions (MOV, PUSH, POP, etc.)
/// </summary>
public class DataTransferHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the DataTransferHandler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public DataTransferHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
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;
}
/// <summary>
/// Decodes a data transfer instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
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;
}
/// <summary>
/// Decodes a MOV instruction with register and memory operands
/// </summary>
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;
}
/// <summary>
/// Decodes a MOV instruction with 8-bit register and immediate operand
/// </summary>
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;
}
/// <summary>
/// Decodes a MOV instruction with 32-bit register and immediate operand
/// </summary>
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;
}
/// <summary>
/// Decodes a MOV instruction with accumulator (AL/EAX) and memory operand
/// </summary>
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;
}
/// <summary>
/// Decodes a MOV instruction with memory operand and accumulator (AL/EAX)
/// </summary>
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;
}
/// <summary>
/// Decodes a PUSH instruction with register operand
/// </summary>
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;
}
/// <summary>
/// Decodes a PUSH instruction with 32-bit immediate operand
/// </summary>
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;
}
/// <summary>
/// Decodes a PUSH instruction with 8-bit immediate operand
/// </summary>
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;
}
/// <summary>
/// Decodes a POP instruction with register operand
/// </summary>
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;
}
/// <summary>
/// Decodes an XCHG instruction with EAX and register operands
/// </summary>
private bool DecodeXCHGEAXReg(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 = $"eax, {regName}";
return true;
}
}

View File

@ -0,0 +1,167 @@
namespace X86Disassembler.X86.Handlers;
/// <summary>
/// Handler for floating-point instructions (D8-DF opcodes)
/// </summary>
public class FloatingPointHandler : InstructionHandler
{
// Floating-point instruction mnemonics based on opcode and ModR/M reg field
private static readonly string[][] FpuMnemonics = new string[8][];
/// <summary>
/// Static constructor to initialize the FPU mnemonic tables
/// </summary>
static FloatingPointHandler()
{
InitializeFpuMnemonics();
}
/// <summary>
/// Initializes the FPU mnemonic tables
/// </summary>
private static void InitializeFpuMnemonics()
{
// Initialize all tables
for (int i = 0; i < 8; i++)
{
FpuMnemonics[i] = new string[8];
for (int j = 0; j < 8; j++)
{
FpuMnemonics[i][j] = "??";
}
}
// D8 opcode - operations on float32
FpuMnemonics[0][0] = "fadd";
FpuMnemonics[0][1] = "fmul";
FpuMnemonics[0][2] = "fcom";
FpuMnemonics[0][3] = "fcomp";
FpuMnemonics[0][4] = "fsub";
FpuMnemonics[0][5] = "fsubr";
FpuMnemonics[0][6] = "fdiv";
FpuMnemonics[0][7] = "fdivr";
// D9 opcode - load, store, and control operations
FpuMnemonics[1][0] = "fld";
FpuMnemonics[1][2] = "fst";
FpuMnemonics[1][3] = "fstp";
FpuMnemonics[1][4] = "fldenv";
FpuMnemonics[1][5] = "fldcw";
FpuMnemonics[1][6] = "fnstenv";
FpuMnemonics[1][7] = "fnstcw";
// DA opcode - operations on int32
FpuMnemonics[2][0] = "fiadd";
FpuMnemonics[2][1] = "fimul";
FpuMnemonics[2][2] = "ficom";
FpuMnemonics[2][3] = "ficomp";
FpuMnemonics[2][4] = "fisub";
FpuMnemonics[2][5] = "fisubr";
FpuMnemonics[2][6] = "fidiv";
FpuMnemonics[2][7] = "fidivr";
// DB opcode - load/store int32, misc
FpuMnemonics[3][0] = "fild";
FpuMnemonics[3][2] = "fist";
FpuMnemonics[3][3] = "fistp";
FpuMnemonics[3][5] = "fld";
FpuMnemonics[3][7] = "fstp";
// DC opcode - operations on float64
FpuMnemonics[4][0] = "fadd";
FpuMnemonics[4][1] = "fmul";
FpuMnemonics[4][2] = "fcom";
FpuMnemonics[4][3] = "fcomp";
FpuMnemonics[4][4] = "fsub";
FpuMnemonics[4][5] = "fsubr";
FpuMnemonics[4][6] = "fdiv";
FpuMnemonics[4][7] = "fdivr";
// DD opcode - load/store float64
FpuMnemonics[5][0] = "fld";
FpuMnemonics[5][2] = "fst";
FpuMnemonics[5][3] = "fstp";
FpuMnemonics[5][4] = "frstor";
FpuMnemonics[5][6] = "fnsave";
FpuMnemonics[5][7] = "fnstsw";
// DE opcode - operations on int16
FpuMnemonics[6][0] = "fiadd";
FpuMnemonics[6][1] = "fimul";
FpuMnemonics[6][2] = "ficom";
FpuMnemonics[6][3] = "ficomp";
FpuMnemonics[6][4] = "fisub";
FpuMnemonics[6][5] = "fisubr";
FpuMnemonics[6][6] = "fidiv";
FpuMnemonics[6][7] = "fidivr";
// DF opcode - load/store int16, misc
FpuMnemonics[7][0] = "fild";
FpuMnemonics[7][2] = "fist";
FpuMnemonics[7][3] = "fistp";
FpuMnemonics[7][4] = "fbld";
FpuMnemonics[7][5] = "fild";
FpuMnemonics[7][6] = "fbstp";
FpuMnemonics[7][7] = "fistp";
}
/// <summary>
/// Initializes a new instance of the FloatingPointHandler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public FloatingPointHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
return opcode >= 0xD8 && opcode <= 0xDF;
}
/// <summary>
/// Decodes a floating-point instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// The opcode index in our tables (0-7 for D8-DF)
int opcodeIndex = opcode - 0xD8;
// Read the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM(opcodeIndex == 7); // DF uses 64-bit operands
// Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = FpuMnemonics[opcodeIndex][reg];
// For memory operands, set the operand
if (mod != 3) // Memory operand
{
instruction.Operands = operand;
}
else // Register operand (ST(i))
{
// For register operands, we need to handle the stack registers
// This is a simplified implementation and may need to be expanded
instruction.Operands = $"st({rm})";
}
return true;
}
}

View File

@ -0,0 +1,104 @@
namespace X86Disassembler.X86.Handlers;
/// <summary>
/// Handler for Group 1 instructions (ADD, OR, ADC, SBB, AND, SUB, XOR, CMP)
/// </summary>
public class Group1Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the Group1Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public Group1Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
return opcode == 0x80 || opcode == 0x81 || opcode == 0x83;
}
/// <summary>
/// Decodes a Group 1 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Determine the operation based on reg field
instruction.Mnemonic = OpcodeMap.Group1Operations[reg];
// Read the immediate value based on opcode
string immOperand;
position = Decoder.GetPosition();
switch (opcode)
{
case 0x80: // 8-bit immediate
if (position < Length)
{
byte imm8 = CodeBuffer[position];
Decoder.SetPosition(position + 1);
immOperand = $"0x{imm8:X2}";
}
else
{
immOperand = "???";
}
break;
case 0x81: // 32-bit immediate
if (position + 4 <= Length)
{
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
immOperand = $"0x{imm32:X8}";
}
else
{
immOperand = "???";
}
break;
case 0x83: // 8-bit sign-extended immediate
if (position < Length)
{
sbyte imm8 = (sbyte)CodeBuffer[position];
Decoder.SetPosition(position + 1);
immOperand = $"0x{imm8:X2}";
}
else
{
immOperand = "???";
}
break;
default:
return false;
}
// Set the operands
instruction.Operands = $"{destOperand}, {immOperand}";
return true;
}
}

View File

@ -0,0 +1,48 @@
namespace X86Disassembler.X86.Handlers;
/// <summary>
/// Base class for all instruction handlers
/// </summary>
public abstract class InstructionHandler
{
// Buffer containing the code to decode
protected readonly byte[] CodeBuffer;
// The instruction decoder that owns this handler
protected readonly InstructionDecoder Decoder;
// Length of the buffer
protected readonly int Length;
// ModRM decoder for handling addressing modes
protected readonly ModRMDecoder ModRMDecoder;
/// <summary>
/// Initializes a new instance of the InstructionHandler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
protected InstructionHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
{
CodeBuffer = codeBuffer;
Decoder = decoder;
Length = length;
ModRMDecoder = new ModRMDecoder(codeBuffer, decoder, length);
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public abstract bool CanHandle(byte opcode);
/// <summary>
/// Decodes an instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public abstract bool Decode(byte opcode, Instruction instruction);
}