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.Or;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for OR AL, imm8 instruction (0x0C)
/// </summary>
@ -8,11 +10,9 @@ public class OrAlImmHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the OrAlImmHandler 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 OrAlImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public OrAlImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,6 +34,9 @@ public class OrAlImmHandler : 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.Or;
if (!Decoder.CanReadByte())
{
return false;
@ -42,11 +45,18 @@ public class OrAlImmHandler : InstructionHandler
// Read the immediate byte
byte imm8 = Decoder.ReadByte();
// Set the mnemonic
instruction.Mnemonic = "or";
// 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.Or;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for OR EAX, imm32 instruction (0x0D)
/// </summary>
@ -8,11 +10,9 @@ public class OrEaxImmHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the OrEaxImmHandler 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 OrEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public OrEaxImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,20 +34,29 @@ public class OrEaxImmHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
if (!Decoder.CanReadByte())
// Set the instruction type
instruction.Type = InstructionType.Or;
if (!Decoder.CanReadUInt())
{
return false;
}
// Read the immediate dword (little-endian)
uint imm32 = Decoder.ReadUInt32();
// Set the mnemonic
instruction.Mnemonic = "or";
// 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,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.Or;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for OR r/m32, imm32 instruction (0x81 /1)
/// </summary>
@ -8,11 +10,9 @@ public class OrImmToRm32Handler : InstructionHandler
/// <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)
public OrImmToRm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -30,7 +30,7 @@ public class OrImmToRm32Handler : InstructionHandler
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte modRM = Decoder.PeakByte();
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR
@ -44,8 +44,8 @@ public class OrImmToRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "or";
// Set the instruction type
instruction.Type = InstructionType.Or;
if (!Decoder.CanReadByte())
{
@ -55,15 +55,24 @@ public class OrImmToRm32Handler : InstructionHandler
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
if (!Decoder.CanReadByte())
// Check if we can read the immediate value
if (!Decoder.CanReadUInt())
{
return false;
}
// Read the immediate value
uint imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
// Create the immediate operand
var immOperand = OperandFactory.CreateImmediateOperand(imm32);
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
immOperand
];
return true;
}

View File

