0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 08:18:36 +03:00

add misc handlers, cleanup and fixes

This commit is contained in:
bird_egop
2025-04-17 20:47:51 +03:00
parent 124493cd94
commit a9d4c39717
22 changed files with 1086 additions and 63 deletions

View File

@ -0,0 +1,59 @@
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for CPUID instruction (0x0F 0xA2)
/// </summary>
public class CpuidHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the CpuidHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public CpuidHandler(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)
{
// CPUID is encoded as 0x0F 0xA2
if (opcode != 0x0F)
return false;
// Check if we can read the second byte
if (!Decoder.CanReadByte())
return false;
// Check if the second byte is 0xA2
byte secondByte = Decoder.PeakByte();
return secondByte == 0xA2;
}
/// <summary>
/// Decodes a CPUID 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.Cpuid;
// Read and discard the second byte (0xA2)
if (!Decoder.CanReadByte())
return false;
Decoder.ReadByte();
// CPUID has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,44 @@
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for HLT instruction (0xF4)
/// </summary>
public class HltHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the HltHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public HltHandler(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)
{
// HLT is encoded as 0xF4
return opcode == 0xF4;
}
/// <summary>
/// Decodes a HLT 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.Hlt;
// HLT has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,97 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for IN instruction (0xE4, 0xE5, 0xEC, 0xED)
/// </summary>
public class InHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the InHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public InHandler(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)
{
// IN AL, imm8 is encoded as 0xE4
// IN EAX, imm8 is encoded as 0xE5
// IN AL, DX is encoded as 0xEC
// IN EAX, DX is encoded as 0xED
return opcode == 0xE4 || opcode == 0xE5 || opcode == 0xEC || opcode == 0xED;
}
/// <summary>
/// Decodes an IN 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.In;
// Determine the operands based on the opcode
Operand destOperand;
Operand srcOperand;
switch (opcode)
{
case 0xE4: // IN AL, imm8
destOperand = OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL);
// Check if we can read the immediate byte
if (!Decoder.CanReadByte())
return false;
// Read the immediate byte (port number)
byte imm8 = Decoder.ReadByte();
srcOperand = OperandFactory.CreateImmediateOperand(imm8);
break;
case 0xE5: // IN EAX, imm8
destOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32);
// Check if we can read the immediate byte
if (!Decoder.CanReadByte())
return false;
// Read the immediate byte (port number)
imm8 = Decoder.ReadByte();
srcOperand = OperandFactory.CreateImmediateOperand(imm8);
break;
case 0xEC: // IN AL, DX
destOperand = OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL);
srcOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.D, 16);
break;
case 0xED: // IN EAX, DX
destOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32);
srcOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.D, 16);
break;
default:
return false;
}
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
srcOperand
];
return true;
}
}

View File

@ -0,0 +1,61 @@
namespace X86Disassembler.X86.Handlers.Misc;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for INT instruction (0xCD)
/// </summary>
public class IntHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the IntHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public IntHandler(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)
{
// INT is encoded as 0xCD
return opcode == 0xCD;
}
/// <summary>
/// Decodes an INT 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.Int;
// Check if we can read the immediate byte
if (!Decoder.CanReadByte())
{
return false;
}
// Read the immediate byte (interrupt vector)
byte imm8 = Decoder.ReadByte();
// Create an immediate operand for the interrupt vector
var operand = OperandFactory.CreateImmediateOperand(imm8);
// Set the structured operands
instruction.StructuredOperands =
[
operand
];
return true;
}
}

View File

@ -0,0 +1,44 @@
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for INTO instruction (0xCE)
/// </summary>
public class IntoHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the IntoHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public IntoHandler(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)
{
// INTO is encoded as 0xCE
return opcode == 0xCE;
}
/// <summary>
/// Decodes an INTO 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.Into;
// INTO has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,44 @@
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for IRET/IRETD instruction (0xCF)
/// </summary>
public class IretHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the IretHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public IretHandler(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)
{
// IRET/IRETD is encoded as 0xCF
return opcode == 0xCF;
}
/// <summary>
/// Decodes an IRET/IRETD 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.Iret;
// IRET/IRETD has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,44 @@
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for LOCK prefix (0xF0)
/// </summary>
public class LockHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the LockHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public LockHandler(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)
{
// LOCK prefix is encoded as 0xF0
return opcode == 0xF0;
}
/// <summary>
/// Decodes a LOCK 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.Lock;
// LOCK prefix has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,95 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for OUT instruction (0xE6, 0xE7, 0xEE, 0xEF)
/// </summary>
public class OutHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the OutHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public OutHandler(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)
{
// OUT imm8, AL is encoded as 0xE6
// OUT imm8, EAX is encoded as 0xE7
// OUT DX, AL is encoded as 0xEE
// OUT DX, EAX is encoded as 0xEF
return opcode == 0xE6 || opcode == 0xE7 || opcode == 0xEE || opcode == 0xEF;
}
/// <summary>
/// Decodes an OUT 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.Out;
// Determine the operands based on the opcode
Operand destOperand;
Operand srcOperand;
switch (opcode)
{
case 0xE6: // OUT imm8, AL
// Check if we can read the immediate byte
if (!Decoder.CanReadByte())
return false;
// Read the immediate byte (port number)
byte imm8 = Decoder.ReadByte();
destOperand = OperandFactory.CreateImmediateOperand(imm8);
srcOperand = OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL);
break;
case 0xE7: // OUT imm8, EAX
// Check if we can read the immediate byte
if (!Decoder.CanReadByte())
return false;
// Read the immediate byte (port number)
imm8 = Decoder.ReadByte();
destOperand = OperandFactory.CreateImmediateOperand(imm8);
srcOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32);
break;
case 0xEE: // OUT DX, AL
destOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.D, 16);
srcOperand = OperandFactory.CreateRegisterOperand8(RegisterIndex8.AL);
break;
case 0xEF: // OUT DX, EAX
destOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.D, 16);
srcOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 32);
break;
default:
return false;
}
// Set the structured operands
instruction.StructuredOperands =
[
destOperand,
srcOperand
];
return true;
}
}

View File

@ -0,0 +1,59 @@
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for RDTSC instruction (0x0F 0x31)
/// </summary>
public class RdtscHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the RdtscHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public RdtscHandler(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)
{
// RDTSC is encoded as 0x0F 0x31
if (opcode != 0x0F)
return false;
// Check if we can read the second byte
if (!Decoder.CanReadByte())
return false;
// Check if the second byte is 0x31
byte secondByte = Decoder.PeakByte();
return secondByte == 0x31;
}
/// <summary>
/// Decodes a RDTSC 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.Rdtsc;
// Read and discard the second byte (0x31)
if (!Decoder.CanReadByte())
return false;
Decoder.ReadByte();
// RDTSC has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,44 @@
namespace X86Disassembler.X86.Handlers.Misc;
/// <summary>
/// Handler for WAIT/FWAIT instruction (0x9B)
/// </summary>
public class WaitHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the WaitHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public WaitHandler(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)
{
// WAIT/FWAIT is encoded as 0x9B
return opcode == 0x9B;
}
/// <summary>
/// Decodes a WAIT/FWAIT 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.Wait;
// WAIT/FWAIT has no operands
instruction.StructuredOperands = [];
return true;
}
}