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

Implemented individual handlers for Group1 and Group3 instructions

This commit is contained in:
bird_egop 2025-04-12 20:13:01 +03:00
parent 58a148ebd8
commit e4b8645da0
28 changed files with 2218 additions and 2 deletions

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for ADC r/m32, imm32 instruction (0x81 /2)
/// </summary>
public class AdcImmToRm32Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the AdcImmToRm32Handler 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 AdcImmToRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 2 (ADC)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 2; // 2 = ADC
}
/// <summary>
/// Decodes an ADC r/m32, imm32 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
instruction.Mnemonic = "adc";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 2 for ADC
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,85 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for ADC r/m32, imm8 (sign-extended) instruction (0x83 /2)
/// </summary>
public class AdcImmToRm32SignExtendedHandler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the AdcImmToRm32SignExtendedHandler 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 AdcImmToRm32SignExtendedHandler(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)
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 2 (ADC)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 2; // 2 = ADC
}
/// <summary>
/// Decodes an ADC r/m32, imm8 (sign-extended) 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
instruction.Mnemonic = "adc";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 2 for ADC
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length)
{
return false;
}
sbyte imm8 = (sbyte)CodeBuffer[position];
int imm32 = imm8; // Sign-extend to 32 bits
Decoder.SetPosition(position + 1);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for ADD r/m32, imm32 instruction (0x81 /0)
/// </summary>
public class AddImmToRm32Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the AddImmToRm32Handler 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 AddImmToRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 0 (ADD)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 0; // 0 = ADD
}
/// <summary>
/// Decodes an ADD r/m32, imm32 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
instruction.Mnemonic = "add";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for ADD
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,85 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for ADD r/m32, imm8 (sign-extended) instruction (0x83 /0)
/// </summary>
public class AddImmToRm32SignExtendedHandler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the AddImmToRm32SignExtendedHandler 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 AddImmToRm32SignExtendedHandler(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)
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 0 (ADD)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 0; // 0 = ADD
}
/// <summary>
/// Decodes an ADD r/m32, imm8 (sign-extended) 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
instruction.Mnemonic = "add";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for ADD
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position >= Length)
{
return false;
}
// Read the immediate value as a signed byte and sign-extend it
sbyte imm8 = (sbyte)CodeBuffer[position++];
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{(uint)imm8:X2}";
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for ADD r/m8, imm8 instruction (0x80 /0)
/// </summary>
public class AddImmToRm8Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the AddImmToRm8Handler 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 AddImmToRm8Handler(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)
{
if (opcode != 0x80)
return false;
// Check if the reg field of the ModR/M byte is 0 (ADD)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 0; // 0 = ADD
}
/// <summary>
/// Decodes an ADD r/m8, imm8 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
instruction.Mnemonic = "add";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for ADD
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position >= Length)
{
return false;
}
byte imm8 = CodeBuffer[position++];
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for AND r/m32, imm32 instruction (0x81 /4)
/// </summary>
public class AndImmWithRm32Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the AndImmWithRm32Handler 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 AndImmWithRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 4 (AND)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 4; // 4 = AND
}
/// <summary>
/// Decodes an AND r/m32, imm32 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
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 4 for AND
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,85 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for AND r/m32, imm8 (sign-extended) instruction (0x83 /4)
/// </summary>
public class AndImmWithRm32SignExtendedHandler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the AndImmWithRm32SignExtendedHandler 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 AndImmWithRm32SignExtendedHandler(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)
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 4 (AND)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 4; // 4 = AND
}
/// <summary>
/// Decodes an AND r/m32, imm8 (sign-extended) 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
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 4 for AND
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length)
{
return false;
}
sbyte imm8 = (sbyte)CodeBuffer[position];
int imm32 = imm8; // Sign-extend to 32 bits
Decoder.SetPosition(position + 1);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for CMP r/m32, imm32 instruction (0x81 /7)
/// </summary>
public class CmpImmWithRm32Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the CmpImmWithRm32Handler 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 CmpImmWithRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 7 (CMP)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP
}
/// <summary>
/// Decodes a CMP r/m32, imm32 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
instruction.Mnemonic = "cmp";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 7 for CMP
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,85 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for CMP r/m32, imm8 (sign-extended) instruction (0x83 /7)
/// </summary>
public class CmpImmWithRm32SignExtendedHandler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the CmpImmWithRm32SignExtendedHandler 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 CmpImmWithRm32SignExtendedHandler(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)
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 7 (CMP)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP
}
/// <summary>
/// Decodes a CMP r/m32, imm8 (sign-extended) 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
instruction.Mnemonic = "cmp";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 7 for CMP
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position >= Length)
{
return false;
}
// Read the immediate value as a signed byte and sign-extend it
sbyte imm8 = (sbyte)CodeBuffer[position++];
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{(uint)imm8:X2}";
return true;
}
}

