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.Cmp;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for CMP AL, imm8 instruction (0x3C)
/// </summary>
@ -8,11 +10,9 @@ public class CmpAlImmHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the CmpAlImmHandler 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 CmpAlImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public CmpAlImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class CmpAlImmHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "cmp";
// Set the instruction type
instruction.Type = InstructionType.Cmp;
if (!Decoder.CanReadByte())
{
@ -45,8 +45,18 @@ public class CmpAlImmHandler : 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,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Cmp;
/// <summary>
@ -8,11 +10,9 @@ public class CmpImmWithRm32Handler : InstructionHandler
/// <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)
public CmpImmWithRm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -30,7 +30,7 @@ public class CmpImmWithRm32Handler : InstructionHandler
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte modRM = Decoder.PeakByte();
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP
@ -44,11 +44,11 @@ public class CmpImmWithRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "cmp";
// Set the instruction type
instruction.Type = InstructionType.Cmp;
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
// Read the immediate value
if (!Decoder.CanReadUInt())
@ -57,19 +57,16 @@ public class CmpImmWithRm32Handler : InstructionHandler
}
uint imm32 = Decoder.ReadUInt32();
// Format the destination operand based on addressing mode
if (mod == 3) // Register addressing mode
{
// Get 32-bit register name
memOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
// Format the immediate value
string immStr = $"0x{imm32:X8}";
// Set the operands
instruction.Operands = $"{memOperand}, {immStr}";
// 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.Cmp;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for CMP r/m32, imm8 (sign-extended) instruction (0x83 /7)
/// </summary>
@ -8,11 +10,9 @@ public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
/// <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)
public CmpImmWithRm32SignExtendedHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -27,11 +27,10 @@ public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
return false;
// Check if the reg field of the ModR/M byte is 7 (CMP)
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 == 7; // 7 = CMP
@ -45,8 +44,8 @@ public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "cmp";
// Set the instruction type
instruction.Type = InstructionType.Cmp;
if (!Decoder.CanReadByte())
{
@ -64,9 +63,16 @@ public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
// Read the immediate value as a signed byte and sign-extend it
sbyte imm8 = (sbyte) Decoder.ReadByte();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{(uint) imm8:X2}";
// 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.Cmp;
/// <summary>
@ -8,11 +10,9 @@ public class CmpImmWithRm8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the CmpImmWithRm8Handler 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 CmpImmWithRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public CmpImmWithRm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -27,11 +27,10 @@ public class CmpImmWithRm8Handler : InstructionHandler
return false;
// Check if the reg field of the ModR/M byte is 7 (CMP)
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 == 7; // 7 = CMP
@ -45,11 +44,14 @@ public class CmpImmWithRm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "cmp";
// Set the instruction type
instruction.Type = InstructionType.Cmp;
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
// 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())
@ -59,34 +61,16 @@ public class CmpImmWithRm8Handler : InstructionHandler
// Read the immediate byte
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
{
// Ensure we have the correct size prefix (byte ptr)
if (memOperand.Contains("dword ptr") || memOperand.Contains("qword ptr"))
{
// Replace the size prefix with "byte ptr"
destOperand = memOperand.Replace(memOperand.StartsWith("dword") ? "dword ptr " : "qword ptr ", "byte ptr ");
}
else
{
// Add the byte ptr prefix if it doesn't have one
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.Cmp;
/// <summary>
@ -8,11 +10,9 @@ public class CmpR32Rm32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the CmpR32Rm32Handler 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 CmpR32Rm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public CmpR32Rm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -39,23 +39,21 @@ public class CmpR32Rm32Handler : InstructionHandler
return false;
}
// Set the instruction type
instruction.Type = InstructionType.Cmp;
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the mnemonic
instruction.Mnemonic = "cmp";
// Get the register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For register operands, set the operand
if (mod == 3)
{
// Register operand
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
instruction.Operands = $"{regName}, {destOperand}";
var (mod, reg, rm, sourceOperand) = ModRMDecoder.ReadModRM();
// Create the destination register operand (32-bit)
var destinationOperand = OperandFactory.CreateRegisterOperand((RegisterIndex)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.Cmp;
/// <summary>
@ -8,11 +10,9 @@ public class CmpRm32R32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the CmpRm32R32Handler 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 CmpRm32R32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public CmpRm32R32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class CmpRm32R32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "cmp";
// Set the instruction type
instruction.Type = InstructionType.Cmp;
if (!Decoder.CanReadByte())
{
@ -43,27 +43,20 @@ public class CmpRm32R32Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For CMP r/m32, r32 (0x39):
// - 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 the register name for the reg field
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Create the source register operand
var sourceOperand = OperandFactory.CreateRegisterOperand(reg, 32);
// Use the destOperand directly from ModRMDecoder
string rmOperand = destOperand;
// If it's a direct register operand, we need to remove the size prefix
if (mod == 3)
{
rmOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else if (rmOperand.StartsWith("dword ptr "))
{
// Remove the "dword ptr " prefix as we'll handle the operands differently
rmOperand = rmOperand.Substring(10);
}
// Set the operands
instruction.Operands = $"{rmOperand}, {regName}";
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}