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.Mov;
/// <summary>
@ -8,11 +10,9 @@ public class MovEaxMoffsHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the MovEaxMoffsHandler 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 MovEaxMoffsHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public MovEaxMoffsHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,25 +34,33 @@ public class MovEaxMoffsHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Set the instruction type
instruction.Type = InstructionType.Mov;
// Get the operand size and register name
int operandSize = (opcode == 0xA0)
? 8
: 32;
string regName = ModRMDecoder.GetRegisterName(RegisterIndex.A, operandSize);
// Get the operand size based on the opcode
int operandSize = (opcode == 0xA0) ? 8 : 32;
// Read the memory offset
uint offset = Decoder.ReadUInt32();
if (Decoder.GetPosition() > Length)
if (!Decoder.CanReadUInt())
{
return false;
}
// Set the operands
instruction.Operands = $"{regName}, [0x{offset:X}]";
uint offset = Decoder.ReadUInt32();
// Create the destination register operand (EAX or AL)
var destinationOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, operandSize);
// Create the source memory operand
// For MOV EAX, moffs32 or MOV AL, moffs8, the memory operand is a direct memory reference
var sourceOperand = OperandFactory.CreateDirectMemoryOperand(offset, operandSize);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Mov;
/// <summary>
@ -8,11 +10,9 @@ public class MovMemRegHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the MovMemRegHandler 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 MovMemRegHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public MovMemRegHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class MovMemRegHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Set the instruction type
instruction.Type = InstructionType.Mov;
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
@ -48,21 +48,23 @@ public class MovMemRegHandler : InstructionHandler
int operandSize = operandSize32 ? 32 : 8;
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// For MOV r/m32, r32 (0x89) or MOV r/m8, r8 (0x88):
// - 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 based on size
string regName = ModRMDecoder.GetRegisterName(reg, operandSize);
// Adjust the operand size based on the opcode
destinationOperand.Size = operandSize;
// For mod == 3, both operands are registers
if (mod == 3)
{
string rmRegName = ModRMDecoder.GetRegisterName(rm, operandSize);
instruction.Operands = $"{rmRegName}, {regName}";
}
else // Memory operand
{
instruction.Operands = $"{memOperand}, {regName}";
}
// Create the source register operand
var sourceOperand = OperandFactory.CreateRegisterOperand(reg, operandSize);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Mov;
/// <summary>
@ -8,11 +10,9 @@ public class MovMoffsEaxHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the MovMoffsEaxHandler 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 MovMoffsEaxHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public MovMoffsEaxHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,23 +34,34 @@ public class MovMoffsEaxHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Set the instruction type
instruction.Type = InstructionType.Mov;
// Get the operand size and register name
// Get the operand size based on the opcode
int operandSize = opcode == 0xA2 ? 8 : 32;
string regName = ModRMDecoder.GetRegisterName(RegisterIndex.A, operandSize);
// Read the memory offset
uint offset = Decoder.ReadUInt32();
if (Decoder.GetPosition() > Length)
// Fixed bug: Changed from if (Decoder.CanReadUInt()) to if (!Decoder.CanReadUInt())
if (!Decoder.CanReadUInt())
{
return false;
}
// Set the operands
instruction.Operands = $"[0x{offset:X}], {regName}";
uint offset = Decoder.ReadUInt32();
// Create the destination memory operand
// For MOV moffs32, EAX or MOV moffs8, AL, the memory operand is a direct memory reference
var destinationOperand = OperandFactory.CreateDirectMemoryOperand(offset, operandSize);
// Create the source register operand (EAX or AL)
var sourceOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, operandSize);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,3 +1,5 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Mov;
/// <summary>
@ -8,11 +10,9 @@ public class MovRegImm32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the MovRegImm32Handler 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 MovRegImm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public MovRegImm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,22 +34,32 @@ public class MovRegImm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Set the instruction type
instruction.Type = InstructionType.Mov;
// Register is encoded in the low 3 bits of the opcode
RegisterIndex reg = (RegisterIndex) (opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 32);
RegisterIndex reg = (RegisterIndex)(opcode & 0x07);
// Read the immediate value
uint imm32 = Decoder.ReadUInt32();
if (Decoder.GetPosition() > Length)
if (!Decoder.CanReadUInt())
{
return false;
}
// Set the operands
instruction.Operands = $"{regName}, 0x{imm32:X}";
uint imm32 = Decoder.ReadUInt32();
// Create the destination register operand
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, 32);
// 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.Mov;
/// <summary>
@ -8,11 +10,9 @@ public class MovRegImm8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the MovRegImm8Handler 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 MovRegImm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public MovRegImm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,23 +34,32 @@ public class MovRegImm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Set the instruction type
instruction.Type = InstructionType.Mov;
// Register is encoded in the low 3 bits of the opcode
RegisterIndex reg = (RegisterIndex) (opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 8);
RegisterIndex reg = (RegisterIndex)(opcode & 0x07);
// Read the immediate value
byte imm8 = Decoder.ReadByte();
if (Decoder.GetPosition() > Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Set the operands
instruction.Operands = $"{regName}, 0x{imm8:X2}";
byte imm8 = Decoder.ReadByte();
// Create the destination register operand
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, 8);
// 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.Mov;
/// <summary>
@ -8,11 +10,9 @@ public class MovRegMemHandler : InstructionHandler
/// <summary>
/// Initializes a new instance of the MovRegMemHandler 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 MovRegMemHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public MovRegMemHandler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class MovRegMemHandler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Set the instruction type
instruction.Type = InstructionType.Mov;
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
@ -47,14 +47,23 @@ public class MovRegMemHandler : InstructionHandler
int operandSize = (opcode & 0x01) != 0 ? 32 : 8;
// Use ModRMDecoder to decode the ModR/M byte
var (mod, reg, rm, rmOperand) = ModRMDecoder.ReadModRM();
// For MOV r32, r/m32 (0x8B) or MOV r8, r/m8 (0x8A):
// - 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 based on size
string regName = ModRMDecoder.GetRegisterName(reg, operandSize);
// Adjust the operand size based on the opcode
sourceOperand.Size = operandSize;
// Set the operands - register is the destination, r/m is the source (for 0x8B)
// This matches the correct x86 instruction format: MOV r32, r/m32
instruction.Operands = $"{regName}, {rmOperand}";
// Create the destination register operand
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, operandSize);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}