View File

@ -0,0 +1,44 @@
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

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for OR r/m32, imm32 instruction (0x81 /1)
/// </summary>
public class OrImmToRm32Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the OrImmToRm32Handler 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 OrImmToRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 1 (OR)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR
}
/// <summary>
/// Decodes an OR r/m32, imm32 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
instruction.Mnemonic = "or";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 1 for OR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,85 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for OR r/m32, imm8 (sign-extended) instruction (0x83 /1)
/// </summary>
public class OrImmToRm32SignExtendedHandler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the OrImmToRm32SignExtendedHandler 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 OrImmToRm32SignExtendedHandler(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)
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 1 (OR)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR
}
/// <summary>
/// Decodes an OR r/m32, imm8 (sign-extended) 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
instruction.Mnemonic = "or";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 1 for OR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length)
{
return false;
}
sbyte imm8 = (sbyte)CodeBuffer[position];
int imm32 = imm8; // Sign-extend to 32 bits
Decoder.SetPosition(position + 1);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for OR r/m8, imm8 instruction (0x80 /1)
/// </summary>
public class OrImmToRm8Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the OrImmToRm8Handler 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 OrImmToRm8Handler(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)
{
if (opcode != 0x80)
return false;
// Check if the reg field of the ModR/M byte is 1 (OR)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR
}
/// <summary>
/// Decodes an OR r/m8, imm8 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
instruction.Mnemonic = "or";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 1 for OR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position >= Length)
{
return false;
}
byte imm8 = CodeBuffer[position++];
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for SBB r/m32, imm32 instruction (0x81 /3)
/// </summary>
public class SbbImmFromRm32Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the SbbImmFromRm32Handler 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 SbbImmFromRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 3 (SBB)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 3; // 3 = SBB
}
/// <summary>
/// Decodes a SBB r/m32, imm32 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
instruction.Mnemonic = "sbb";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 3 for SBB
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,85 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for SBB r/m32, imm8 (sign-extended) instruction (0x83 /3)
/// </summary>
public class SbbImmFromRm32SignExtendedHandler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the SbbImmFromRm32SignExtendedHandler 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 SbbImmFromRm32SignExtendedHandler(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)
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 3 (SBB)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 3; // 3 = SBB
}
/// <summary>
/// Decodes a SBB r/m32, imm8 (sign-extended) 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
instruction.Mnemonic = "sbb";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 3 for SBB
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length)
{
return false;
}
sbyte imm8 = (sbyte)CodeBuffer[position];
int imm32 = imm8; // Sign-extend to 32 bits
Decoder.SetPosition(position + 1);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for SUB r/m32, imm32 instruction (0x81 /5)
/// </summary>
public class SubImmFromRm32Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the SubImmFromRm32Handler 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 SubImmFromRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 5 (SUB)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 5; // 5 = SUB
}
/// <summary>
/// Decodes a SUB r/m32, imm32 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
instruction.Mnemonic = "sub";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 5 for SUB
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,85 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for SUB r/m32, imm8 (sign-extended) instruction (0x83 /5)
/// </summary>
public class SubImmFromRm32SignExtendedHandler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the SubImmFromRm32SignExtendedHandler 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 SubImmFromRm32SignExtendedHandler(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)
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 5 (SUB)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 5; // 5 = SUB
}
/// <summary>
/// Decodes a SUB r/m32, imm8 (sign-extended) 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
instruction.Mnemonic = "sub";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 5 for SUB
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position >= Length)
{
return false;
}
// Read the immediate value as a signed byte and sign-extend it
sbyte imm8 = (sbyte)CodeBuffer[position++];
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{(uint)imm8:X2}";
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for XOR r/m32, imm32 instruction (0x81 /6)
/// </summary>
public class XorImmWithRm32Handler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the XorImmWithRm32Handler 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 XorImmWithRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 6 (XOR)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 6; // 6 = XOR
}
/// <summary>
/// Decodes a XOR r/m32, imm32 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
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 6 for XOR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,85 @@
namespace X86Disassembler.X86.Handlers.Group1;
/// <summary>
/// Handler for XOR r/m32, imm8 (sign-extended) instruction (0x83 /6)
/// </summary>
public class XorImmWithRm32SignExtendedHandler : Group1BaseHandler
{
/// <summary>
/// Initializes a new instance of the XorImmWithRm32SignExtendedHandler 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 XorImmWithRm32SignExtendedHandler(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)
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 6 (XOR)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 6; // 6 = XOR
}
/// <summary>
/// Decodes a XOR r/m32, imm8 (sign-extended) 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
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 6 for XOR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length)
{
return false;
}
sbyte imm8 = (sbyte)CodeBuffer[position];
int imm32 = imm8; // Sign-extend to 32 bits
Decoder.SetPosition(position + 1);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -0,0 +1,75 @@
namespace X86Disassembler.X86.Handlers.Group3;
/// <summary>
/// Handler for DIV r/m32 instruction (0xF7 /6)
/// </summary>
public class DivRm32Handler : Group3BaseHandler
{
/// <summary>
/// Initializes a new instance of the DivRm32Handler 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 DivRm32Handler(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)
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 6 (DIV)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 6; // 6 = DIV
}
/// <summary>
/// Decodes a DIV r/m32 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
instruction.Mnemonic = "div";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 6 for DIV
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands
instruction.Operands = operand;
return true;
}
}

