mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 08:18:36 +03:00
Reorganized instruction handlers into more descriptive folders (ArithmeticImmediate and ArithmeticUnary)
This commit is contained in:
@ -0,0 +1,75 @@
|
||||
namespace X86Disassembler.X86.Handlers.ArithmeticUnary;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for DIV r/m32 instruction (0xF7 /6)
|
||||
/// </summary>
|
||||
public class DivRm32Handler : InstructionHandler
|
||||
{
|
||||
/// <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;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
namespace X86Disassembler.X86.Handlers.ArithmeticUnary;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for IDIV r/m32 instruction (0xF7 /7)
|
||||
/// </summary>
|
||||
public class IdivRm32Handler : InstructionHandler
|
||||
{
|
||||
/// <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;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
namespace X86Disassembler.X86.Handlers.ArithmeticUnary;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for IMUL r/m32 instruction (0xF7 /5)
|
||||
/// </summary>
|
||||
public class ImulRm32Handler : InstructionHandler
|
||||
{
|
||||
/// <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;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
namespace X86Disassembler.X86.Handlers.ArithmeticUnary;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for MUL r/m32 instruction (0xF7 /4)
|
||||
/// </summary>
|
||||
public class MulRm32Handler : InstructionHandler
|
||||
{
|
||||
/// <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;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
namespace X86Disassembler.X86.Handlers.ArithmeticUnary;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for NEG r/m32 instruction (0xF7 /3)
|
||||
/// </summary>
|
||||
public class NegRm32Handler : InstructionHandler
|
||||
{
|
||||
/// <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;
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
namespace X86Disassembler.X86.Handlers.ArithmeticUnary;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for NOT r/m32 instruction (0xF7 /2)
|
||||
/// </summary>
|
||||
public class NotRm32Handler : InstructionHandler
|
||||
{
|
||||
/// <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)
|
||||
{
|
||||
// This handler only handles opcode 0xF7
|
||||
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)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the ModR/M byte
|
||||
byte modRM = CodeBuffer[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);
|
||||
|
||||
// Verify this is a NOT instruction
|
||||
if (reg != 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "not";
|
||||
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
// For direct register addressing (mod == 3), the r/m field specifies a register
|
||||
string operand;
|
||||
if (mod == 3)
|
||||
{
|
||||
operand = GetRegister32(rm);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use the ModR/M decoder for memory addressing
|
||||
operand = ModRMDecoder.DecodeModRM(mod, rm, false);
|
||||
}
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = operand;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user