0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 16:18:37 +03:00

Updated instruction handlers to use Type and StructuredOperands instead of Mnemonic and Operands

This commit is contained in:
bird_egop
2025-04-14 22:08:50 +03:00
parent c516e063e7
commit 685eeda03d
136 changed files with 3694 additions and 2584 deletions

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Add;
/// <summary>
@ -8,11 +10,9 @@ public class AddEaxImmHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AddEaxImmHandler 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 AddEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AddEaxImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,7 @@ public class AddEaxImmHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "add";
instruction.Type = InstructionType.Add;
if (!Decoder.CanReadUInt())
{
@ -45,11 +44,11 @@ public class AddEaxImmHandler : InstructionHandler
// Read the 32-bit immediate value
uint imm32 = Decoder.ReadUInt32();
// Format the immediate value
string immStr = $"0x{imm32:X}";
// Set the operands
instruction.Operands = $"eax, {immStr}";
instruction.StructuredOperands =
[
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32),
OperandFactory.CreateImmediateOperand(imm32, 32)
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Add;
/// <summary>
@ -8,11 +10,9 @@ public class AddImmToRm32Handler : InstructionHandler
/// <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)
public AddImmToRm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -27,11 +27,10 @@ public class AddImmToRm32Handler : InstructionHandler
return false;
// Check if the reg field of the ModR/M byte is 0 (ADD)
int position = Decoder.GetPosition();
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte modRM = Decoder.PeakByte();
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 0; // 0 = ADD
@ -46,7 +45,7 @@ public class AddImmToRm32Handler : InstructionHandler
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "add";
instruction.Type = InstructionType.Add;
if (!Decoder.CanReadByte())
{
@ -65,12 +64,10 @@ public class AddImmToRm32Handler : InstructionHandler
// Read the immediate value in little-endian format
var imm = Decoder.ReadUInt32();
// Format the immediate value as expected by the tests (0x12345678)
// Note: The bytes are reversed to match the expected format in the tests
string immStr = $"0x{imm:X8}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
instruction.StructuredOperands = [
destOperand,
OperandFactory.CreateImmediateOperand(imm, 32)
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Add;
/// <summary>
@ -8,11 +10,9 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
/// <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)
public AddImmToRm32SignExtendedHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -27,11 +27,10 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
return false;
// Check if the reg field of the ModR/M byte is 0 (ADD)
int position = Decoder.GetPosition();
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte modRM = Decoder.PeakByte();
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 0; // 0 = ADD
@ -45,8 +44,7 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "add";
instruction.Type = InstructionType.Add;
if (!Decoder.CanReadByte())
{
@ -65,21 +63,10 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
// Read the immediate value as a signed byte and automatically sign-extend it to int
int imm = (sbyte) Decoder.ReadByte();
// Format the immediate value
string immStr;
if (imm < 0)
{
// For negative values, use the full 32-bit representation (0xFFFFFFxx)
immStr = $"0x{(uint) imm:X8}";
}
else
{
// For positive values, use the regular format with leading zeros
immStr = $"0x{imm:X8}";
}
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
instruction.StructuredOperands = [
destOperand,
OperandFactory.CreateImmediateOperand(imm, 32),
];
return true;
}

View File

@ -1,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.Add;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for ADD r/m8, imm8 instruction (0x80 /0)
/// </summary>
@ -8,11 +10,9 @@ public class AddImmToRm8Handler : InstructionHandler
/// <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)
public AddImmToRm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -26,12 +26,10 @@ public class AddImmToRm8Handler : InstructionHandler
if (opcode != 0x80)
return false;
// Check if the reg field of the ModR/M byte is 0 (ADD)
int position = Decoder.GetPosition();
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte modRM = Decoder.PeakByte();
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 0; // 0 = ADD
@ -45,8 +43,8 @@ public class AddImmToRm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "add";
// Set the instruction type and mnemonic
instruction.Type = InstructionType.Add;
if (!Decoder.CanReadByte())
{
@ -56,12 +54,8 @@ public class AddImmToRm8Handler : InstructionHandler
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For direct register addressing (mod == 3), use 8-bit register names
if (mod == 3)
{
// Use 8-bit register names for direct register addressing
destOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
// Adjust the operand size to 8-bit
destOperand.Size = 8;
// Read the immediate value
if (!Decoder.CanReadByte())
@ -71,8 +65,15 @@ public class AddImmToRm8Handler : InstructionHandler
byte imm8 = Decoder.ReadByte();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
// Create the immediate operand
var sourceOperand = OperandFactory.CreateImmediateOperand(imm8, 8);
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
sourceOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Add;
/// <summary>
@ -8,11 +10,9 @@ public class AddR32Rm32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AddR32Rm32Handler 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 AddR32Rm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AddR32Rm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,27 +34,30 @@ public class AddR32Rm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the instruction type
instruction.Type = InstructionType.Add;
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For ADD r32, r/m32 (0x03):
// - The reg field specifies the destination register
// - The r/m field with mod specifies the source operand (register or memory)
// The sourceOperand is already created by ModRMDecoder based on mod and rm fields
var (mod, reg, rm, sourceOperand) = ModRMDecoder.ReadModRM();
// Set the mnemonic
instruction.Mnemonic = "add";
// Create the destination register operand from the reg field
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, 32);
// Get the register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
if (mod == 3)
{
// Register operand
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
instruction.Operands = $"{regName}, {destOperand}";
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Add;
/// <summary>
@ -8,11 +10,9 @@ public class AddRm32R32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AddRm32R32Handler 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 AddRm32R32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AddRm32R32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,27 +34,30 @@ public class AddRm32R32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the instruction type
instruction.Type = InstructionType.Add;
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For ADD r/m32, r32 (0x01):
// - The r/m field with mod specifies the destination operand (register or memory)
// - The reg field specifies the source register
// The destinationOperand is already created by ModRMDecoder based on mod and rm fields
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
// Set the mnemonic
instruction.Mnemonic = "add";
// Create the source register operand from the reg field
var sourceOperand = OperandFactory.CreateRegisterOperand(reg, 32);
// Get the register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
if (mod == 3)
{
// Register operand
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
instruction.Operands = $"{destOperand}, {regName}";
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}