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:
@ -1,3 +1,5 @@
|
||||
using X86Disassembler.X86.Operands;
|
||||
|
||||
namespace X86Disassembler.X86.Handlers.FloatingPoint;
|
||||
|
||||
/// <summary>
|
||||
@ -5,95 +7,93 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
|
||||
/// </summary>
|
||||
public class LoadStoreInt32Handler : InstructionHandler
|
||||
{
|
||||
// Memory operand mnemonics for DB opcode - load/store int32, misc
|
||||
private static readonly string[] MemoryMnemonics =
|
||||
// Memory operand instruction types for DB opcode - load/store int32, misc
|
||||
private static readonly InstructionType[] MemoryInstructionTypes =
|
||||
[
|
||||
"fild", // 0 - 32-bit integer
|
||||
"??", // 1
|
||||
"fist", // 2 - 32-bit integer
|
||||
"fistp", // 3 - 32-bit integer
|
||||
"??", // 4
|
||||
"fld", // 5 - 80-bit extended precision
|
||||
"??", // 6
|
||||
"fstp" // 7 - 80-bit extended precision
|
||||
InstructionType.Unknown, // fild - not in enum
|
||||
InstructionType.Unknown, // ??
|
||||
InstructionType.Unknown, // fist - not in enum
|
||||
InstructionType.Unknown, // fistp - not in enum
|
||||
InstructionType.Unknown, // ??
|
||||
InstructionType.Fld, // fld - 80-bit extended precision
|
||||
InstructionType.Unknown, // ??
|
||||
InstructionType.Fstp // fstp - 80-bit extended precision
|
||||
];
|
||||
|
||||
// Register-register operations mapping (mod=3)
|
||||
private static readonly Dictionary<(RegisterIndex Reg, RegisterIndex Rm), (string Mnemonic, string Operands)> RegisterOperations = new()
|
||||
private static readonly Dictionary<(RegisterIndex Reg, RegisterIndex Rm), (InstructionType Type, FpuRegisterIndex DestIndex, FpuRegisterIndex? SrcIndex)> RegisterOperations = new()
|
||||
{
|
||||
// FCMOVNB ST(0), ST(i)
|
||||
{ (RegisterIndex.A, RegisterIndex.A), ("fcmovnb", "st(0), st(0)") },
|
||||
{ (RegisterIndex.A, RegisterIndex.C), ("fcmovnb", "st(0), st(1)") },
|
||||
{ (RegisterIndex.A, RegisterIndex.D), ("fcmovnb", "st(0), st(2)") },
|
||||
{ (RegisterIndex.A, RegisterIndex.B), ("fcmovnb", "st(0), st(3)") },
|
||||
{ (RegisterIndex.A, RegisterIndex.Sp), ("fcmovnb", "st(0), st(4)") },
|
||||
{ (RegisterIndex.A, RegisterIndex.Bp), ("fcmovnb", "st(0), st(5)") },
|
||||
{ (RegisterIndex.A, RegisterIndex.Si), ("fcmovnb", "st(0), st(6)") },
|
||||
{ (RegisterIndex.A, RegisterIndex.Di), ("fcmovnb", "st(0), st(7)") },
|
||||
{ (RegisterIndex.A, RegisterIndex.A), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
||||
{ (RegisterIndex.A, RegisterIndex.C), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
||||
{ (RegisterIndex.A, RegisterIndex.D), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
||||
{ (RegisterIndex.A, RegisterIndex.B), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
||||
{ (RegisterIndex.A, RegisterIndex.Sp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
||||
{ (RegisterIndex.A, RegisterIndex.Bp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
||||
{ (RegisterIndex.A, RegisterIndex.Si), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
||||
{ (RegisterIndex.A, RegisterIndex.Di), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
||||
|
||||
// FCMOVNE ST(0), ST(i)
|
||||
{ (RegisterIndex.B, RegisterIndex.A), ("fcmovne", "st(0), st(0)") },
|
||||
{ (RegisterIndex.B, RegisterIndex.C), ("fcmovne", "st(0), st(1)") },
|
||||
{ (RegisterIndex.B, RegisterIndex.D), ("fcmovne", "st(0), st(2)") },
|
||||
{ (RegisterIndex.B, RegisterIndex.B), ("fcmovne", "st(0), st(3)") },
|
||||
{ (RegisterIndex.B, RegisterIndex.Sp), ("fcmovne", "st(0), st(4)") },
|
||||
{ (RegisterIndex.B, RegisterIndex.Bp), ("fcmovne", "st(0), st(5)") },
|
||||
{ (RegisterIndex.B, RegisterIndex.Si), ("fcmovne", "st(0), st(6)") },
|
||||
{ (RegisterIndex.B, RegisterIndex.Di), ("fcmovne", "st(0), st(7)") },
|
||||
{ (RegisterIndex.B, RegisterIndex.A), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
||||
{ (RegisterIndex.B, RegisterIndex.C), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
||||
{ (RegisterIndex.B, RegisterIndex.D), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
||||
{ (RegisterIndex.B, RegisterIndex.B), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
||||
{ (RegisterIndex.B, RegisterIndex.Sp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
||||
{ (RegisterIndex.B, RegisterIndex.Bp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
||||
{ (RegisterIndex.B, RegisterIndex.Si), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
||||
{ (RegisterIndex.B, RegisterIndex.Di), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
||||
|
||||
// FCMOVNBE ST(0), ST(i)
|
||||
{ (RegisterIndex.C, RegisterIndex.A), ("fcmovnbe", "st(0), st(0)") },
|
||||
{ (RegisterIndex.C, RegisterIndex.C), ("fcmovnbe", "st(0), st(1)") },
|
||||
{ (RegisterIndex.C, RegisterIndex.D), ("fcmovnbe", "st(0), st(2)") },
|
||||
{ (RegisterIndex.C, RegisterIndex.B), ("fcmovnbe", "st(0), st(3)") },
|
||||
{ (RegisterIndex.C, RegisterIndex.Sp), ("fcmovnbe", "st(0), st(4)") },
|
||||
{ (RegisterIndex.C, RegisterIndex.Bp), ("fcmovnbe", "st(0), st(5)") },
|
||||
{ (RegisterIndex.C, RegisterIndex.Si), ("fcmovnbe", "st(0), st(6)") },
|
||||
{ (RegisterIndex.C, RegisterIndex.Di), ("fcmovnbe", "st(0), st(7)") },
|
||||
{ (RegisterIndex.C, RegisterIndex.A), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
||||
{ (RegisterIndex.C, RegisterIndex.C), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
||||
{ (RegisterIndex.C, RegisterIndex.D), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
||||
{ (RegisterIndex.C, RegisterIndex.B), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
||||
{ (RegisterIndex.C, RegisterIndex.Sp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
||||
{ (RegisterIndex.C, RegisterIndex.Bp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
||||
{ (RegisterIndex.C, RegisterIndex.Si), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
||||
{ (RegisterIndex.C, RegisterIndex.Di), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
||||
|
||||
// FCMOVNU ST(0), ST(i)
|
||||
{ (RegisterIndex.D, RegisterIndex.A), ("fcmovnu", "st(0), st(0)") },
|
||||
{ (RegisterIndex.D, RegisterIndex.C), ("fcmovnu", "st(0), st(1)") },
|
||||
{ (RegisterIndex.D, RegisterIndex.D), ("fcmovnu", "st(0), st(2)") },
|
||||
{ (RegisterIndex.D, RegisterIndex.B), ("fcmovnu", "st(0), st(3)") },
|
||||
{ (RegisterIndex.D, RegisterIndex.Sp), ("fcmovnu", "st(0), st(4)") },
|
||||
{ (RegisterIndex.D, RegisterIndex.Bp), ("fcmovnu", "st(0), st(5)") },
|
||||
{ (RegisterIndex.D, RegisterIndex.Si), ("fcmovnu", "st(0), st(6)") },
|
||||
{ (RegisterIndex.D, RegisterIndex.Di), ("fcmovnu", "st(0), st(7)") },
|
||||
{ (RegisterIndex.D, RegisterIndex.A), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
||||
{ (RegisterIndex.D, RegisterIndex.C), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
||||
{ (RegisterIndex.D, RegisterIndex.D), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
||||
{ (RegisterIndex.D, RegisterIndex.B), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
||||
{ (RegisterIndex.D, RegisterIndex.Sp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
||||
{ (RegisterIndex.D, RegisterIndex.Bp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
||||
{ (RegisterIndex.D, RegisterIndex.Si), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
||||
{ (RegisterIndex.D, RegisterIndex.Di), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
||||
|
||||
// Special cases
|
||||
{ (RegisterIndex.Si, RegisterIndex.C), ("fclex", "") },
|
||||
{ (RegisterIndex.Si, RegisterIndex.D), ("finit", "") },
|
||||
{ (RegisterIndex.Si, RegisterIndex.C), (InstructionType.Unknown, FpuRegisterIndex.ST0, null) }, // fclex
|
||||
{ (RegisterIndex.Si, RegisterIndex.D), (InstructionType.Unknown, FpuRegisterIndex.ST0, null) }, // finit
|
||||
|
||||
// FUCOMI ST(0), ST(i)
|
||||
{ (RegisterIndex.Di, RegisterIndex.A), ("fucomi", "st(0), st(0)") },
|
||||
{ (RegisterIndex.Di, RegisterIndex.C), ("fucomi", "st(0), st(1)") },
|
||||
{ (RegisterIndex.Di, RegisterIndex.D), ("fucomi", "st(0), st(2)") },
|
||||
{ (RegisterIndex.Di, RegisterIndex.B), ("fucomi", "st(0), st(3)") },
|
||||
{ (RegisterIndex.Di, RegisterIndex.Sp), ("fucomi", "st(0), st(4)") },
|
||||
{ (RegisterIndex.Di, RegisterIndex.Bp), ("fucomi", "st(0), st(5)") },
|
||||
{ (RegisterIndex.Di, RegisterIndex.Si), ("fucomi", "st(0), st(6)") },
|
||||
{ (RegisterIndex.Di, RegisterIndex.Di), ("fucomi", "st(0), st(7)") },
|
||||
{ (RegisterIndex.Di, RegisterIndex.A), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
||||
{ (RegisterIndex.Di, RegisterIndex.C), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
||||
{ (RegisterIndex.Di, RegisterIndex.D), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
||||
{ (RegisterIndex.Di, RegisterIndex.B), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
||||
{ (RegisterIndex.Di, RegisterIndex.Sp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
||||
{ (RegisterIndex.Di, RegisterIndex.Bp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
||||
{ (RegisterIndex.Di, RegisterIndex.Si), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
||||
{ (RegisterIndex.Di, RegisterIndex.Di), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
||||
|
||||
// FCOMI ST(0), ST(i)
|
||||
{ (RegisterIndex.Sp, RegisterIndex.A), ("fcomi", "st(0), st(0)") },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.C), ("fcomi", "st(0), st(1)") },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.D), ("fcomi", "st(0), st(2)") },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.B), ("fcomi", "st(0), st(3)") },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.Sp), ("fcomi", "st(0), st(4)") },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.Bp), ("fcomi", "st(0), st(5)") },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.Si), ("fcomi", "st(0), st(6)") },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.Di), ("fcomi", "st(0), st(7)") }
|
||||
{ (RegisterIndex.Sp, RegisterIndex.A), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.C), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.D), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.B), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.Sp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.Bp), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.Si), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
||||
{ (RegisterIndex.Sp, RegisterIndex.Di), (InstructionType.Unknown, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the LoadStoreInt32Handler 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 LoadStoreInt32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
public LoadStoreInt32Handler(InstructionDecoder decoder)
|
||||
: base(decoder)
|
||||
{
|
||||
}
|
||||
|
||||
@ -121,45 +121,65 @@ public class LoadStoreInt32Handler : InstructionHandler
|
||||
}
|
||||
|
||||
// Read the ModR/M byte
|
||||
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
|
||||
var (mod, reg, rm, memoryOperand) = ModRMDecoder.ReadModRM();
|
||||
|
||||
// Handle based on addressing mode
|
||||
if (mod != 3) // Memory operand
|
||||
{
|
||||
// Set the mnemonic based on the reg field
|
||||
instruction.Mnemonic = MemoryMnemonics[(int)reg];
|
||||
// Set the instruction type based on the reg field
|
||||
instruction.Type = MemoryInstructionTypes[(int)reg];
|
||||
|
||||
// Get the base operand without size prefix
|
||||
string baseOperand = memOperand.Replace("dword ptr ", "");
|
||||
|
||||
// Apply the appropriate size prefix based on the operation
|
||||
// Set the size based on the operation
|
||||
if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D) // 32-bit integer operations
|
||||
{
|
||||
// Keep the dword ptr prefix for integer operations
|
||||
instruction.Operands = memOperand;
|
||||
// Keep the default 32-bit size
|
||||
memoryOperand.Size = 32;
|
||||
}
|
||||
else if (reg == RegisterIndex.Di || reg == RegisterIndex.Bp) // 80-bit extended precision operations
|
||||
{
|
||||
instruction.Operands = $"tword ptr {baseOperand}";
|
||||
}
|
||||
else
|
||||
{
|
||||
instruction.Operands = memOperand;
|
||||
// Set to 80-bit for extended precision
|
||||
memoryOperand.Size = 80;
|
||||
}
|
||||
|
||||
// Set the structured operands
|
||||
instruction.StructuredOperands =
|
||||
[
|
||||
memoryOperand
|
||||
];
|
||||
}
|
||||
else // Register operand (ST(i))
|
||||
{
|
||||
// Look up the register operation in our dictionary
|
||||
// Look up the instruction type in the register operations dictionary
|
||||
if (RegisterOperations.TryGetValue((reg, rm), out var operation))
|
||||
{
|
||||
instruction.Mnemonic = operation.Mnemonic;
|
||||
instruction.Operands = operation.Operands;
|
||||
instruction.Type = operation.Type;
|
||||
|
||||
// Create the FPU register operands
|
||||
var destOperand = OperandFactory.CreateFPURegisterOperand(operation.DestIndex);
|
||||
|
||||
// Set the structured operands
|
||||
if (operation.SrcIndex.HasValue)
|
||||
{
|
||||
var srcOperand = OperandFactory.CreateFPURegisterOperand(operation.SrcIndex.Value);
|
||||
instruction.StructuredOperands =
|
||||
[
|
||||
destOperand,
|
||||
srcOperand
|
||||
];
|
||||
}
|
||||
else
|
||||
{
|
||||
instruction.StructuredOperands =
|
||||
[
|
||||
destOperand
|
||||
];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Unknown instruction
|
||||
instruction.Mnemonic = "??";
|
||||
instruction.Operands = "";
|
||||
instruction.Type = InstructionType.Unknown;
|
||||
instruction.StructuredOperands = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user