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 int32 and miscellaneous operations (DB opcode)
|
|
|
|
/// </summary>
|
2025-04-13 23:06:52 +03:00
|
|
|
public class LoadStoreInt32Handler : InstructionHandler
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
// Memory operand instruction types for DB opcode - load/store int32, misc
|
|
|
|
private static readonly InstructionType[] MemoryInstructionTypes =
|
2025-04-12 23:24:42 +03:00
|
|
|
[
|
2025-04-15 02:29:32 +03:00
|
|
|
InstructionType.Fild, // fild dword ptr [r/m]
|
|
|
|
InstructionType.Unknown, // fisttp dword ptr [r/m] (not implemented)
|
|
|
|
InstructionType.Fist, // fist dword ptr [r/m]
|
|
|
|
InstructionType.Fistp, // fistp dword ptr [r/m]
|
|
|
|
InstructionType.Unknown, // (reserved)
|
|
|
|
InstructionType.Fld, // fld extended-precision [r/m]
|
|
|
|
InstructionType.Unknown, // (reserved)
|
|
|
|
InstructionType.Fstp // fstp extended-precision [r/m]
|
2025-04-12 23:24:42 +03:00
|
|
|
];
|
2025-04-14 00:00:30 +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 DestIndex, FpuRegisterIndex? SrcIndex)> RegisterOperations = new()
|
2025-04-14 00:00:30 +03:00
|
|
|
{
|
|
|
|
// FCMOVNB ST(0), ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.A, RegisterIndex.A), (InstructionType.Fcmovnb, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.C), (InstructionType.Fcmovnb, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.D), (InstructionType.Fcmovnb, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.B), (InstructionType.Fcmovnb, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.Sp), (InstructionType.Fcmovnb, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.Bp), (InstructionType.Fcmovnb, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.Si), (InstructionType.Fcmovnb, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.A, RegisterIndex.Di), (InstructionType.Fcmovnb, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
2025-04-14 00:00:30 +03:00
|
|
|
|
|
|
|
// FCMOVNE ST(0), ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.B, RegisterIndex.A), (InstructionType.Fcmovne, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.B, RegisterIndex.C), (InstructionType.Fcmovne, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.B, RegisterIndex.D), (InstructionType.Fcmovne, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.B, RegisterIndex.B), (InstructionType.Fcmovne, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.B, RegisterIndex.Sp), (InstructionType.Fcmovne, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.B, RegisterIndex.Bp), (InstructionType.Fcmovne, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.B, RegisterIndex.Si), (InstructionType.Fcmovne, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.B, RegisterIndex.Di), (InstructionType.Fcmovne, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
2025-04-14 00:00:30 +03:00
|
|
|
|
|
|
|
// FCMOVNBE ST(0), ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.C, RegisterIndex.A), (InstructionType.Fcmovnbe, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.C), (InstructionType.Fcmovnbe, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.D), (InstructionType.Fcmovnbe, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.B), (InstructionType.Fcmovnbe, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.Sp), (InstructionType.Fcmovnbe, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.Bp), (InstructionType.Fcmovnbe, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.Si), (InstructionType.Fcmovnbe, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.C, RegisterIndex.Di), (InstructionType.Fcmovnbe, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
2025-04-14 00:00:30 +03:00
|
|
|
|
|
|
|
// FCMOVNU ST(0), ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.D, RegisterIndex.A), (InstructionType.Fcmovnu, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.C), (InstructionType.Fcmovnu, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.D), (InstructionType.Fcmovnu, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.B), (InstructionType.Fcmovnu, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.Sp), (InstructionType.Fcmovnu, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.Bp), (InstructionType.Fcmovnu, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.Si), (InstructionType.Fcmovnu, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.D, RegisterIndex.Di), (InstructionType.Fcmovnu, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
2025-04-14 00:00:30 +03:00
|
|
|
|
|
|
|
// Special cases
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.Si, RegisterIndex.C), (InstructionType.Fclex, FpuRegisterIndex.ST0, null) }, // fclex
|
|
|
|
{ (RegisterIndex.Si, RegisterIndex.D), (InstructionType.Finit, FpuRegisterIndex.ST0, null) }, // finit
|
2025-04-14 00:00:30 +03:00
|
|
|
|
|
|
|
// FUCOMI ST(0), ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.Di, RegisterIndex.A), (InstructionType.Fucomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.C), (InstructionType.Fucomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.D), (InstructionType.Fucomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.B), (InstructionType.Fucomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.Sp), (InstructionType.Fucomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.Bp), (InstructionType.Fucomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.Si), (InstructionType.Fucomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.Di, RegisterIndex.Di), (InstructionType.Fucomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) },
|
2025-04-14 00:00:30 +03:00
|
|
|
|
|
|
|
// FCOMI ST(0), ST(i)
|
2025-04-15 02:29:32 +03:00
|
|
|
{ (RegisterIndex.Sp, RegisterIndex.A), (InstructionType.Fcomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST0) },
|
|
|
|
{ (RegisterIndex.Sp, RegisterIndex.C), (InstructionType.Fcomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST1) },
|
|
|
|
{ (RegisterIndex.Sp, RegisterIndex.D), (InstructionType.Fcomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST2) },
|
|
|
|
{ (RegisterIndex.Sp, RegisterIndex.B), (InstructionType.Fcomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST3) },
|
|
|
|
{ (RegisterIndex.Sp, RegisterIndex.Sp), (InstructionType.Fcomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST4) },
|
|
|
|
{ (RegisterIndex.Sp, RegisterIndex.Bp), (InstructionType.Fcomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST5) },
|
|
|
|
{ (RegisterIndex.Sp, RegisterIndex.Si), (InstructionType.Fcomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST6) },
|
|
|
|
{ (RegisterIndex.Sp, RegisterIndex.Di), (InstructionType.Fcomi, FpuRegisterIndex.ST0, FpuRegisterIndex.ST7) }
|
2025-04-14 00:00:30 +03:00
|
|
|
};
|
2025-04-12 23:24:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the LoadStoreInt32Handler class
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
public LoadStoreInt32Handler(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 == 0xDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Decodes a floating-point instruction for load/store int32 and miscellaneous operations
|
|
|
|
/// </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 00:00:30 +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 00:00:30 +03:00
|
|
|
// Handle based on addressing mode
|
2025-04-12 23:24:42 +03:00
|
|
|
if (mod != 3) // Memory operand
|
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
// Set the instruction type based on the reg field
|
|
|
|
instruction.Type = MemoryInstructionTypes[(int)reg];
|
2025-04-14 00:00:30 +03:00
|
|
|
|
2025-04-16 18:30:17 +03:00
|
|
|
// Create a new memory operand with the appropriate size based on the operation
|
|
|
|
Operand memoryOperand;
|
|
|
|
int operandSize;
|
|
|
|
|
2025-04-14 00:00:30 +03:00
|
|
|
if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D) // 32-bit integer operations
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
2025-04-16 18:30:17 +03:00
|
|
|
// Use 32-bit size for integer operations
|
|
|
|
operandSize = 32;
|
2025-04-12 23:24:42 +03:00
|
|
|
}
|
2025-04-14 00:00:30 +03:00
|
|
|
else if (reg == RegisterIndex.Di || reg == RegisterIndex.Bp) // 80-bit extended precision operations
|
2025-04-12 23:24:42 +03:00
|
|
|
{
|
2025-04-16 18:30:17 +03:00
|
|
|
// Use 80-bit size for extended precision operations
|
|
|
|
operandSize = 80;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Default size
|
|
|
|
operandSize = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the appropriate memory operand with the correct size
|
|
|
|
if (rawMemoryOperand is DirectMemoryOperand directMemory)
|
|
|
|
{
|
|
|
|
memoryOperand = OperandFactory.CreateDirectMemoryOperand(directMemory.Address, operandSize);
|
|
|
|
}
|
|
|
|
else if (rawMemoryOperand is BaseRegisterMemoryOperand baseRegMemory)
|
|
|
|
{
|
|
|
|
memoryOperand = OperandFactory.CreateBaseRegisterMemoryOperand(baseRegMemory.BaseRegister, operandSize);
|
|
|
|
}
|
|
|
|
else if (rawMemoryOperand is DisplacementMemoryOperand dispMemory)
|
|
|
|
{
|
|
|
|
memoryOperand = OperandFactory.CreateDisplacementMemoryOperand(dispMemory.BaseRegister, dispMemory.Displacement, operandSize);
|
|
|
|
}
|
|
|
|
else if (rawMemoryOperand is ScaledIndexMemoryOperand scaledMemory)
|
|
|
|
{
|
|
|
|
memoryOperand = OperandFactory.CreateScaledIndexMemoryOperand(scaledMemory.IndexRegister, scaledMemory.Scale, scaledMemory.BaseRegister, scaledMemory.Displacement, operandSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memoryOperand = rawMemoryOperand;
|
2025-04-12 23:24:42 +03:00
|
|
|
}
|
2025-04-14 22:08:50 +03:00
|
|
|
|
|
|
|
// Set the structured operands
|
|
|
|
instruction.StructuredOperands =
|
|
|
|
[
|
|
|
|
memoryOperand
|
|
|
|
];
|
2025-04-12 23:24:42 +03:00
|
|
|
}
|
|
|
|
else // Register operand (ST(i))
|
|
|
|
{
|
2025-04-14 22:08:50 +03:00
|
|
|
// Look up the instruction type in the register operations dictionary
|
2025-04-14 00:00:30 +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 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
|
|
|
|
];
|
|
|
|
}
|
2025-04-12 23:24:42 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Unknown instruction
|
2025-04-14 22:08:50 +03:00
|
|
|
instruction.Type = InstructionType.Unknown;
|
|
|
|
instruction.StructuredOperands = [];
|
2025-04-12 23:24:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|