0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 20:01:17 +03:00

move handlers, remove bases

This commit is contained in:
bird_egop 2025-04-12 23:03:07 +03:00
parent acccf5169a
commit bb695cf3bb
30 changed files with 74 additions and 482 deletions

View File

@ -1,334 +0,0 @@
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)
{
// 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;
}
}

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for ADC r/m32, imm32 instruction (0x81 /2) /// Handler for ADC r/m32, imm32 instruction (0x81 /2)
/// </summary> /// </summary>
public class AdcImmToRm32Handler : Group1BaseHandler public class AdcImmToRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the AdcImmToRm32Handler class /// Initializes a new instance of the AdcImmToRm32Handler class
@ -65,7 +65,7 @@ public class AdcImmToRm32Handler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for ADC r/m32, imm8 (sign-extended) instruction (0x83 /2) /// Handler for ADC r/m32, imm8 (sign-extended) instruction (0x83 /2)
/// </summary> /// </summary>
public class AdcImmToRm32SignExtendedHandler : Group1BaseHandler public class AdcImmToRm32SignExtendedHandler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the AdcImmToRm32SignExtendedHandler class /// Initializes a new instance of the AdcImmToRm32SignExtendedHandler class
@ -65,7 +65,7 @@ public class AdcImmToRm32SignExtendedHandler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // 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) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for ADD r/m32, imm32 instruction (0x81 /0) /// Handler for ADD r/m32, imm32 instruction (0x81 /0)
/// </summary> /// </summary>
public class AddImmToRm32Handler : Group1BaseHandler public class AddImmToRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the AddImmToRm32Handler class /// Initializes a new instance of the AddImmToRm32Handler class
@ -65,7 +65,7 @@ public class AddImmToRm32Handler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for ADD r/m32, imm8 (sign-extended) instruction (0x83 /0) /// Handler for ADD r/m32, imm8 (sign-extended) instruction (0x83 /0)
/// </summary> /// </summary>
public class AddImmToRm32SignExtendedHandler : Group1BaseHandler public class AddImmToRm32SignExtendedHandler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the AddImmToRm32SignExtendedHandler class /// Initializes a new instance of the AddImmToRm32SignExtendedHandler class
@ -65,7 +65,7 @@ public class AddImmToRm32SignExtendedHandler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position >= Length) if (position >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for ADD r/m8, imm8 instruction (0x80 /0) /// Handler for ADD r/m8, imm8 instruction (0x80 /0)
/// </summary> /// </summary>
public class AddImmToRm8Handler : Group1BaseHandler public class AddImmToRm8Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the AddImmToRm8Handler class /// Initializes a new instance of the AddImmToRm8Handler class
@ -73,7 +73,7 @@ public class AddImmToRm8Handler : Group1BaseHandler
else else
{ {
// Use ModR/M decoder for memory addressing // Use ModR/M decoder for memory addressing
destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
} }
Decoder.SetPosition(position); Decoder.SetPosition(position);

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for AND r/m32, imm32 instruction (0x81 /4) /// Handler for AND r/m32, imm32 instruction (0x81 /4)
/// </summary> /// </summary>
public class AndImmWithRm32Handler : Group1BaseHandler public class AndImmWithRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the AndImmWithRm32Handler class /// Initializes a new instance of the AndImmWithRm32Handler class
@ -65,7 +65,7 @@ public class AndImmWithRm32Handler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for AND r/m32, imm8 (sign-extended) instruction (0x83 /4) /// Handler for AND r/m32, imm8 (sign-extended) instruction (0x83 /4)
/// </summary> /// </summary>
public class AndImmWithRm32SignExtendedHandler : Group1BaseHandler public class AndImmWithRm32SignExtendedHandler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the AndImmWithRm32SignExtendedHandler class /// Initializes a new instance of the AndImmWithRm32SignExtendedHandler class
@ -65,7 +65,7 @@ public class AndImmWithRm32SignExtendedHandler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // 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) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for CMP r/m32, imm32 instruction (0x81 /7) /// Handler for CMP r/m32, imm32 instruction (0x81 /7)
/// </summary> /// </summary>
public class CmpImmWithRm32Handler : Group1BaseHandler public class CmpImmWithRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the CmpImmWithRm32Handler class /// Initializes a new instance of the CmpImmWithRm32Handler class
@ -65,7 +65,7 @@ public class CmpImmWithRm32Handler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for CMP r/m32, imm8 (sign-extended) instruction (0x83 /7) /// Handler for CMP r/m32, imm8 (sign-extended) instruction (0x83 /7)
/// </summary> /// </summary>
public class CmpImmWithRm32SignExtendedHandler : Group1BaseHandler public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the CmpImmWithRm32SignExtendedHandler class /// Initializes a new instance of the CmpImmWithRm32SignExtendedHandler class
@ -65,7 +65,7 @@ public class CmpImmWithRm32SignExtendedHandler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position >= Length) if (position >= Length)

