mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 00:18:02 +03:00
fixes
This commit is contained in:
47
X86Disassembler/X86/Handlers/Ret/Retf16Handler.cs
Normal file
47
X86Disassembler/X86/Handlers/Ret/Retf16Handler.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using X86Disassembler.X86.Operands;
|
||||
|
||||
namespace X86Disassembler.X86.Handlers.Ret;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for RETF instruction with operand size prefix (0xCB with 0x66 prefix)
|
||||
/// </summary>
|
||||
public class Retf16Handler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the Retf16Handler class
|
||||
/// </summary>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
public Retf16Handler(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)
|
||||
{
|
||||
// RETF with operand size prefix is encoded as 0xCB with 0x66 prefix
|
||||
// Only handle when the operand size prefix IS present
|
||||
return opcode == 0xCB && Decoder.HasOperandSizePrefix();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a RETF instruction with operand size prefix
|
||||
/// </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)
|
||||
{
|
||||
// Set the instruction type
|
||||
instruction.Type = InstructionType.Retf;
|
||||
|
||||
// RETF has no operands
|
||||
instruction.StructuredOperands = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
48
X86Disassembler/X86/Handlers/Ret/RetfHandler.cs
Normal file
48
X86Disassembler/X86/Handlers/Ret/RetfHandler.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using X86Disassembler.X86.Operands;
|
||||
|
||||
namespace X86Disassembler.X86.Handlers.Ret;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for RETF instruction (0xCB)
|
||||
/// </summary>
|
||||
public class RetfHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RetfHandler class
|
||||
/// </summary>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
public RetfHandler(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)
|
||||
{
|
||||
// RETF is encoded as 0xCB
|
||||
// Only handle when the operand size prefix is NOT present
|
||||
// This ensures 16-bit handlers get priority when the prefix is present
|
||||
return opcode == 0xCB && !Decoder.HasOperandSizePrefix();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a RETF 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)
|
||||
{
|
||||
// Set the instruction type
|
||||
instruction.Type = InstructionType.Retf;
|
||||
|
||||
// RETF has no operands
|
||||
instruction.StructuredOperands = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
60
X86Disassembler/X86/Handlers/Ret/RetfImm16Handler.cs
Normal file
60
X86Disassembler/X86/Handlers/Ret/RetfImm16Handler.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using X86Disassembler.X86.Operands;
|
||||
|
||||
namespace X86Disassembler.X86.Handlers.Ret;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for RETF imm16 instruction with operand size prefix (0xCA with 0x66 prefix)
|
||||
/// </summary>
|
||||
public class RetfImm16Handler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RetfImm16Handler class
|
||||
/// </summary>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
public RetfImm16Handler(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)
|
||||
{
|
||||
// RETF imm16 with operand size prefix is encoded as 0xCA with 0x66 prefix
|
||||
// Only handle when the operand size prefix IS present
|
||||
return opcode == 0xCA && Decoder.HasOperandSizePrefix();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a RETF imm16 instruction with operand size prefix
|
||||
/// </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)
|
||||
{
|
||||
// Set the instruction type
|
||||
instruction.Type = InstructionType.Retf;
|
||||
|
||||
// Check if we can read the immediate word
|
||||
if (!Decoder.CanReadUShort())
|
||||
return false;
|
||||
|
||||
// Read the immediate word (number of bytes to pop from stack)
|
||||
ushort imm16 = Decoder.ReadUInt16();
|
||||
|
||||
// Create an immediate operand for the pop count
|
||||
var immOperand = OperandFactory.CreateImmediateOperand(imm16, 16);
|
||||
|
||||
// Set the structured operands
|
||||
instruction.StructuredOperands =
|
||||
[
|
||||
immOperand
|
||||
];
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
61
X86Disassembler/X86/Handlers/Ret/RetfImmHandler.cs
Normal file
61
X86Disassembler/X86/Handlers/Ret/RetfImmHandler.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using X86Disassembler.X86.Operands;
|
||||
|
||||
namespace X86Disassembler.X86.Handlers.Ret;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for RETF imm16 instruction (0xCA)
|
||||
/// </summary>
|
||||
public class RetfImmHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RetfImmHandler class
|
||||
/// </summary>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
public RetfImmHandler(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)
|
||||
{
|
||||
// RETF imm16 is encoded as 0xCA
|
||||
// Only handle when the operand size prefix is NOT present
|
||||
// This ensures 16-bit handlers get priority when the prefix is present
|
||||
return opcode == 0xCA && !Decoder.HasOperandSizePrefix();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a RETF imm16 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)
|
||||
{
|
||||
// Set the instruction type
|
||||
instruction.Type = InstructionType.Retf;
|
||||
|
||||
// Check if we can read the immediate word
|
||||
if (!Decoder.CanReadUShort())
|
||||
return false;
|
||||
|
||||
// Read the immediate word (number of bytes to pop from stack)
|
||||
ushort imm16 = Decoder.ReadUInt16();
|
||||
|
||||
// Create an immediate operand for the pop count
|
||||
var immOperand = OperandFactory.CreateImmediateOperand(imm16, 16);
|
||||
|
||||
// Set the structured operands
|
||||
instruction.StructuredOperands =
|
||||
[
|
||||
immOperand
|
||||
];
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -3,47 +3,67 @@ namespace X86Disassembler.X86.Handlers.String;
|
||||
using Operands;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for string instructions (MOVS, STOS, LODS, SCAS) with and without REP/REPNE prefixes
|
||||
/// Handler for string instructions (MOVS, CMPS, STOS, LODS, SCAS)
|
||||
/// The REP/REPNE prefixes are handled by the InstructionDecoder class
|
||||
/// </summary>
|
||||
public class StringInstructionHandler : InstructionHandler
|
||||
{
|
||||
// Dictionary mapping opcodes to their instruction types and operand factories
|
||||
private static readonly Dictionary<byte, (InstructionType Type, Func<Operand[]> CreateOperands)> StringInstructions = new()
|
||||
// Dictionary mapping opcodes to their instruction types and operand factories for 32-bit mode (default)
|
||||
private static readonly Dictionary<byte, (InstructionType Type, Func<Operand[]> CreateOperands)> StringInstructions32 = new()
|
||||
{
|
||||
// MOVS instructions
|
||||
{ 0xA4, (InstructionType.MovsB, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 8, "es"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Si, 8, "ds")
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Di, "es"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Si, "ds")
|
||||
]) }, // MOVSB
|
||||
{ 0xA5, (InstructionType.MovsD, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 32, "es"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Si, 32, "ds")
|
||||
]) }, // MOVSD
|
||||
]) }, // MOVSD
|
||||
|
||||
// CMPS instructions
|
||||
{ 0xA6, (InstructionType.CmpsB, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Si, "ds"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Di, "es")
|
||||
]) }, // CMPSB
|
||||
{ 0xA7, (InstructionType.CmpsD, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Si, 32, "ds"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 32, "es")
|
||||
]) }, // CMPSD
|
||||
|
||||
// STOS instructions
|
||||
{ 0xAA, (InstructionType.StosB, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 8, "es"),
|
||||
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 8)
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Di, "es"),
|
||||
OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL)
|
||||
]) }, // STOSB
|
||||
{ 0xAB, (InstructionType.StosD, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 32, "es"),
|
||||
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32)
|
||||
]) }, // STOSD
|
||||
|
||||
// LODS instructions
|
||||
{ 0xAC, (InstructionType.LodsB, () =>
|
||||
[
|
||||
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 8),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Si, 8, "ds")
|
||||
OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Si, "ds")
|
||||
]) }, // LODSB
|
||||
{ 0xAD, (InstructionType.LodsD, () =>
|
||||
[
|
||||
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Si, 32, "ds")
|
||||
]) }, // LODSD
|
||||
|
||||
// SCAS instructions
|
||||
{ 0xAE, (InstructionType.ScasB, () =>
|
||||
[
|
||||
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 8),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 8, "es")
|
||||
OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Di, "es")
|
||||
]) }, // SCASB
|
||||
{ 0xAF, (InstructionType.ScasD, () =>
|
||||
[
|
||||
@ -52,28 +72,68 @@ public class StringInstructionHandler : InstructionHandler
|
||||
]) } // SCASD
|
||||
};
|
||||
|
||||
// REP/REPNE prefix opcodes
|
||||
private const byte REP_PREFIX = 0xF3;
|
||||
private const byte REPNE_PREFIX = 0xF2;
|
||||
|
||||
// Dictionary mapping base instruction types to their REP-prefixed versions
|
||||
private static readonly Dictionary<InstructionType, InstructionType> RepPrefixMap = new()
|
||||
// Dictionary mapping opcodes to their instruction types and operand factories for 16-bit mode (with operand size prefix)
|
||||
private static readonly Dictionary<byte, (InstructionType Type, Func<Operand[]> CreateOperands)> StringInstructions16 = new()
|
||||
{
|
||||
{ InstructionType.MovsB, InstructionType.RepMovsB },
|
||||
{ InstructionType.MovsD, InstructionType.RepMovsD },
|
||||
{ InstructionType.StosB, InstructionType.RepStosB },
|
||||
{ InstructionType.StosD, InstructionType.RepStosD },
|
||||
{ InstructionType.LodsB, InstructionType.RepLodsB },
|
||||
{ InstructionType.LodsD, InstructionType.RepLodsD },
|
||||
{ InstructionType.ScasB, InstructionType.RepScasB },
|
||||
{ InstructionType.ScasD, InstructionType.RepScasD }
|
||||
};
|
||||
|
||||
// Dictionary mapping base instruction types to their REPNE-prefixed versions
|
||||
private static readonly Dictionary<InstructionType, InstructionType> RepnePrefixMap = new()
|
||||
{
|
||||
{ InstructionType.ScasB, InstructionType.RepneScasB },
|
||||
{ InstructionType.ScasD, InstructionType.RepneScasD }
|
||||
// MOVS instructions
|
||||
{ 0xA4, (InstructionType.MovsB, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Di, "es"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Si, "ds")
|
||||
]) }, // MOVSB (same for 16-bit)
|
||||
{ 0xA5, (InstructionType.MovsW, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 16, "es"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Si, 16, "ds")
|
||||
]) }, // MOVSW
|
||||
|
||||
// CMPS instructions
|
||||
{ 0xA6, (InstructionType.CmpsB, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Si, "ds"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Di, "es")
|
||||
]) }, // CMPSB (same for 16-bit)
|
||||
{ 0xA7, (InstructionType.CmpsW, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Si, 16, "ds"),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 16, "es")
|
||||
]) }, // CMPSW
|
||||
|
||||
// STOS instructions
|
||||
{ 0xAA, (InstructionType.StosB, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Di, "es"),
|
||||
OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL)
|
||||
]) }, // STOSB (same for 16-bit)
|
||||
{ 0xAB, (InstructionType.StosW, () =>
|
||||
[
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 16, "es"),
|
||||
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 16)
|
||||
]) }, // STOSW
|
||||
|
||||
// LODS instructions
|
||||
{ 0xAC, (InstructionType.LodsB, () =>
|
||||
[
|
||||
OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Si, "ds")
|
||||
]) }, // LODSB (same for 16-bit)
|
||||
{ 0xAD, (InstructionType.LodsW, () =>
|
||||
[
|
||||
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 16),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Si, 16, "ds")
|
||||
]) }, // LODSW
|
||||
|
||||
// SCAS instructions
|
||||
{ 0xAE, (InstructionType.ScasB, () =>
|
||||
[
|
||||
OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand8(RegisterIndex.Di, "es")
|
||||
]) }, // SCASB (same for 16-bit)
|
||||
{ 0xAF, (InstructionType.ScasW, () =>
|
||||
[
|
||||
OperandFactory.CreateRegisterOperand(RegisterIndex.A, 16),
|
||||
OperandFactory.CreateBaseRegisterMemoryOperand(RegisterIndex.Di, 16, "es")
|
||||
]) } // SCASW
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@ -92,27 +152,8 @@ public class StringInstructionHandler : InstructionHandler
|
||||
/// <returns>True if this handler can handle the opcode</returns>
|
||||
public override bool CanHandle(byte opcode)
|
||||
{
|
||||
// Check if the opcode is a string instruction
|
||||
if (StringInstructions.ContainsKey(opcode))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if the opcode is a REP/REPNE prefix followed by a string instruction
|
||||
if (opcode != REP_PREFIX && opcode != REPNE_PREFIX)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if we can read the next byte
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if the next byte is a string instruction
|
||||
byte nextByte = Decoder.PeakByte();
|
||||
return StringInstructions.ContainsKey(nextByte);
|
||||
// Check if the opcode is a string instruction in either 16-bit or 32-bit mode
|
||||
return StringInstructions32.ContainsKey(opcode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -123,59 +164,19 @@ public class StringInstructionHandler : InstructionHandler
|
||||
/// <returns>True if the instruction was successfully decoded</returns>
|
||||
public override bool Decode(byte opcode, Instruction instruction)
|
||||
{
|
||||
// Check if this is a REP/REPNE prefix
|
||||
bool hasRepPrefix = opcode == REP_PREFIX || opcode == REPNE_PREFIX;
|
||||
// Select the appropriate dictionary based on operand size prefix
|
||||
var instructionsDict = Decoder.HasOperandSizePrefix()
|
||||
? StringInstructions16
|
||||
: StringInstructions32;
|
||||
|
||||
// If this is a REP/REPNE prefix, get the actual string instruction opcode
|
||||
byte stringOpcode = opcode;
|
||||
|
||||
if (hasRepPrefix)
|
||||
{
|
||||
// Read the next byte (the actual string instruction opcode)
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
stringOpcode = Decoder.ReadByte();
|
||||
|
||||
if (!StringInstructions.ContainsKey(stringOpcode))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the instruction type and operands for the string instruction
|
||||
if (StringInstructions.TryGetValue(stringOpcode, out var instructionInfo))
|
||||
if (instructionsDict.TryGetValue(opcode, out var instructionInfo))
|
||||
{
|
||||
// Set the instruction type based on whether there's a REP/REPNE prefix
|
||||
if (hasRepPrefix)
|
||||
{
|
||||
// Determine the appropriate prefixed instruction type based on the prefix
|
||||
if (opcode == REP_PREFIX)
|
||||
{
|
||||
// Use the REP prefix map to get the prefixed instruction type
|
||||
instruction.Type = RepPrefixMap.TryGetValue(instructionInfo.Type, out var repType)
|
||||
? repType
|
||||
: instructionInfo.Type;
|
||||
}
|
||||
else // REPNE prefix
|
||||
{
|
||||
// Use the REPNE prefix map to get the prefixed instruction type
|
||||
instruction.Type = RepnePrefixMap.TryGetValue(instructionInfo.Type, out var repneType)
|
||||
? repneType
|
||||
: instructionInfo.Type;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No prefix, use the original instruction type
|
||||
instruction.Type = instructionInfo.Type;
|
||||
}
|
||||
// Set the instruction type
|
||||
instruction.Type = instructionInfo.Type;
|
||||
|
||||
// Create and set the structured operands
|
||||
instruction.StructuredOperands = instructionInfo.CreateOperands().ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user