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,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
@ -8,11 +10,9 @@ public class AndAlImmHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndAlImmHandler 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 AndAlImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndAlImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,21 +34,30 @@ public class AndAlImmHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
// Create the destination register operand (AL)
var destinationOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 8);
// Read immediate value
if (!Decoder.CanReadByte())
{
instruction.Operands = "al, ??";
return true;
return false;
}
// Read immediate value
byte imm8 = Decoder.ReadByte();
// Set operands
instruction.Operands = $"al, 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,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
@ -8,11 +10,9 @@ public class AndEaxImmHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndEaxImmHandler 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 AndEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndEaxImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,21 +34,30 @@ public class AndEaxImmHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
// Create the destination register operand (EAX)
var destinationOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32);
// Read immediate value
if (!Decoder.CanReadUInt())
{
instruction.Operands = "eax, ??";
return true;
return false;
}
// Read immediate value
uint imm32 = Decoder.ReadUInt32();
// Set operands
instruction.Operands = $"eax, 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,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.And;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for AND r/m32, imm32 instruction (0x81 /4)
/// </summary>
@ -8,11 +10,9 @@ public class AndImmToRm32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndImmToRm32Handler 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 AndImmToRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndImmToRm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -27,11 +27,10 @@ public class AndImmToRm32Handler : InstructionHandler
return false;
// Check if the reg field of the ModR/M byte is 4 (AND)
int position = Decoder.GetPosition();
if (Decoder.CanReadByte())
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte modRM = Decoder.PeakByte();
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 4; // 4 = AND
@ -45,8 +44,8 @@ public class AndImmToRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
if (!Decoder.CanReadByte())
{
@ -65,12 +64,15 @@ public class AndImmToRm32Handler : 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}";
// Create the immediate operand
var immOperand = OperandFactory.CreateImmediateOperand(imm);
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
immOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
@ -8,11 +10,9 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndImmToRm32SignExtendedHandler 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 AndImmToRm32SignExtendedHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndImmToRm32SignExtendedHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -29,14 +29,13 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
}
// Check if we have enough bytes to read the ModR/M byte
int position = Decoder.GetPosition();
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte to check the reg field (bits 5-3)
byte modRM = CodeBuffer[position];
byte modRM = Decoder.PeakByte();
int reg = (modRM >> 3) & 0x7;
// reg = 4 means AND operation
@ -51,11 +50,14 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// For AND r/m32, imm8 (sign-extended) (0x83 /4):
// - The r/m field with mod specifies the destination operand (register or memory)
// - The immediate value is the source operand (sign-extended from 8 to 32 bits)
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
if (!Decoder.CanReadByte())
{
@ -63,36 +65,17 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
}
// Read the immediate value as a signed byte and automatically sign-extend it to int
int imm = (sbyte) Decoder.ReadByte();
int imm = (sbyte)Decoder.ReadByte();
// Format the destination operand based on addressing mode
string destOperand;
if (mod == 3) // Register addressing mode
{
// Get 32-bit register name
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Memory addressing mode
{
// Memory operand already includes dword ptr prefix
destOperand = memOperand;
}
// Format the immediate value
string immStr;
if (imm < 0)
{
// For negative values, use the full 32-bit representation
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}";
// Create the source immediate operand with the sign-extended value
var sourceOperand = OperandFactory.CreateImmediateOperand(imm, 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.And;
/// <summary>
@ -8,11 +10,9 @@ public class AndImmToRm8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndImmToRm8Handler 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 AndImmToRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndImmToRm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -29,14 +29,13 @@ public class AndImmToRm8Handler : InstructionHandler
}
// Check if we have enough bytes to read the ModR/M byte
if (Decoder.CanReadByte())
if (!Decoder.CanReadByte())
{
return false;
}
int position = Decoder.GetPosition();
// Read the ModR/M byte to check the reg field (bits 5-3)
byte modRM = CodeBuffer[position];
byte modRM = Decoder.PeakByte();
int reg = (modRM >> 3) & 0x7;
// reg = 4 means AND operation
@ -51,11 +50,17 @@ public class AndImmToRm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// For AND r/m8, imm8 (0x80 /4):
// - 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;
if (!Decoder.CanReadByte())
{
@ -65,24 +70,15 @@ public class AndImmToRm8Handler : InstructionHandler
// Read the immediate value
byte imm8 = Decoder.ReadByte();
// Format the destination operand based on addressing mode
string destOperand;
if (mod == 3) // Register addressing mode
{
// Get 8-bit register name
destOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
else // Memory addressing mode
{
// Add byte ptr prefix for memory operands
destOperand = $"byte ptr {memOperand}";
}
// Format the immediate value
string immStr = $"0x{imm8:X2}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
// Create the source immediate operand
var sourceOperand = OperandFactory.CreateImmediateOperand(imm8, 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.And;
/// <summary>
@ -8,11 +10,9 @@ public class AndImmWithRm32Handler : InstructionHandler
/// <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)
public AndImmWithRm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -27,11 +27,10 @@ public class AndImmWithRm32Handler : InstructionHandler
return false;
// Check if the reg field of the ModR/M byte is 4 (AND)
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 == 4; // 4 = AND
@ -45,15 +44,15 @@ public class AndImmWithRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// For AND r/m32, imm32 (0x81 /4):
// - 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();
// Get the position after decoding the ModR/M byte
int position = Decoder.GetPosition();
// Check if we have enough bytes for the immediate value
if (!Decoder.CanReadUInt())
{
@ -63,24 +62,15 @@ public class AndImmWithRm32Handler : InstructionHandler
// Read the immediate value
uint imm32 = Decoder.ReadUInt32();
// Format the destination operand based on addressing mode
string destOperand;
if (mod == 3) // Register addressing mode
{
// Get 32-bit register name
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Memory addressing mode
{
// Memory operand already includes dword ptr prefix
destOperand = memOperand;
}
// Create the source immediate operand
var sourceOperand = OperandFactory.CreateImmediateOperand(imm32, 32);
// Format the immediate value
string immStr = $"0x{imm32:X8}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
@ -8,11 +10,9 @@ public class AndMemRegHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndMemRegHandler 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 AndMemRegHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndMemRegHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class AndMemRegHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
if (!Decoder.CanReadByte())
{
@ -43,18 +43,20 @@ public class AndMemRegHandler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// For AND r/m32, r32 (0x21):
// - 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();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For mod == 3, both operands are registers
if (mod == 3)
{
memOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
instruction.Operands = $"{memOperand}, {regName}";
// Create the source register operand
var sourceOperand = OperandFactory.CreateRegisterOperand(reg, 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.And;
/// <summary>
@ -8,11 +10,9 @@ public class AndR32Rm32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndR32Rm32Handler 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 AndR32Rm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndR32Rm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class AndR32Rm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
if (!Decoder.CanReadByte())
{
@ -43,18 +43,20 @@ public class AndR32Rm32Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// For AND r32, r/m32 (0x23):
// - 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();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For mod == 3, both operands are registers
if (mod == 3)
{
memOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
instruction.Operands = $"{regName}, {memOperand}";
// Create the destination register operand
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, 32);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.And;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for AND r8, r/m8 instruction (0x22)
/// </summary>
@ -8,11 +10,9 @@ public class AndR8Rm8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndR8Rm8Handler 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 AndR8Rm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndR8Rm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class AndR8Rm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
if (!Decoder.CanReadByte())
{
@ -43,20 +43,38 @@ public class AndR8Rm8Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, srcOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
// Create the destination register operand
var destOperand = OperandFactory.CreateRegisterOperand(reg, 8);
// For mod == 3, both operands are registers
if (mod == 3)
{
string rmRegName = ModRMDecoder.GetRegisterName(rm, 8);
instruction.Operands = $"{regName}, {rmRegName}";
// Create a register operand for the r/m field
var rmOperand = OperandFactory.CreateRegisterOperand(rm, 8);
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
rmOperand
];
}
else // Memory operand
{
instruction.Operands = $"{regName}, byte ptr {memOperand}";
// Ensure memory operand has the correct size (8-bit)
if (srcOperand is MemoryOperand memOperand)
{
memOperand.Size = 8;
}
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
srcOperand
];
}
return true;

View File

@ -1,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.And;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for AND r/m8, r8 instruction (0x20)
/// </summary>
@ -8,11 +10,9 @@ public class AndRm8R8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the AndRm8R8Handler 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 AndRm8R8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public AndRm8R8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class AndRm8R8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Set the instruction type
instruction.Type = InstructionType.And;
if (!Decoder.CanReadByte())
{
@ -43,20 +43,38 @@ public class AndRm8R8Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
// Create the source register operand
var srcOperand = OperandFactory.CreateRegisterOperand(reg, 8);
// For mod == 3, both operands are registers
if (mod == 3)
{
string rmRegName = ModRMDecoder.GetRegisterName(rm, 8);
instruction.Operands = $"{rmRegName}, {regName}";
// Create a register operand for the r/m field
var rmOperand = OperandFactory.CreateRegisterOperand(rm, 8);
// Set the structured operands
instruction.StructuredOperands =
[
rmOperand,
srcOperand
];
}
else // Memory operand
{
instruction.Operands = $"byte ptr {memOperand}, {regName}";
// Ensure memory operand has the correct size (8-bit)
if (destOperand is MemoryOperand memOperand)
{
memOperand.Size = 8;
}
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
srcOperand
];
}
return true;