View File

@ -0,0 +1,44 @@
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

@ -0,0 +1,75 @@
namespace X86Disassembler.X86.Handlers.Group3;
/// <summary>
/// Handler for IDIV r/m32 instruction (0xF7 /7)
/// </summary>
public class IdivRm32Handler : Group3BaseHandler
{
/// <summary>
/// Initializes a new instance of the IdivRm32Handler 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 IdivRm32Handler(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)
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 7 (IDIV)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 7; // 7 = IDIV
}
/// <summary>
/// Decodes an IDIV r/m32 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
instruction.Mnemonic = "idiv";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 7 for IDIV
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands
instruction.Operands = operand;
return true;
}
}

View File

@ -0,0 +1,75 @@
namespace X86Disassembler.X86.Handlers.Group3;
/// <summary>
/// Handler for IMUL r/m32 instruction (0xF7 /5)
/// </summary>
public class ImulRm32Handler : Group3BaseHandler
{
/// <summary>
/// Initializes a new instance of the ImulRm32Handler 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 ImulRm32Handler(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)
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 5 (IMUL)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 5; // 5 = IMUL
}
/// <summary>
/// Decodes an IMUL r/m32 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
instruction.Mnemonic = "imul";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 5 for IMUL
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands
instruction.Operands = operand;
return true;
}
}

View File

@ -0,0 +1,75 @@
namespace X86Disassembler.X86.Handlers.Group3;
/// <summary>
/// Handler for MUL r/m32 instruction (0xF7 /4)
/// </summary>
public class MulRm32Handler : Group3BaseHandler
{
/// <summary>
/// Initializes a new instance of the MulRm32Handler 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 MulRm32Handler(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)
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 4 (MUL)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 4; // 4 = MUL
}
/// <summary>
/// Decodes a MUL r/m32 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
instruction.Mnemonic = "mul";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 4 for MUL
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands
instruction.Operands = operand;
return true;
}
}

View File

@ -0,0 +1,75 @@
namespace X86Disassembler.X86.Handlers.Group3;
/// <summary>
/// Handler for NEG r/m32 instruction (0xF7 /3)
/// </summary>
public class NegRm32Handler : Group3BaseHandler
{
/// <summary>
/// Initializes a new instance of the NegRm32Handler 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 NegRm32Handler(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)
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 3 (NEG)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 3; // 3 = NEG
}
/// <summary>
/// Decodes a NEG r/m32 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
instruction.Mnemonic = "neg";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 3 for NEG
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands
instruction.Operands = operand;
return true;
}
}

View File

