0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 08:18:36 +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,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.Test;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for TEST AL, imm8 instruction (0xA8)
/// </summary>
@ -8,11 +10,9 @@ public class TestAlImmHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the TestAlImmHandler 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 TestAlImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public TestAlImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class TestAlImmHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "test";
// Set the instruction type
instruction.Type = InstructionType.Test;
if (!Decoder.CanReadByte())
{
@ -45,8 +45,18 @@ public class TestAlImmHandler : InstructionHandler
// Read the immediate value
byte imm8 = Decoder.ReadByte();
// Set the operands
instruction.Operands = $"al, 0x{imm8:X2}";
// Create the register operand for AL
var alOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 8);
// Create the immediate operand
var immOperand = OperandFactory.CreateImmediateOperand(imm8, 8);
// Set the structured operands
instruction.StructuredOperands =
[
alOperand,
immOperand
];
return true;
}

View File

@ -1,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.Test;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for TEST EAX, imm32 instruction (0xA9)
/// </summary>
@ -8,11 +10,9 @@ public class TestEaxImmHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the TestEaxImmHandler 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 TestEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public TestEaxImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class TestEaxImmHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "test";
// Set the instruction type
instruction.Type = InstructionType.Test;
if (!Decoder.CanReadUInt())
{
@ -45,8 +45,18 @@ public class TestEaxImmHandler : InstructionHandler
// Read the immediate value - x86 is little-endian, so we need to read the bytes in the correct order
var imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"eax, 0x{imm32:X8}";
// Create the register operand for EAX
var eaxOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A);
// Create the immediate operand
var immOperand = OperandFactory.CreateImmediateOperand(imm32);
// Set the structured operands
instruction.StructuredOperands =
[
eaxOperand,
immOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Test;
/// <summary>
@ -8,11 +10,9 @@ public class TestImmWithRm32Handler : InstructionHandler
/// <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)
public TestImmWithRm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -36,7 +36,7 @@ public class TestImmWithRm32Handler : InstructionHandler
}
// Check if the reg field is 0 (TEST operation)
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte modRM = Decoder.PeakByte();
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 0; // 0 = TEST
@ -50,7 +50,8 @@ public class TestImmWithRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
instruction.Mnemonic = "test";
// Set the instruction type
instruction.Type = InstructionType.Test;
if (!Decoder.CanReadByte())
{
@ -58,13 +59,7 @@ public class TestImmWithRm32Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3)
{
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
// Read the immediate value
if (!Decoder.CanReadUInt())
@ -73,9 +68,16 @@ public class TestImmWithRm32Handler : InstructionHandler
}
uint imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
// Create the source immediate operand
var sourceOperand = OperandFactory.CreateImmediateOperand(imm32, 32);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Test;
/// <summary>
@ -8,11 +10,9 @@ public class TestImmWithRm8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the TestImmWithRm8Handler 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 TestImmWithRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public TestImmWithRm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -36,7 +36,7 @@ public class TestImmWithRm8Handler : InstructionHandler
}
// Check if the reg field is 0 (TEST operation)
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte modRM = Decoder.PeakByte();
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 0; // 0 = TEST
@ -50,18 +50,14 @@ public class TestImmWithRm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "test";
// Set the instruction type
instruction.Type = InstructionType.Test;
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
// Get the destination operand based on addressing mode
if (mod == 3) // Register operand
{
// For direct register addressing, use the correct 8-bit register name
destOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
// Ensure the destination operand has the correct size (8-bit)
destinationOperand.Size = 8;
// Check if we have enough bytes for the immediate value
if (!Decoder.CanReadByte())
@ -71,9 +67,16 @@ public class TestImmWithRm8Handler : InstructionHandler
// Read the immediate value
byte imm8 = Decoder.ReadByte();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
// Create the source immediate operand
var sourceOperand = OperandFactory.CreateImmediateOperand(imm8, 8);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.Test;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for TEST r/m8, r8 instruction (0x84)
/// </summary>
@ -8,11 +10,9 @@ public class TestRegMem8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the TestRegMem8Handler 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 TestRegMem8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public TestRegMem8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class TestRegMem8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "test";
// Set the instruction type
instruction.Type = InstructionType.Test;
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
@ -46,19 +46,30 @@ public class TestRegMem8Handler : InstructionHandler
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get the register name for the reg field
string regOperand = ModRMDecoder.GetRegisterName(reg, 8);
// Create the register operand for the reg field (8-bit)
var regOperand = OperandFactory.CreateRegisterOperand(reg, 8);
// For direct register addressing (mod == 3), get the r/m register name
if (mod == 3)
// Set the structured operands based on addressing mode
if (mod == 3) // Direct register addressing
{
string rmOperand = ModRMDecoder.GetRegisterName(rm, 8);
instruction.Operands = $"{rmOperand}, {regOperand}";
// Create the register operand for the r/m field (8-bit)
var rmOperand = OperandFactory.CreateRegisterOperand(rm, 8);
// Set the structured operands
instruction.StructuredOperands =
[
rmOperand,
regOperand
];
}
else
else // Memory addressing
{
// For memory operands, use the decoded operand string
instruction.Operands = $"{destOperand}, {regOperand}";
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
regOperand
];
}
return true;

View File

@ -1,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.Test;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for TEST r/m32, r32 instruction (0x85)
/// </summary>
@ -8,11 +10,9 @@ public class TestRegMemHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the TestRegMemHandler 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 TestRegMemHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public TestRegMemHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class TestRegMemHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "test";
// Set the instruction type
instruction.Type = InstructionType.Test;
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
@ -46,19 +46,30 @@ public class TestRegMemHandler : InstructionHandler
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get the register name for the reg field
string regOperand = ModRMDecoder.GetRegisterName(reg, 32);
// Create the register operand for the reg field
var regOperand = OperandFactory.CreateRegisterOperand(reg);
// For direct register addressing (mod == 3), get the r/m register name
if (mod == 3)
// Set the structured operands based on addressing mode
if (mod == 3) // Direct register addressing
{
string rmOperand = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{rmOperand}, {regOperand}";
// Create the register operand for the r/m field
var rmOperand = OperandFactory.CreateRegisterOperand(rm);
// Set the structured operands
instruction.StructuredOperands =
[
rmOperand,
regOperand
];
}
else
else // Memory addressing
{
// For memory operands, use the decoded operand string
instruction.Operands = $"{destOperand}, {regOperand}";
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
regOperand
];
}
return true;