View File

@ -1,5 +1,7 @@
namespace X86Disassembler.X86.Handlers.Mov;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for MOV r/m32, imm32 instruction (0xC7)
/// </summary>
@ -8,11 +10,9 @@ public class MovRm32Imm32Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the MovRm32Imm32Handler 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 MovRm32Imm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public MovRm32Imm32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class MovRm32Imm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Set the instruction type
instruction.Type = InstructionType.Mov;
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
@ -44,7 +44,7 @@ public class MovRm32Imm32Handler : InstructionHandler
}
// Use ModRMDecoder to decode the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM(false);
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM(false);
// MOV r/m32, imm32 only uses reg=0
if (reg != 0)
@ -58,9 +58,18 @@ public class MovRm32Imm32Handler : InstructionHandler
return false;
}
// Read the immediate dword and format the operands
// Read the immediate dword and create the operands
uint imm32 = Decoder.ReadUInt32();
instruction.Operands = $"{operand}, 0x{imm32:X8}";
// Create the 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.Mov;
/// <summary>
@ -8,11 +10,9 @@ public class MovRm8Imm8Handler : InstructionHandler
/// <summary>
/// Initializes a new instance of the MovRm8Imm8Handler 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 MovRm8Imm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
public MovRm8Imm8Handler(InstructionDecoder decoder)
: base(decoder)
{
}
@ -34,8 +34,8 @@ public class MovRm8Imm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Set the instruction type
instruction.Type = InstructionType.Mov;
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
@ -44,7 +44,10 @@ public class MovRm8Imm8Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For MOV r/m8, imm8 (0xC6):
// - 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();
// MOV r/m8, imm8 only uses reg=0
if (reg != 0)
@ -52,17 +55,8 @@ public class MovRm8Imm8Handler : InstructionHandler
return false;
}
// 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);
}
else
{
// Replace the size prefix with "byte ptr" for memory operands
destOperand = destOperand.Replace("dword ptr", "byte ptr");
}
// Adjust the operand size to 8-bit
destinationOperand.Size = 8;
// Read the immediate value
if (!Decoder.CanReadByte())
@ -72,8 +66,15 @@ public class MovRm8Imm8Handler : InstructionHandler
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;
}