View File

@ -1,44 +0,0 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Base class for Group 1 instruction handlers (ADD, OR, ADC, SBB, AND, SUB, XOR, CMP)
/// </summary>
public abstract class Group1BaseHandler : InstructionHandler
{
// ModR/M decoder
protected readonly ModRMDecoder _modRMDecoder;
/// <summary>
/// Initializes a new instance of the Group1BaseHandler 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 Group1BaseHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
_modRMDecoder = new ModRMDecoder(codeBuffer, decoder, length);
}
/// <summary>
/// Gets the 32-bit register name for the given register index
/// </summary>
/// <param name="reg">The register index</param>
/// <returns>The register name</returns>
protected static string GetRegister32(byte reg)
{
string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
return registerNames[reg & 0x07];
}
/// <summary>
/// Gets the 8-bit register name for the given register index
/// </summary>
/// <param name="reg">The register index</param>
/// <returns>The register name</returns>
protected static string GetRegister8(byte reg)
{
string[] registerNames = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
return registerNames[reg & 0x07];
}
}

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for OR r/m32, imm32 instruction (0x81 /1) /// Handler for OR r/m32, imm32 instruction (0x81 /1)
/// </summary> /// </summary>
public class OrImmToRm32Handler : Group1BaseHandler public class OrImmToRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the OrImmToRm32Handler class /// Initializes a new instance of the OrImmToRm32Handler class
@ -65,7 +65,7 @@ public class OrImmToRm32Handler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for OR r/m32, imm8 (sign-extended) instruction (0x83 /1) /// Handler for OR r/m32, imm8 (sign-extended) instruction (0x83 /1)
/// </summary> /// </summary>
public class OrImmToRm32SignExtendedHandler : Group1BaseHandler public class OrImmToRm32SignExtendedHandler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the OrImmToRm32SignExtendedHandler class /// Initializes a new instance of the OrImmToRm32SignExtendedHandler class
@ -65,7 +65,7 @@ public class OrImmToRm32SignExtendedHandler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // 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) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for OR r/m8, imm8 instruction (0x80 /1) /// Handler for OR r/m8, imm8 instruction (0x80 /1)
/// </summary> /// </summary>
public class OrImmToRm8Handler : Group1BaseHandler public class OrImmToRm8Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the OrImmToRm8Handler class /// Initializes a new instance of the OrImmToRm8Handler class
@ -73,7 +73,7 @@ public class OrImmToRm8Handler : Group1BaseHandler
else else
{ {
// Use ModR/M decoder for memory addressing // Use ModR/M decoder for memory addressing
destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
} }
Decoder.SetPosition(position); Decoder.SetPosition(position);

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for SBB r/m32, imm32 instruction (0x81 /3) /// Handler for SBB r/m32, imm32 instruction (0x81 /3)
/// </summary> /// </summary>
public class SbbImmFromRm32Handler : Group1BaseHandler public class SbbImmFromRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the SbbImmFromRm32Handler class /// Initializes a new instance of the SbbImmFromRm32Handler class
@ -65,7 +65,7 @@ public class SbbImmFromRm32Handler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for SBB r/m32, imm8 (sign-extended) instruction (0x83 /3) /// Handler for SBB r/m32, imm8 (sign-extended) instruction (0x83 /3)
/// </summary> /// </summary>
public class SbbImmFromRm32SignExtendedHandler : Group1BaseHandler public class SbbImmFromRm32SignExtendedHandler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the SbbImmFromRm32SignExtendedHandler class /// Initializes a new instance of the SbbImmFromRm32SignExtendedHandler class
@ -65,7 +65,7 @@ public class SbbImmFromRm32SignExtendedHandler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // 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) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for SUB r/m32, imm32 instruction (0x81 /5) /// Handler for SUB r/m32, imm32 instruction (0x81 /5)
/// </summary> /// </summary>
public class SubImmFromRm32Handler : Group1BaseHandler public class SubImmFromRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the SubImmFromRm32Handler class /// Initializes a new instance of the SubImmFromRm32Handler class
@ -65,7 +65,7 @@ public class SubImmFromRm32Handler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group1;
/// <summary> /// <summary>
/// Handler for SUB r/m32, imm8 (sign-extended) instruction (0x83 /5) /// Handler for SUB r/m32, imm8 (sign-extended) instruction (0x83 /5)
/// </summary> /// </summary>
public class SubImmFromRm32SignExtendedHandler : Group1BaseHandler public class SubImmFromRm32SignExtendedHandler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the SubImmFromRm32SignExtendedHandler class /// Initializes a new instance of the SubImmFromRm32SignExtendedHandler class
@ -65,7 +65,7 @@ public class SubImmFromRm32SignExtendedHandler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position >= Length) if (position >= Length)

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3;
/// <summary> /// <summary>
/// Handler for DIV r/m32 instruction (0xF7 /6) /// Handler for DIV r/m32 instruction (0xF7 /6)
/// </summary> /// </summary>
public class DivRm32Handler : Group3BaseHandler public class DivRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the DivRm32Handler class /// Initializes a new instance of the DivRm32Handler class
@ -65,7 +65,7 @@ public class DivRm32Handler : Group3BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the operand // Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false); string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = operand;

