namespace X86Disassembler.X86.Handlers;
///
/// Abstract base class for instruction handlers
///
public abstract class InstructionHandler : IInstructionHandler
{
// 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;
///
/// Initializes a new instance of the InstructionHandler class
///
/// The buffer containing the code to decode
/// The instruction decoder that owns this handler
/// The length of the buffer
protected InstructionHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
{
CodeBuffer = codeBuffer;
Decoder = decoder;
Length = length;
ModRMDecoder = new ModRMDecoder(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 abstract bool CanHandle(byte opcode);
///
/// Decodes an instruction
///
/// The opcode of the instruction
/// 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];
}
}