mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 08:18:36 +03:00
Refactor instruction handlers to use single instruction per handler pattern
This commit is contained in:
59
X86Disassembler/X86/Handlers/CallRel32Handler.cs
Normal file
59
X86Disassembler/X86/Handlers/CallRel32Handler.cs
Normal file
@ -0,0 +1,59 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for CALL rel32 instruction (0xE8)
|
||||
/// </summary>
|
||||
public class CallRel32Handler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the CallRel32Handler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public CallRel32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0xE8;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a CALL rel32 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 mnemonic
|
||||
instruction.Mnemonic = "call";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position + 4 > Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
int offset = BitConverter.ToInt32(CodeBuffer, position);
|
||||
Decoder.SetPosition(position + 4);
|
||||
|
||||
// Calculate the target address
|
||||
uint targetAddress = (uint)(position + offset + 4);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
68
X86Disassembler/X86/Handlers/ConditionalJumpHandler.cs
Normal file
68
X86Disassembler/X86/Handlers/ConditionalJumpHandler.cs
Normal file
@ -0,0 +1,68 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for conditional jump instructions (0x70-0x7F)
|
||||
/// </summary>
|
||||
public class ConditionalJumpHandler : InstructionHandler
|
||||
{
|
||||
// Mnemonics for conditional jumps
|
||||
private static readonly string[] ConditionalJumpMnemonics = new string[]
|
||||
{
|
||||
"jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "jnbe",
|
||||
"js", "jns", "jp", "jnp", "jl", "jnl", "jle", "jnle"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ConditionalJumpHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public ConditionalJumpHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
// Conditional jumps are in the range 0x70-0x7F
|
||||
return opcode >= 0x70 && opcode <= 0x7F;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a conditional jump 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)
|
||||
{
|
||||
// Get the mnemonic from the table
|
||||
int index = opcode - 0x70;
|
||||
instruction.Mnemonic = ConditionalJumpMnemonics[index];
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
sbyte offset = (sbyte)CodeBuffer[position];
|
||||
Decoder.SetPosition(position + 1);
|
||||
|
||||
// Calculate the target address
|
||||
uint targetAddress = (uint)(position + offset + 1);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
63
X86Disassembler/X86/Handlers/FnstswHandler.cs
Normal file
63
X86Disassembler/X86/Handlers/FnstswHandler.cs
Normal file
@ -0,0 +1,63 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for FNSTSW instruction (0xDFE0)
|
||||
/// </summary>
|
||||
public class FnstswHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the FnstswHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public FnstswHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
// FNSTSW is a two-byte opcode (0xDF 0xE0)
|
||||
if (opcode == 0xDF)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
if (position < Length && CodeBuffer[position] == 0xE0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an FNSTSW 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)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length || CodeBuffer[position] != 0xE0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Skip the second byte of the opcode
|
||||
Decoder.SetPosition(position + 1);
|
||||
|
||||
// Set the mnemonic and operands
|
||||
instruction.Mnemonic = "fnstsw";
|
||||
instruction.Operands = "ax";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
22
X86Disassembler/X86/Handlers/IInstructionHandler.cs
Normal file
22
X86Disassembler/X86/Handlers/IInstructionHandler.cs
Normal file
@ -0,0 +1,22 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for instruction handlers
|
||||
/// </summary>
|
||||
public interface IInstructionHandler
|
||||
{
|
||||
/// <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>
|
||||
bool CanHandle(byte opcode);
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an 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>
|
||||
bool Decode(byte opcode, Instruction instruction);
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for all instruction handlers
|
||||
/// Abstract base class for instruction handlers
|
||||
/// </summary>
|
||||
public abstract class InstructionHandler
|
||||
public abstract class InstructionHandler : IInstructionHandler
|
||||
{
|
||||
// Buffer containing the code to decode
|
||||
protected readonly byte[] CodeBuffer;
|
||||
|
72
X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs
Normal file
72
X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs
Normal file
@ -0,0 +1,72 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Factory for creating instruction handlers
|
||||
/// </summary>
|
||||
public class InstructionHandlerFactory
|
||||
{
|
||||
private readonly byte[] _codeBuffer;
|
||||
private readonly InstructionDecoder _decoder;
|
||||
private readonly int _length;
|
||||
private readonly List<IInstructionHandler> _handlers = new List<IInstructionHandler>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the InstructionHandlerFactory class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this factory</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public InstructionHandlerFactory(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
{
|
||||
_codeBuffer = codeBuffer;
|
||||
_decoder = decoder;
|
||||
_length = length;
|
||||
|
||||
// Register all instruction handlers
|
||||
RegisterHandlers();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all instruction handlers
|
||||
/// </summary>
|
||||
private void RegisterHandlers()
|
||||
{
|
||||
// Register specific instruction handlers
|
||||
_handlers.Add(new RetHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new RetImmHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new JmpRel32Handler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new JmpRel8Handler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new CallRel32Handler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new XorRegMemHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new TestRegMemHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new TestAlImmHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new TestEaxImmHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new FnstswHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new ConditionalJumpHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new TwoByteConditionalJumpHandler(_codeBuffer, _decoder, _length));
|
||||
|
||||
// Register group handlers for instructions that share similar decoding logic
|
||||
_handlers.Add(new Group1Handler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new Group3Handler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new FloatingPointHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new DataTransferHandler(_codeBuffer, _decoder, _length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a handler that can decode the given opcode
|
||||
/// </summary>
|
||||
/// <param name="opcode">The opcode to decode</param>
|
||||
/// <returns>A handler that can decode the opcode, or null if no handler is found</returns>
|
||||
public IInstructionHandler? GetHandler(byte opcode)
|
||||
{
|
||||
foreach (var handler in _handlers)
|
||||
{
|
||||
if (handler.CanHandle(opcode))
|
||||
{
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
59
X86Disassembler/X86/Handlers/JmpRel32Handler.cs
Normal file
59
X86Disassembler/X86/Handlers/JmpRel32Handler.cs
Normal file
@ -0,0 +1,59 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for JMP rel32 instruction (0xE9)
|
||||
/// </summary>
|
||||
public class JmpRel32Handler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the JmpRel32Handler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public JmpRel32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0xE9;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a JMP rel32 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 mnemonic
|
||||
instruction.Mnemonic = "jmp";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position + 4 > Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
int offset = BitConverter.ToInt32(CodeBuffer, position);
|
||||
Decoder.SetPosition(position + 4);
|
||||
|
||||
// Calculate the target address
|
||||
uint targetAddress = (uint)(position + offset + 4);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
59
X86Disassembler/X86/Handlers/JmpRel8Handler.cs
Normal file
59
X86Disassembler/X86/Handlers/JmpRel8Handler.cs
Normal file
@ -0,0 +1,59 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for JMP rel8 instruction (0xEB)
|
||||
/// </summary>
|
||||
public class JmpRel8Handler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the JmpRel8Handler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public JmpRel8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0xEB;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a JMP rel8 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 mnemonic
|
||||
instruction.Mnemonic = "jmp";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
sbyte offset = (sbyte)CodeBuffer[position];
|
||||
Decoder.SetPosition(position + 1);
|
||||
|
||||
// Calculate the target address
|
||||
uint targetAddress = (uint)(position + offset + 1);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
45
X86Disassembler/X86/Handlers/RetHandler.cs
Normal file
45
X86Disassembler/X86/Handlers/RetHandler.cs
Normal file
@ -0,0 +1,45 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for RET instruction (0xC3)
|
||||
/// </summary>
|
||||
public class RetHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RetHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public RetHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0xC3;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a RET 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 mnemonic
|
||||
instruction.Mnemonic = "ret";
|
||||
|
||||
// No operands for RET
|
||||
instruction.Operands = string.Empty;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
56
X86Disassembler/X86/Handlers/RetImmHandler.cs
Normal file
56
X86Disassembler/X86/Handlers/RetImmHandler.cs
Normal file
@ -0,0 +1,56 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for RET instruction with immediate operand (0xC2)
|
||||
/// </summary>
|
||||
public class RetImmHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RetImmHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public RetImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0xC2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a RET instruction with immediate operand
|
||||
/// </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 mnemonic
|
||||
instruction.Mnemonic = "ret";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position + 2 > Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the immediate value
|
||||
ushort imm16 = BitConverter.ToUInt16(CodeBuffer, position);
|
||||
Decoder.SetPosition(position + 2);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{imm16:X4}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
56
X86Disassembler/X86/Handlers/TestAlImmHandler.cs
Normal file
56
X86Disassembler/X86/Handlers/TestAlImmHandler.cs
Normal file
@ -0,0 +1,56 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for TEST AL, imm8 instruction (0xA8)
|
||||
/// </summary>
|
||||
public class TestAlImmHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the TestAlImmHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public TestAlImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0xA8;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a TEST AL, imm8 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 mnemonic
|
||||
instruction.Mnemonic = "test";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the immediate value
|
||||
byte imm8 = CodeBuffer[position];
|
||||
Decoder.SetPosition(position + 1);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"al, 0x{imm8:X2}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
56
X86Disassembler/X86/Handlers/TestEaxImmHandler.cs
Normal file
56
X86Disassembler/X86/Handlers/TestEaxImmHandler.cs
Normal file
@ -0,0 +1,56 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for TEST EAX, imm32 instruction (0xA9)
|
||||
/// </summary>
|
||||
public class TestEaxImmHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the TestEaxImmHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public TestEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0xA9;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a TEST EAX, imm32 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 mnemonic
|
||||
instruction.Mnemonic = "test";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position + 4 > Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the immediate value
|
||||
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
|
||||
Decoder.SetPosition(position + 4);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"eax, 0x{imm32:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
82
X86Disassembler/X86/Handlers/TestRegMemHandler.cs
Normal file
82
X86Disassembler/X86/Handlers/TestRegMemHandler.cs
Normal file
@ -0,0 +1,82 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for TEST r/m32, r32 instruction (0x85)
|
||||
/// </summary>
|
||||
public class TestRegMemHandler : InstructionHandler
|
||||
{
|
||||
// ModR/M decoder
|
||||
private readonly ModRMDecoder _modRMDecoder;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the TestRegMemHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public TestRegMemHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
_modRMDecoder = new ModRMDecoder(codeBuffer, decoder, length);
|
||||
}
|
||||
|
||||
/// <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 == 0x85;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a TEST r/m32, r32 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 mnemonic
|
||||
instruction.Mnemonic = "test";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the ModR/M byte
|
||||
byte modRM = CodeBuffer[position++];
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
// Extract the fields from the ModR/M byte
|
||||
byte mod = (byte)((modRM & 0xC0) >> 6);
|
||||
byte reg = (byte)((modRM & 0x38) >> 3);
|
||||
byte rm = (byte)(modRM & 0x07);
|
||||
|
||||
// Decode the destination operand
|
||||
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
|
||||
|
||||
// Get the source register
|
||||
string srcReg = GetRegister32(reg);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destOperand}, {srcReg}";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the 32-bit register name for the given register index
|
||||
/// </summary>
|
||||
/// <param name="reg">The register index</param>
|
||||
/// <returns>The register name</returns>
|
||||
private static string GetRegister32(byte reg)
|
||||
{
|
||||
string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
|
||||
return registerNames[reg & 0x07];
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for two-byte conditional jump instructions (0x0F 0x80-0x8F)
|
||||
/// </summary>
|
||||
public class TwoByteConditionalJumpHandler : InstructionHandler
|
||||
{
|
||||
// Mnemonics for conditional jumps
|
||||
private static readonly string[] ConditionalJumpMnemonics = new string[]
|
||||
{
|
||||
"jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "jnbe",
|
||||
"js", "jns", "jp", "jnp", "jl", "jnl", "jle", "jnle"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the TwoByteConditionalJumpHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public TwoByteConditionalJumpHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
// Two-byte conditional jumps start with 0x0F
|
||||
if (opcode == 0x0F)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
if (position < Length)
|
||||
{
|
||||
byte secondByte = CodeBuffer[position];
|
||||
// Second byte must be in the range 0x80-0x8F
|
||||
return secondByte >= 0x80 && secondByte <= 0x8F;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a two-byte conditional jump 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)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the second byte of the opcode
|
||||
byte secondByte = CodeBuffer[position++];
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
// Get the mnemonic from the table
|
||||
int index = secondByte - 0x80;
|
||||
instruction.Mnemonic = ConditionalJumpMnemonics[index];
|
||||
|
||||
if (position + 4 > Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset (32-bit)
|
||||
int offset = BitConverter.ToInt32(CodeBuffer, position);
|
||||
Decoder.SetPosition(position + 4);
|
||||
|
||||
// Calculate the target address
|
||||
uint targetAddress = (uint)(position + offset + 4);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
56
X86Disassembler/X86/Handlers/XorAlImmHandler.cs
Normal file
56
X86Disassembler/X86/Handlers/XorAlImmHandler.cs
Normal file
@ -0,0 +1,56 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for XOR AL, imm8 instruction (0x34)
|
||||
/// </summary>
|
||||
public class XorAlImmHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the XorAlImmHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public XorAlImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0x34;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a XOR AL, imm8 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 mnemonic
|
||||
instruction.Mnemonic = "xor";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the immediate value
|
||||
byte imm8 = CodeBuffer[position];
|
||||
Decoder.SetPosition(position + 1);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"al, 0x{imm8:X2}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
56
X86Disassembler/X86/Handlers/XorEaxImmHandler.cs
Normal file
56
X86Disassembler/X86/Handlers/XorEaxImmHandler.cs
Normal file
@ -0,0 +1,56 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for XOR EAX, imm32 instruction (0x35)
|
||||
/// </summary>
|
||||
public class XorEaxImmHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the XorEaxImmHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public XorEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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 == 0x35;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a XOR EAX, imm32 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 mnemonic
|
||||
instruction.Mnemonic = "xor";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position + 4 > Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the immediate value
|
||||
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
|
||||
Decoder.SetPosition(position + 4);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"eax, 0x{imm32:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
82
X86Disassembler/X86/Handlers/XorMemRegHandler.cs
Normal file
82
X86Disassembler/X86/Handlers/XorMemRegHandler.cs
Normal file
@ -0,0 +1,82 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for XOR r/m32, r32 instruction (0x31)
|
||||
/// </summary>
|
||||
public class XorMemRegHandler : InstructionHandler
|
||||
{
|
||||
// ModR/M decoder
|
||||
private readonly ModRMDecoder _modRMDecoder;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the XorMemRegHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public XorMemRegHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
_modRMDecoder = new ModRMDecoder(codeBuffer, decoder, length);
|
||||
}
|
||||
|
||||
/// <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 == 0x31;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an XOR r/m32, r32 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 mnemonic
|
||||
instruction.Mnemonic = "xor";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the ModR/M byte
|
||||
byte modRM = CodeBuffer[position++];
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
// Extract the fields from the ModR/M byte
|
||||
byte mod = (byte)((modRM & 0xC0) >> 6);
|
||||
byte reg = (byte)((modRM & 0x38) >> 3);
|
||||
byte rm = (byte)(modRM & 0x07);
|
||||
|
||||
// Decode the destination operand
|
||||
string destOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
|
||||
|
||||
// Get the source register
|
||||
string srcReg = GetRegister32(reg);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destOperand}, {srcReg}";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the 32-bit register name for the given register index
|
||||
/// </summary>
|
||||
/// <param name="reg">The register index</param>
|
||||
/// <returns>The register name</returns>
|
||||
private static string GetRegister32(byte reg)
|
||||
{
|
||||
string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
|
||||
return registerNames[reg & 0x07];
|
||||
}
|
||||
}
|
82
X86Disassembler/X86/Handlers/XorRegMemHandler.cs
Normal file
82
X86Disassembler/X86/Handlers/XorRegMemHandler.cs
Normal file
@ -0,0 +1,82 @@
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for XOR r32, r/m32 instruction (0x33)
|
||||
/// </summary>
|
||||
public class XorRegMemHandler : InstructionHandler
|
||||
{
|
||||
// ModR/M decoder
|
||||
private readonly ModRMDecoder _modRMDecoder;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the XorRegMemHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public XorRegMemHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
_modRMDecoder = new ModRMDecoder(codeBuffer, decoder, length);
|
||||
}
|
||||
|
||||
/// <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 == 0x33;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an XOR r32, r/m32 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 mnemonic
|
||||
instruction.Mnemonic = "xor";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the ModR/M byte
|
||||
byte modRM = CodeBuffer[position++];
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
// Extract the fields from the ModR/M byte
|
||||
byte mod = (byte)((modRM & 0xC0) >> 6);
|
||||
byte reg = (byte)((modRM & 0x38) >> 3);
|
||||
byte rm = (byte)(modRM & 0x07);
|
||||
|
||||
// Decode the source operand
|
||||
string srcOperand = _modRMDecoder.DecodeModRM(mod, rm, false);
|
||||
|
||||
// Get the destination register
|
||||
string destReg = GetRegister32(reg);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destReg}, {srcOperand}";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the 32-bit register name for the given register index
|
||||
/// </summary>
|
||||
/// <param name="reg">The register index</param>
|
||||
/// <returns>The register name</returns>
|
||||
private static string GetRegister32(byte reg)
|
||||
{
|
||||
string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
|
||||
return registerNames[reg & 0x07];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user