mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 16:18:37 +03:00
Implemented individual handlers for Group1 and Group3 instructions
This commit is contained in:
84
X86Disassembler/X86/Handlers/Group1/AdcImmToRm32Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/AdcImmToRm32Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/AddImmToRm32Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/AddImmToRm32Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/AddImmToRm8Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/AddImmToRm8Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/AndImmWithRm32Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/AndImmWithRm32Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/CmpImmWithRm32Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
44
X86Disassembler/X86/Handlers/Group1/Group1BaseHandler.cs
Normal file
44
X86Disassembler/X86/Handlers/Group1/Group1BaseHandler.cs
Normal 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];
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/OrImmToRm32Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/OrImmToRm32Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/OrImmToRm8Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/OrImmToRm8Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/SbbImmFromRm32Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/SubImmFromRm32Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/SubImmFromRm32Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
84
X86Disassembler/X86/Handlers/Group1/XorImmWithRm32Handler.cs
Normal file
84
X86Disassembler/X86/Handlers/Group1/XorImmWithRm32Handler.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user