0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 03:41:18 +03:00

Refactored floating point handlers into specialized classes for better organization and maintainability

This commit is contained in:
bird_egop 2025-04-17 23:57:16 +03:00
parent 5916d13995
commit ec56576116
22 changed files with 1509 additions and 3 deletions

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Arithmetic;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FABS instruction (D9 E1)
/// </summary>
public class FabsHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FabsHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FabsHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FABS is D9 E1
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is E1
byte nextByte = Decoder.PeakByte();
return nextByte == 0xE1;
}
/// <summary>
/// Decodes a FABS instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.Fabs;
// FABS has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Arithmetic;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FCHS instruction (D9 E0)
/// </summary>
public class FchsHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FchsHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FchsHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FCHS is D9 E0
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is E0
byte nextByte = Decoder.PeakByte();
return nextByte == 0xE0;
}
/// <summary>
/// Decodes a FCHS instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.Fchs;
// FCHS has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Arithmetic;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FIADD int32 instruction (DA /0)
/// </summary>
public class FiaddInt32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FiaddInt32Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FiaddInt32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FIADD is DA /0
if (opcode != 0xDA) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 0
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 0 && mod != 3;
}
/// <summary>
/// Decodes a FIADD int32 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fiadd;
// Set the structured operands - the operand already has the correct size from ReadModRM
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Arithmetic;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FIDIV int32 instruction (DA /6)
/// </summary>
public class FidivInt32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FidivInt32Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FidivInt32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FIDIV is DA /6
if (opcode != 0xDA) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 6
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 6 && mod != 3;
}
/// <summary>
/// Decodes a FIDIV int32 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fidiv;
// Set the structured operands - the operand already has the correct size from ReadModRM
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Arithmetic;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FIDIVR int32 instruction (DA /7)
/// </summary>
public class FidivrInt32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FidivrInt32Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FidivrInt32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FIDIVR is DA /7
if (opcode != 0xDA) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 7
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 7 && mod != 3;
}
/// <summary>
/// Decodes a FIDIVR int32 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fidivr;
// Set the structured operands - the operand already has the correct size from ReadModRM
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Arithmetic;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FIMUL int32 instruction (DA /1)
/// </summary>
public class FimulInt32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FimulInt32Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FimulInt32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FIMUL is DA /1
if (opcode != 0xDA) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 1
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 1 && mod != 3;
}
/// <summary>
/// Decodes a FIMUL int32 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fimul;
// Set the structured operands - the operand already has the correct size from ReadModRM
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Arithmetic;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FISUB int32 instruction (DA /4)
/// </summary>
public class FisubInt32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FisubInt32Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FisubInt32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FISUB is DA /4
if (opcode != 0xDA) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 4
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 4 && mod != 3;
}
/// <summary>
/// Decodes a FISUB int32 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fisub;
// Set the structured operands - the operand already has the correct size from ReadModRM
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Arithmetic;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FISUBR int32 instruction (DA /5)
/// </summary>
public class FisubrInt32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FisubrInt32Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FisubrInt32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FISUBR is DA /5
if (opcode != 0xDA) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 5
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 5 && mod != 3;
}
/// <summary>
/// Decodes a FISUBR int32 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fisubr;
// Set the structured operands - the operand already has the correct size from ReadModRM
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Comparison;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FICOM int32 instruction (DA /2)
/// </summary>
public class FicomInt32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FicomInt32Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FicomInt32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FICOM is DA /2
if (opcode != 0xDA) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 2
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 2 && mod != 3;
}
/// <summary>
/// Decodes a FICOM int32 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Ficom;
// Set the structured operands - the operand already has the correct size from ReadModRM
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Comparison;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FICOMP int32 instruction (DA /3)
/// </summary>
public class FicompInt32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FicompInt32Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FicompInt32Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FICOMP is DA /3
if (opcode != 0xDA) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 3
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 3 && mod != 3;
}
/// <summary>
/// Decodes a FICOMP int32 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Ficomp;
// Set the structured operands - the operand already has the correct size from ReadModRM
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Comparison;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FTST instruction (D9 E4)
/// </summary>
public class FtstHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FtstHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FtstHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FTST is D9 E4
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is E4
byte nextByte = Decoder.PeakByte();
return nextByte == 0xE4;
}
/// <summary>
/// Decodes a FTST instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.Ftst;
// FTST has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,94 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FLDCW instruction (D9 /5)
/// </summary>
public class FldcwHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FldcwHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FldcwHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FLDCW is D9 /5
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 5
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 5 && mod != 3;
}
/// <summary>
/// Decodes a FLDCW instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fldcw;
// Create a 16-bit memory operand for control word operations
Operand memoryOperand;
if (rawOperand is DirectMemoryOperand directMemory)
{
memoryOperand = OperandFactory.CreateDirectMemoryOperand16(directMemory.Address);
}
else if (rawOperand is BaseRegisterMemoryOperand baseRegMemory)
{
memoryOperand = OperandFactory.CreateBaseRegisterMemoryOperand16(baseRegMemory.BaseRegister);
}
else if (rawOperand is DisplacementMemoryOperand dispMemory)
{
memoryOperand = OperandFactory.CreateDisplacementMemoryOperand16(dispMemory.BaseRegister, dispMemory.Displacement);
}
else if (rawOperand is ScaledIndexMemoryOperand scaledMemory)
{
memoryOperand = OperandFactory.CreateScaledIndexMemoryOperand16(scaledMemory.IndexRegister, scaledMemory.Scale, scaledMemory.BaseRegister, scaledMemory.Displacement);
}
else
{
memoryOperand = rawOperand;
}
// Set the structured operands
instruction.StructuredOperands =
[
memoryOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FLDENV instruction (D9 /4)
/// </summary>
public class FldenvHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FldenvHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FldenvHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FLDENV is D9 /4
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 4
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 4 && mod != 3;
}
/// <summary>
/// Decodes a FLDENV instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fldenv;
// Set the structured operands
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,94 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FNSTCW instruction (D9 /7)
/// </summary>
public class FnstcwHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FnstcwHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FnstcwHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FNSTCW is D9 /7
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 7
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 7 && mod != 3;
}
/// <summary>
/// Decodes a FNSTCW instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fnstcw;
// Create a 16-bit memory operand for control word operations
Operand memoryOperand;
if (rawOperand is DirectMemoryOperand directMemory)
{
memoryOperand = OperandFactory.CreateDirectMemoryOperand16(directMemory.Address);
}
else if (rawOperand is BaseRegisterMemoryOperand baseRegMemory)
{
memoryOperand = OperandFactory.CreateBaseRegisterMemoryOperand16(baseRegMemory.BaseRegister);
}
else if (rawOperand is DisplacementMemoryOperand dispMemory)
{
memoryOperand = OperandFactory.CreateDisplacementMemoryOperand16(dispMemory.BaseRegister, dispMemory.Displacement);
}
else if (rawOperand is ScaledIndexMemoryOperand scaledMemory)
{
memoryOperand = OperandFactory.CreateScaledIndexMemoryOperand16(scaledMemory.IndexRegister, scaledMemory.Scale, scaledMemory.BaseRegister, scaledMemory.Displacement);
}
else
{
memoryOperand = rawOperand;
}
// Set the structured operands
instruction.StructuredOperands =
[
memoryOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FNSTENV instruction (D9 /6)
/// </summary>
public class FnstenvHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FnstenvHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FnstenvHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FNSTENV is D9 /6
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 6
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 6 && mod != 3;
}
/// <summary>
/// Decodes a FNSTENV instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fnstenv;
// Set the structured operands
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FXAM instruction (D9 E5)
/// </summary>
public class FxamHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FxamHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FxamHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FXAM is D9 E5
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is E5
byte nextByte = Decoder.PeakByte();
return nextByte == 0xE5;
}
/// <summary>
/// Decodes a FXAM instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.Fxam;
// FXAM has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,87 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FXCH instruction (D9 /1 with mod=3)
/// </summary>
public class FxchHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FxchHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FxchHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FXCH is D9 /1 with mod=3
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 1 and mod = 3
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle register operands (mod = 3) with reg = 1
return reg == 1 && mod == 3;
}
/// <summary>
/// Decodes a FXCH instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, _) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fxch;
// Map rm field to FPU register index
FpuRegisterIndex stIndex = rm switch
{
RegisterIndex.A => FpuRegisterIndex.ST0,
RegisterIndex.C => FpuRegisterIndex.ST1,
RegisterIndex.D => FpuRegisterIndex.ST2,
RegisterIndex.B => FpuRegisterIndex.ST3,
RegisterIndex.Sp => FpuRegisterIndex.ST4,
RegisterIndex.Bp => FpuRegisterIndex.ST5,
RegisterIndex.Si => FpuRegisterIndex.ST6,
RegisterIndex.Di => FpuRegisterIndex.ST7,
_ => FpuRegisterIndex.ST0 // Default case, should not happen
};
// Create the FPU register operand
var operand = OperandFactory.CreateFPURegisterOperand(stIndex);
// Set the structured operands
instruction.StructuredOperands =
[
operand
];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Transcendental;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for F2XM1 instruction (D9 F0)
/// </summary>
public class F2xm1Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the F2xm1Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public F2xm1Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// F2XM1 is D9 F0
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is F0
byte nextByte = Decoder.PeakByte();
return nextByte == 0xF0;
}
/// <summary>
/// Decodes a F2XM1 instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.F2xm1;
// F2XM1 has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Transcendental;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FPATAN instruction (D9 F3)
/// </summary>
public class FpatanHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FpatanHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FpatanHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FPATAN is D9 F3
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is F3
byte nextByte = Decoder.PeakByte();
return nextByte == 0xF3;
}
/// <summary>
/// Decodes a FPATAN instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.Fpatan;
// FPATAN has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Transcendental;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FPTAN instruction (D9 F2)
/// </summary>
public class FptanHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FptanHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FptanHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FPTAN is D9 F2
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is F2
byte nextByte = Decoder.PeakByte();
return nextByte == 0xF2;
}
/// <summary>
/// Decodes a FPTAN instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.Fptan;
// FPTAN has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Transcendental;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FYL2X instruction (D9 F1)
/// </summary>
public class Fyl2xHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the Fyl2xHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public Fyl2xHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// FYL2X is D9 F1
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is F1
byte nextByte = Decoder.PeakByte();
return nextByte == 0xF1;
}
/// <summary>
/// Decodes a FYL2X instruction
/// </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)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.Fyl2x;
// FYL2X has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -389,6 +389,20 @@ public class InstructionHandlerFactory
_handlers.Add(new FloatingPoint.LoadStore.FldFloat32Handler(_decoder)); // FLD float32 (D9 /0)
_handlers.Add(new FloatingPoint.LoadStore.FstFloat32Handler(_decoder)); // FST float32 (D9 /2)
_handlers.Add(new FloatingPoint.LoadStore.FstpFloat32Handler(_decoder)); // FSTP float32 (D9 /3)
_handlers.Add(new FloatingPoint.Control.FldenvHandler(_decoder)); // FLDENV (D9 /4)
_handlers.Add(new FloatingPoint.Control.FldcwHandler(_decoder)); // FLDCW (D9 /5)
_handlers.Add(new FloatingPoint.Control.FnstenvHandler(_decoder)); // FNSTENV (D9 /6)
_handlers.Add(new FloatingPoint.Control.FnstcwHandler(_decoder)); // FNSTCW (D9 /7)
// DA opcode handlers (int32 operations)
_handlers.Add(new FloatingPoint.Arithmetic.FiaddInt32Handler(_decoder)); // FIADD int32 (DA /0)
_handlers.Add(new FloatingPoint.Arithmetic.FimulInt32Handler(_decoder)); // FIMUL int32 (DA /1)
_handlers.Add(new FloatingPoint.Comparison.FicomInt32Handler(_decoder)); // FICOM int32 (DA /2)
_handlers.Add(new FloatingPoint.Comparison.FicompInt32Handler(_decoder)); // FICOMP int32 (DA /3)
_handlers.Add(new FloatingPoint.Arithmetic.FisubInt32Handler(_decoder)); // FISUB int32 (DA /4)
_handlers.Add(new FloatingPoint.Arithmetic.FisubrInt32Handler(_decoder)); // FISUBR int32 (DA /5)
_handlers.Add(new FloatingPoint.Arithmetic.FidivInt32Handler(_decoder)); // FIDIV int32 (DA /6)
_handlers.Add(new FloatingPoint.Arithmetic.FidivrInt32Handler(_decoder)); // FIDIVR int32 (DA /7)
// DC opcode handlers (float64 operations)
_handlers.Add(new FloatingPoint.Arithmetic.FaddFloat64Handler(_decoder)); // FADD float64 (DC /0)
@ -405,12 +419,25 @@ public class InstructionHandlerFactory
_handlers.Add(new FloatingPoint.LoadStore.FstFloat64Handler(_decoder)); // FST float64 (DD /2)
_handlers.Add(new FloatingPoint.LoadStore.FstpFloat64Handler(_decoder)); // FSTP float64 (DD /3)
// Register-register operations
_handlers.Add(new FloatingPoint.Control.FxchHandler(_decoder)); // FXCH (D9 C8-CF)
// Special floating point instructions
_handlers.Add(new FloatingPoint.Arithmetic.FchsHandler(_decoder)); // FCHS (D9 E0)
_handlers.Add(new FloatingPoint.Arithmetic.FabsHandler(_decoder)); // FABS (D9 E1)
_handlers.Add(new FloatingPoint.Comparison.FtstHandler(_decoder)); // FTST (D9 E4)
_handlers.Add(new FloatingPoint.Control.FxamHandler(_decoder)); // FXAM (D9 E5)
// Transcendental functions
_handlers.Add(new FloatingPoint.Transcendental.F2xm1Handler(_decoder)); // F2XM1 (D9 F0)
_handlers.Add(new FloatingPoint.Transcendental.Fyl2xHandler(_decoder)); // FYL2X (D9 F1)
_handlers.Add(new FloatingPoint.Transcendental.FptanHandler(_decoder)); // FPTAN (D9 F2)
_handlers.Add(new FloatingPoint.Transcendental.FpatanHandler(_decoder)); // FPATAN (D9 F3)
// Other floating point handlers
_handlers.Add(new FloatingPoint.Control.FnstswHandler(_decoder)); // FNSTSW AX (DF E0)
// Keep the existing handlers for operations not yet migrated to specialized handlers
_handlers.Add(new LoadStoreControlHandler(_decoder)); // Load and store control words (D9 /4-/7)
_handlers.Add(new Int32OperationHandler(_decoder)); // Integer operations on 32-bit values
_handlers.Add(new LoadStoreInt32Handler(_decoder)); // Load and store 32-bit values
_handlers.Add(new Float64OperationHandler(_decoder)); // Remaining float64 operations
_handlers.Add(new LoadStoreFloat64Handler(_decoder)); // Load and store 64-bit values