@ -1,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.Or;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for OR r/m32, imm8 (sign-extended) instruction (0x83 /1)
/// </summary>
@ -8,11 +10,9 @@ public class OrImmToRm32SignExtendedHandler : InstructionHandler
/// <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)
public OrImmToRm32SignExtendedHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -30,7 +30,7 @@ public class OrImmToRm32SignExtendedHandler : InstructionHandler
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte modRM = Decoder.PeakByte();
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR
@ -44,12 +44,10 @@ public class OrImmToRm32SignExtendedHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "or";
// Set the instruction type
instruction.Type = InstructionType.Or;
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
@ -58,16 +56,23 @@ public class OrImmToRm32SignExtendedHandler : InstructionHandler
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Sign-extend to 32 bits
int imm32 = (sbyte) Decoder.ReadByte();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
sbyte imm8 = (sbyte) Decoder.ReadByte();
// Create the immediate operand with sign extension
var immOperand = OperandFactory.CreateImmediateOperand(imm8);
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
immOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Or;
/// <summary>
@ -8,11 +10,9 @@ public class OrImmToRm8Handler : InstructionHandler
/// <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)
public OrImmToRm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -30,7 +30,7 @@ public class OrImmToRm8Handler : InstructionHandler
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte modRM = Decoder.PeakByte();
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR
@ -44,8 +44,8 @@ public class OrImmToRm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "or";
// Set the instruction type
instruction.Type = InstructionType.Or;
if (!Decoder.CanReadByte())
{
@ -53,14 +53,13 @@ public class OrImmToRm8Handler : 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);
}
// For OR r/m8, imm8 (0x80 /1):
// - The r/m field with mod specifies the destination operand (register or memory)
// - The immediate value is the source operand
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
// Adjust the operand size to 8-bit
destinationOperand.Size = 8;
// Read the immediate value
if (!Decoder.CanReadByte())
@ -70,9 +69,16 @@ public class OrImmToRm8Handler : 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.Or;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for OR r32, r/m32 instruction (0x0B)
/// </summary>
@ -8,11 +10,9 @@ public class OrR32Rm32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the OrR32Rm32Handler 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 OrR32Rm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public OrR32Rm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,6 +34,9 @@ public class OrR32Rm32Handler : 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.Or;
if (!Decoder.CanReadByte())
{
return false;
@ -42,21 +45,30 @@ public class OrR32Rm32Handler : InstructionHandler
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the mnemonic
instruction.Mnemonic = "or";
// Get the register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand
if (mod != 3) // Memory operand
// Create the register operand for the reg field
var regOperand = OperandFactory.CreateRegisterOperand(reg);
// Set the structured operands based on addressing mode
if (mod == 3) // Direct register addressing
{
instruction.Operands = $"{regName}, {destOperand}";
// Create the register operand for the r/m field
var rmOperand = OperandFactory.CreateRegisterOperand(rm);
// Set the structured operands
instruction.StructuredOperands =
[
regOperand,
rmOperand
];
}
else // Register operand
else // Memory addressing
{
string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{regName}, {rmName}";
// Set the structured operands
instruction.StructuredOperands =
[
regOperand,
destOperand
];
}
return true;

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Or;
/// <summary>
@ -8,11 +10,9 @@ public class OrR8Rm8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the OrR8Rm8Handler 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 OrR8Rm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public OrR8Rm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,32 +34,32 @@ public class OrR8Rm8Handler : 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.Or;
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For OR r8, r/m8 (0x0A):
// - The reg field specifies the destination register
// - The r/m field with mod specifies the source operand (register or memory)
var (mod, reg, rm, sourceOperand) = ModRMDecoder.ReadModRM();
// Set the mnemonic
instruction.Mnemonic = "or";
// Adjust the operand size to 8-bit
sourceOperand.Size = 8;
// Get the register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
// For memory operands, set the operand
if (mod != 3) // Memory operand
{
// Replace dword ptr with byte ptr for 8-bit operations
destOperand = destOperand.Replace("dword ptr", "byte ptr");
instruction.Operands = $"{regName}, {destOperand}";
}
else // Register operand
{
string rmName = ModRMDecoder.GetRegisterName(rm, 8);
instruction.Operands = $"{regName}, {rmName}";
}
// Create the destination register operand
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, 8);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Or;
/// <summary>
@ -8,11 +10,9 @@ public class OrRm8R8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the OrRm8R8Handler 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 OrRm8R8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public OrRm8R8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class OrRm8R8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "or";
// Set the instruction type
instruction.Type = InstructionType.Or;
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
@ -43,28 +43,24 @@ public class OrRm8R8Handler : InstructionHandler
return false;
}
// Read the ModR/M byte and decode the operands
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Read the ModR/M byte
// For OR r/m8, r8 (0x08):
// - The r/m field with mod specifies the destination operand (register or memory)
// - The reg field specifies the source register
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
// The register operand is in the reg field (8-bit register)
string regOperand = ModRMDecoder.GetRegisterName(reg, 8);
// Adjust the operand size to 8-bit
destinationOperand.Size = 8;
// Handle the r/m operand based on mod field
string rmOperand;
// Create the source register operand
var sourceOperand = OperandFactory.CreateRegisterOperand(reg, 8);
if (mod == 3) // Register-to-register
{
// Direct register addressing
rmOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
else // Memory addressing
{
// Replace "dword ptr" with "byte ptr" for 8-bit operands
rmOperand = destOperand.Replace("dword ptr", "byte ptr");
}
// Set the operands (r/m8, r8 format)
instruction.Operands = $"{rmOperand}, {regOperand}";
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}