2025-04-14 22:08:50 +03:00
|
|
|
using X86Disassembler.X86.Operands;
|
|
|
|
|
2025-04-12 23:24:42 +03:00
|
|
|
namespace X86Disassembler.X86.Handlers.FloatingPoint;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handler for floating-point load/store float64 operations (DD opcode)
|
|
|
|
/// </summary>
|
2025-04-13 23:06:52 +03:00
|
|
|
public class LoadStoreFloat64Handler : InstructionHandler
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
2025-04-13 23:52:00 +03:00
|
|
|
// Memory operand mnemonics for DD opcode - load/store float64
|
|
|
|
private static readonly string[] MemoryMnemonics =
|
2025-04-12 23:24:42 +03:00
|
|
|
[
|
2025-04-13 23:52:00 +03:00
|
|
|
"fld", // 0
|
|
|
|
"??", // 1
|
|
|
|
"fst", // 2
|
|
|
|
"fstp", // 3
|
|
|
|
"frstor", // 4
|
|
|
|
"??", // 5
|
|
|
|
"fnsave", // 6
|
|
|
|
"fnstsw" // 7
|
2025-04-12 23:24:42 +03:00
|
|
|
];
|
2025-04-13 23:52:00 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Memory operand instruction types for DD opcode
|
|
|
|
private static readonly InstructionType[] MemoryInstructionTypes =
|
|
|
|
[
|
2025-04-15 02:29:32 +03:00
|
|
|
InstructionType.Fld, // 0 - fld qword ptr [r/m]
|
|
|
|
InstructionType.Unknown, // 1 - (reserved)
|
|
|
|
InstructionType.Fst, // 2 - fst qword ptr [r/m]
|
|
|
|
InstructionType.Fstp, // 3 - fstp qword ptr [r/m]
|
|
|
|
InstructionType.Frstor, // 4 - frstor [r/m]
|
|
|
|
InstructionType.Unknown, // 5 - (reserved)
|
|
|
|
InstructionType.Fnsave, // 6 - fnsave [r/m]
|
|
|
|
InstructionType.Fnstsw // 7 - fnstsw [r/m]
|
2025-04-14 22:08:50 +03:00
|
|
|
];
|
|
|
|
|
2025-04-13 23:52:00 +03:00
|
|
|
// Register-register operations mapping (mod=3)
|
2025-04-14 22:08:50 +03:00
|
|
|
private static readonly Dictionary<(RegisterIndex Reg, RegisterIndex Rm), (InstructionType Type, FpuRegisterIndex OperandIndex)> RegisterOperations = new()
|
2025-04-13 23:52:00 +03:00
|
|
|
{
|
|
|
|
// FFREE ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.A, RegisterIndex.A), (InstructionType.Ffree, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.C), (InstructionType.Ffree, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.D), (InstructionType.Ffree, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.B), (InstructionType.Ffree, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.Sp), (InstructionType.Ffree, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.Bp), (InstructionType.Ffree, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.Si), (InstructionType.Ffree, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.Di), (InstructionType.Ffree, FpuRegisterIndex.ST7) },
|
2025-04-13 23:52:00 +03:00
|
|
|
|
|
|
|
// FST ST(i)
|
2025-04-14 22:08:50 +03:00
|
|
|
{ (RegisterIndex.C, RegisterIndex.A), (InstructionType.Fst, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.C), (InstructionType.Fst, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.D), (InstructionType.Fst, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.B), (InstructionType.Fst, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.Sp), (InstructionType.Fst, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.Bp), (InstructionType.Fst, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.Si), (InstructionType.Fst, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.Di), (InstructionType.Fst, FpuRegisterIndex.ST7) },
|
2025-04-13 23:52:00 +03:00
|
|
|
|
|
|
|
// FSTP ST(i)
|
2025-04-14 22:08:50 +03:00
|
|
|
{ (RegisterIndex.D, RegisterIndex.A), (InstructionType.Fstp, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.C), (InstructionType.Fstp, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.D), (InstructionType.Fstp, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.B), (InstructionType.Fstp, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.Sp), (InstructionType.Fstp, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.Bp), (InstructionType.Fstp, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.Si), (InstructionType.Fstp, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.Di), (InstructionType.Fstp, FpuRegisterIndex.ST7) },
|
2025-04-13 23:52:00 +03:00
|
|
|
|
|
|
|
// FUCOM ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.Si, RegisterIndex.A), (InstructionType.Fucom, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.Si, RegisterIndex.C), (InstructionType.Fucom, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.Si, RegisterIndex.D), (InstructionType.Fucom, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.Si, RegisterIndex.B), (InstructionType.Fucom, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.Si, RegisterIndex.Sp), (InstructionType.Fucom, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.Si, RegisterIndex.Bp), (InstructionType.Fucom, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.Si, RegisterIndex.Si), (InstructionType.Fucom, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.Si, RegisterIndex.Di), (InstructionType.Fucom, FpuRegisterIndex.ST7) },
|
2025-04-13 23:52:00 +03:00
|
|
|
|
|
|
|
// FUCOMP ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.Di, RegisterIndex.A), (InstructionType.Fucomp, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.C), (InstructionType.Fucomp, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.D), (InstructionType.Fucomp, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.B), (InstructionType.Fucomp, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.Sp), (InstructionType.Fucomp, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.Bp), (InstructionType.Fucomp, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.Si), (InstructionType.Fucomp, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.Di), (InstructionType.Fucomp, FpuRegisterIndex.ST7) }
|
2025-04-13 23:52:00 +03:00
|
|
|
};
|
2025-04-12 23:24:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the LoadStoreFloat64Handler class
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
public LoadStoreFloat64Handler(InstructionDecoder decoder)
|
|
|
|
: base(decoder)
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if this handler can decode the given opcode
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="opcode">The opcode to check</param>
|
|
|
|
/// <returns>True if this handler can decode the opcode</returns>
|
|
|
|
public override bool CanHandle(byte opcode)
|
|
|
|
{
|
|
|
|
return opcode == 0xDD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-14 22:08:50 +03:00
|
|
|
/// Decodes a floating point instruction with the DD opcode
|
2025-04-12 23:24:42 +03:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="opcode">The opcode of the instruction</param>
|
|
|
|
/// <param name="instruction">The instruction object to populate</param>
|
|
|
|
/// <returns>True if the instruction was successfully decoded</returns>
|
|
|
|
public override bool Decode(byte opcode, Instruction instruction)
|
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
// Check if we have enough bytes for the ModR/M byte
|
2025-04-13 23:52:00 +03:00
|
|
|
if (!Decoder.CanReadByte())
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the ModR/M byte
|
2025-04-16 18:30:17 +03:00
|
|
|
var (mod, reg, rm, rawMemoryOperand) = ModRMDecoder.ReadModRM();
|
2025-04-12 23:24:42 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Set the instruction type based on the mod and reg fields
|
2025-04-12 23:24:42 +03:00
|
|
|
if (mod != 3) // Memory operand
|
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
instruction.Type = MemoryInstructionTypes[(int)reg];
|
2025-04-13 23:52:00 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// For memory operands, the instruction depends on the reg field
|
|
|
|
switch (reg)
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
case RegisterIndex.A: // FLD m64real
|
|
|
|
case RegisterIndex.C: // FST m64real
|
|
|
|
case RegisterIndex.D: // FSTP m64real
|
2025-04-16 18:30:17 +03:00
|
|
|
// Create a new memory operand with 64-bit size using the appropriate factory method
|
|
|
|
Operand memoryOperand;
|
|
|
|
|
|
|
|
if (rawMemoryOperand is DirectMemoryOperand directMemory)
|
|
|
|
{
|
|
|
|
memoryOperand = OperandFactory.CreateDirectMemoryOperand(directMemory.Address, 64);
|
|
|
|
}
|
|
|
|
else if (rawMemoryOperand is BaseRegisterMemoryOperand baseRegMemory)
|
|
|
|
{
|
|
|
|
memoryOperand = OperandFactory.CreateBaseRegisterMemoryOperand(baseRegMemory.BaseRegister, 64);
|
|
|
|
}
|
|
|
|
else if (rawMemoryOperand is DisplacementMemoryOperand dispMemory)
|
|
|
|
{
|
|
|
|
memoryOperand = OperandFactory.CreateDisplacementMemoryOperand(dispMemory.BaseRegister, dispMemory.Displacement, 64);
|
|
|
|
}
|
|
|
|
else if (rawMemoryOperand is ScaledIndexMemoryOperand scaledMemory)
|
|
|
|
{
|
|
|
|
memoryOperand = OperandFactory.CreateScaledIndexMemoryOperand(scaledMemory.IndexRegister, scaledMemory.Scale, scaledMemory.BaseRegister, scaledMemory.Displacement, 64);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memoryOperand = rawMemoryOperand;
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Set the structured operands
|
|
|
|
instruction.StructuredOperands =
|
|
|
|
[
|
|
|
|
memoryOperand
|
|
|
|
];
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// For unsupported instructions, just set the type to Unknown
|
|
|
|
instruction.Type = InstructionType.Unknown;
|
|
|
|
return true;
|
2025-04-12 23:24:42 +03:00
|
|
|
}
|
|
|
|
}
|
2025-04-14 22:08:50 +03:00
|
|
|
else // Register operand (mod == 3)
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
// Look up the instruction type in the register operations dictionary
|
2025-04-13 23:52:00 +03:00
|
|
|
if (RegisterOperations.TryGetValue((reg, rm), out var operation))
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
instruction.Type = operation.Type;
|
|
|
|
|
|
|
|
// Create the FPU register operand
|
|
|
|
var fpuRegisterOperand = OperandFactory.CreateFPURegisterOperand(operation.OperandIndex);
|
|
|
|
|
|
|
|
// Set the structured operands
|
|
|
|
instruction.StructuredOperands =
|
|
|
|
[
|
|
|
|
fpuRegisterOperand
|
|
|
|
];
|
|
|
|
|
|
|
|
return true;
|
2025-04-12 23:24:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
return false;
|
2025-04-12 23:24:42 +03:00
|
|
|
}
|
|
|
|
}
|