View File

@ -1,44 +0,0 @@
namespace X86Disassembler.X86.Handlers.Group3;
/// <summary>
/// Base class for Group 3 instruction handlers (TEST, NOT, NEG, MUL, IMUL, DIV, IDIV)
/// </summary>
public abstract class Group3BaseHandler : InstructionHandler
{
// ModR/M decoder
protected readonly ModRMDecoder _modRMDecoder;
/// <summary>
/// Initializes a new instance of the Group3BaseHandler 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 Group3BaseHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
_modRMDecoder = new ModRMDecoder(codeBuffer, decoder, length);
}
/// <summary>
/// Gets the 32-bit register name for the given register index
/// </summary>
/// <param name="reg">The register index</param>
/// <returns>The register name</returns>
protected static string GetRegister32(byte reg)
{
string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
return registerNames[reg & 0x07];
}
/// <summary>
/// Gets the 8-bit register name for the given register index
/// </summary>
/// <param name="reg">The register index</param>
/// <returns>The register name</returns>
protected static string GetRegister8(byte reg)
{
string[] registerNames = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
return registerNames[reg & 0x07];
}
}

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3;
/// <summary> /// <summary>
/// Handler for IDIV r/m32 instruction (0xF7 /7) /// Handler for IDIV r/m32 instruction (0xF7 /7)
/// </summary> /// </summary>
public class IdivRm32Handler : Group3BaseHandler public class IdivRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the IdivRm32Handler class /// Initializes a new instance of the IdivRm32Handler class
@ -65,7 +65,7 @@ public class IdivRm32Handler : Group3BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the operand // Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false); string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = operand;

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3;
/// <summary> /// <summary>
/// Handler for IMUL r/m32 instruction (0xF7 /5) /// Handler for IMUL r/m32 instruction (0xF7 /5)
/// </summary> /// </summary>
public class ImulRm32Handler : Group3BaseHandler public class ImulRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the ImulRm32Handler class /// Initializes a new instance of the ImulRm32Handler class
@ -65,7 +65,7 @@ public class ImulRm32Handler : Group3BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the operand // Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false); string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = operand;

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3;
/// <summary> /// <summary>
/// Handler for MUL r/m32 instruction (0xF7 /4) /// Handler for MUL r/m32 instruction (0xF7 /4)
/// </summary> /// </summary>
public class MulRm32Handler : Group3BaseHandler public class MulRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the MulRm32Handler class /// Initializes a new instance of the MulRm32Handler class
@ -65,7 +65,7 @@ public class MulRm32Handler : Group3BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the operand // Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false); string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = operand;

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3;
/// <summary> /// <summary>
/// Handler for NEG r/m32 instruction (0xF7 /3) /// Handler for NEG r/m32 instruction (0xF7 /3)
/// </summary> /// </summary>
public class NegRm32Handler : Group3BaseHandler public class NegRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the NegRm32Handler class /// Initializes a new instance of the NegRm32Handler class
@ -65,7 +65,7 @@ public class NegRm32Handler : Group3BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the operand // Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false); string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = operand;

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.Group3;
/// <summary> /// <summary>
/// Handler for NOT r/m32 instruction (0xF7 /2) /// Handler for NOT r/m32 instruction (0xF7 /2)
/// </summary> /// </summary>
public class NotRm32Handler : Group3BaseHandler public class NotRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the NotRm32Handler class /// Initializes a new instance of the NotRm32Handler class
@ -81,7 +81,7 @@ public class NotRm32Handler : Group3BaseHandler
else else
{ {
// Use the ModR/M decoder for memory addressing // Use the ModR/M decoder for memory addressing
operand = _modRMDecoder.DecodeModRM(mod, rm, false); operand = ModRMDecoder.DecodeModRM(mod, rm, false);
} }
// Set the operands // Set the operands