@ -0,0 +1,75 @@
namespace X86Disassembler.X86.Handlers.Group3;
/// <summary>
/// Handler for NOT r/m32 instruction (0xF7 /2)
/// </summary>
public class NotRm32Handler : Group3BaseHandler
{
/// <summary>
/// Initializes a new instance of the NotRm32Handler 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 NotRm32Handler(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)
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 2 (NOT)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 2; // 2 = NOT
}
/// <summary>
/// Decodes a NOT r/m32 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
instruction.Mnemonic = "not";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 2 for NOT
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands
instruction.Operands = operand;
return true;
}
}

View File

@ -0,0 +1,84 @@
namespace X86Disassembler.X86.Handlers.Group3;
/// <summary>
/// Handler for TEST r/m32, imm32 instruction (0xF7 /0)
/// </summary>
public class TestImmWithRm32Handler : Group3BaseHandler
{
/// <summary>
/// Initializes a new instance of the TestImmWithRm32Handler 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 TestImmWithRm32Handler(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)
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 0 (TEST)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 0; // 0 = TEST
}
/// <summary>
/// Decodes a TEST r/m32, imm32 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
instruction.Mnemonic = "test";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for TEST
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}

View File

@ -1,5 +1,8 @@
namespace X86Disassembler.X86.Handlers; namespace X86Disassembler.X86.Handlers;
using X86Disassembler.X86.Handlers.Group1;
using X86Disassembler.X86.Handlers.Group3;
/// <summary> /// <summary>
/// Factory for creating instruction handlers /// Factory for creating instruction handlers
/// </summary> /// </summary>
@ -45,13 +48,84 @@ public class InstructionHandlerFactory
_handlers.Add(new ConditionalJumpHandler(_codeBuffer, _decoder, _length)); _handlers.Add(new ConditionalJumpHandler(_codeBuffer, _decoder, _length));
_handlers.Add(new TwoByteConditionalJumpHandler(_codeBuffer, _decoder, _length)); _handlers.Add(new TwoByteConditionalJumpHandler(_codeBuffer, _decoder, _length));
// Register Group1 handlers
RegisterGroup1Handlers();
// Register Group3 handlers
RegisterGroup3Handlers();
// Register group handlers for instructions that share similar decoding logic // Register group handlers for instructions that share similar decoding logic
_handlers.Add(new Group1Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new Group3Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new FloatingPointHandler(_codeBuffer, _decoder, _length)); _handlers.Add(new FloatingPointHandler(_codeBuffer, _decoder, _length));
_handlers.Add(new DataTransferHandler(_codeBuffer, _decoder, _length)); _handlers.Add(new DataTransferHandler(_codeBuffer, _decoder, _length));
} }
/// <summary>
/// Registers the Group1 handlers
/// </summary>
private void RegisterGroup1Handlers()
{
// ADD handlers
_handlers.Add(new AddImmToRm8Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AddImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AddImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// OR handlers
_handlers.Add(new OrImmToRm8Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new OrImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new OrImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// ADC handlers
_handlers.Add(new AdcImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AdcImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// SBB handlers
_handlers.Add(new SbbImmFromRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new SbbImmFromRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// AND handlers
_handlers.Add(new AndImmWithRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AndImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// SUB handlers
_handlers.Add(new SubImmFromRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new SubImmFromRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// XOR handlers
_handlers.Add(new XorImmWithRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new XorImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// CMP handlers
_handlers.Add(new CmpImmWithRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new CmpImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
}
/// <summary>
/// Registers the Group3 handlers
/// </summary>
private void RegisterGroup3Handlers()
{
// TEST handler
_handlers.Add(new TestImmWithRm32Handler(_codeBuffer, _decoder, _length));
// NOT handler
_handlers.Add(new NotRm32Handler(_codeBuffer, _decoder, _length));
// NEG handler
_handlers.Add(new NegRm32Handler(_codeBuffer, _decoder, _length));
// MUL handler
_handlers.Add(new MulRm32Handler(_codeBuffer, _decoder, _length));
// IMUL handler
_handlers.Add(new ImulRm32Handler(_codeBuffer, _decoder, _length));
// DIV handler
_handlers.Add(new DivRm32Handler(_codeBuffer, _decoder, _length));
// IDIV handler
_handlers.Add(new IdivRm32Handler(_codeBuffer, _decoder, _length));
}
/// <summary> /// <summary>
/// Gets a handler that can decode the given opcode /// Gets a handler that can decode the given opcode
/// </summary> /// </summary>