View File

@ -45,4 +45,26 @@ public abstract class InstructionHandler : IInstructionHandler
/// <param name="instruction">The instruction object to populate</param> /// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns> /// <returns>True if the instruction was successfully decoded</returns>
public abstract bool Decode(byte opcode, Instruction instruction); public abstract bool Decode(byte opcode, Instruction instruction);
/// <summary>
/// Gets the 32-bit register name for the given register index
/// </summary>
/// <param name="reg">The register index</param>
/// <returns>The register name</returns>
protected static string GetRegister32(byte reg)
{
string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
return registerNames[reg & 0x07];
}
/// <summary>
/// Gets the 8-bit register name for the given register index
/// </summary>
/// <param name="reg">The register index</param>
/// <returns>The register name</returns>
protected static string GetRegister8(byte reg)
{
string[] registerNames = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
return registerNames[reg & 0x07];
}
} }

View File

@ -1,11 +1,9 @@
using X86Disassembler.X86.Handlers.Group3;
namespace X86Disassembler.X86.Handlers.Test; namespace X86Disassembler.X86.Handlers.Test;
/// <summary> /// <summary>
/// Handler for TEST r/m32, imm32 instruction (0xF7 /0) /// Handler for TEST r/m32, imm32 instruction (0xF7 /0)
/// </summary> /// </summary>
public class TestImmWithRm32Handler : Group3BaseHandler public class TestImmWithRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the TestImmWithRm32Handler class /// Initializes a new instance of the TestImmWithRm32Handler class
@ -75,7 +73,7 @@ public class TestImmWithRm32Handler : Group3BaseHandler
else else
{ {
// Use the ModR/M decoder for memory addressing // Use the ModR/M decoder for memory addressing
destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
} }
// Read the immediate value // Read the immediate value

View File

@ -1,11 +1,9 @@
using X86Disassembler.X86.Handlers.Group3;
namespace X86Disassembler.X86.Handlers.Test; namespace X86Disassembler.X86.Handlers.Test;
/// <summary> /// <summary>
/// Handler for TEST r/m8, imm8 instruction (0xF6 /0) /// Handler for TEST r/m8, imm8 instruction (0xF6 /0)
/// </summary> /// </summary>
public class TestImmWithRm8Handler : Group3BaseHandler public class TestImmWithRm8Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the TestImmWithRm8Handler class /// Initializes a new instance of the TestImmWithRm8Handler class
@ -75,7 +73,7 @@ public class TestImmWithRm8Handler : Group3BaseHandler
else else
{ {
// Use the ModR/M decoder for memory addressing // Use the ModR/M decoder for memory addressing
destOperand = _modRMDecoder.DecodeModRM(mod, rm, true); destOperand = ModRMDecoder.DecodeModRM(mod, rm, true);
} }
// Read the immediate value // Read the immediate value

View File

@ -1,11 +1,9 @@
using X86Disassembler.X86.Handlers.Group1;
namespace X86Disassembler.X86.Handlers.Xor; namespace X86Disassembler.X86.Handlers.Xor;
/// <summary> /// <summary>
/// Handler for XOR r/m32, imm32 instruction (0x81 /6) /// Handler for XOR r/m32, imm32 instruction (0x81 /6)
/// </summary> /// </summary>
public class XorImmWithRm32Handler : Group1BaseHandler public class XorImmWithRm32Handler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the XorImmWithRm32Handler class /// Initializes a new instance of the XorImmWithRm32Handler class
@ -67,7 +65,7 @@ public class XorImmWithRm32Handler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false); string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -1,11 +1,9 @@
using X86Disassembler.X86.Handlers.Group1;
namespace X86Disassembler.X86.Handlers.Xor; namespace X86Disassembler.X86.Handlers.Xor;
/// <summary> /// <summary>
/// Handler for XOR r/m32, imm8 (sign-extended) instruction (0x83 /6) /// Handler for XOR r/m32, imm8 (sign-extended) instruction (0x83 /6)
/// </summary> /// </summary>
public class XorImmWithRm32SignExtendedHandler : Group1BaseHandler public class XorImmWithRm32SignExtendedHandler : InstructionHandler
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the XorImmWithRm32SignExtendedHandler class /// Initializes a new instance of the XorImmWithRm32SignExtendedHandler class
@ -67,7 +65,7 @@ public class XorImmWithRm32SignExtendedHandler : Group1BaseHandler
byte rm = (byte)(modRM & 0x07); byte rm = (byte)(modRM & 0x07);
// Decode the destination operand // 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) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)