mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-19 20:01:17 +03:00
Fixed FnstswHandler test by registering the handler in InstructionHandlerFactory
This commit is contained in:
parent
c027adc113
commit
acccf5169a
@ -1,23 +1,26 @@
|
|||||||
using X86Disassembler.X86.Handlers.Call;
|
using X86Disassembler.X86.Handlers.Call;
|
||||||
|
using X86Disassembler.X86.Handlers.Group1;
|
||||||
|
using X86Disassembler.X86.Handlers.Group3;
|
||||||
using X86Disassembler.X86.Handlers.Jump;
|
using X86Disassembler.X86.Handlers.Jump;
|
||||||
|
using X86Disassembler.X86.Handlers.Mov;
|
||||||
|
using X86Disassembler.X86.Handlers.Pop;
|
||||||
|
using X86Disassembler.X86.Handlers.Push;
|
||||||
using X86Disassembler.X86.Handlers.Ret;
|
using X86Disassembler.X86.Handlers.Ret;
|
||||||
using X86Disassembler.X86.Handlers.Test;
|
using X86Disassembler.X86.Handlers.Test;
|
||||||
|
using X86Disassembler.X86.Handlers.Xchg;
|
||||||
using X86Disassembler.X86.Handlers.Xor;
|
using X86Disassembler.X86.Handlers.Xor;
|
||||||
|
|
||||||
namespace X86Disassembler.X86.Handlers;
|
namespace X86Disassembler.X86.Handlers;
|
||||||
|
|
||||||
using X86Disassembler.X86.Handlers.Group1;
|
|
||||||
using X86Disassembler.X86.Handlers.Group3;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Factory for creating instruction handlers
|
/// Factory for creating instruction handlers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class InstructionHandlerFactory
|
public class InstructionHandlerFactory
|
||||||
{
|
{
|
||||||
|
private readonly List<IInstructionHandler> _handlers = new();
|
||||||
private readonly byte[] _codeBuffer;
|
private readonly byte[] _codeBuffer;
|
||||||
private readonly InstructionDecoder _decoder;
|
private readonly InstructionDecoder _decoder;
|
||||||
private readonly int _length;
|
private readonly int _length;
|
||||||
private readonly List<IInstructionHandler> _handlers = [];
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the InstructionHandlerFactory class
|
/// Initializes a new instance of the InstructionHandlerFactory class
|
||||||
@ -31,7 +34,6 @@ public class InstructionHandlerFactory
|
|||||||
_decoder = decoder;
|
_decoder = decoder;
|
||||||
_length = length;
|
_length = length;
|
||||||
|
|
||||||
// Register all instruction handlers
|
|
||||||
RegisterHandlers();
|
RegisterHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,49 +42,38 @@ public class InstructionHandlerFactory
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void RegisterHandlers()
|
private void RegisterHandlers()
|
||||||
{
|
{
|
||||||
// Register Group3 handlers first to ensure they take precedence
|
// Register group handlers
|
||||||
// over generic handlers for the same opcodes
|
|
||||||
RegisterGroup3Handlers();
|
RegisterGroup3Handlers();
|
||||||
|
|
||||||
// Register Group1 handlers
|
|
||||||
RegisterGroup1Handlers();
|
RegisterGroup1Handlers();
|
||||||
|
|
||||||
// Register specific instruction handlers
|
// Register specific instruction handlers
|
||||||
_handlers.Add(new Int3Handler(_codeBuffer, _decoder, _length));
|
_handlers.Add(new Int3Handler(_codeBuffer, _decoder, _length));
|
||||||
|
|
||||||
_handlers.Add(new RetHandler(_codeBuffer, _decoder, _length));
|
// Register Return handlers
|
||||||
_handlers.Add(new RetImmHandler(_codeBuffer, _decoder, _length));
|
RegisterReturnHandlers();
|
||||||
_handlers.Add(new CallRel32Handler(_codeBuffer, _decoder, _length));
|
|
||||||
|
|
||||||
// XOR handlers
|
// Register Call handlers
|
||||||
_handlers.Add(new XorRegMemHandler(_codeBuffer, _decoder, _length));
|
RegisterCallHandlers();
|
||||||
_handlers.Add(new XorMemRegHandler(_codeBuffer, _decoder, _length));
|
|
||||||
_handlers.Add(new XorAlImmHandler(_codeBuffer, _decoder, _length));
|
|
||||||
_handlers.Add(new XorEaxImmHandler(_codeBuffer, _decoder, _length));
|
|
||||||
|
|
||||||
|
// Register Jump handlers
|
||||||
|
RegisterJumpHandlers();
|
||||||
|
|
||||||
|
// Register Test handlers
|
||||||
|
RegisterTestHandlers();
|
||||||
|
|
||||||
|
// Register Xor handlers
|
||||||
|
RegisterXorHandlers();
|
||||||
|
|
||||||
|
// Register Data Transfer handlers
|
||||||
|
RegisterDataTransferHandlers();
|
||||||
|
|
||||||
|
// Register floating point handlers
|
||||||
_handlers.Add(new FnstswHandler(_codeBuffer, _decoder, _length));
|
_handlers.Add(new FnstswHandler(_codeBuffer, _decoder, _length));
|
||||||
|
|
||||||
// TEST handlers
|
|
||||||
_handlers.Add(new TestImmWithRm32Handler(_codeBuffer, _decoder, _length));
|
|
||||||
_handlers.Add(new TestImmWithRm8Handler(_codeBuffer, _decoder, _length));
|
|
||||||
_handlers.Add(new TestRegMem8Handler(_codeBuffer, _decoder, _length));
|
|
||||||
_handlers.Add(new TestRegMemHandler(_codeBuffer, _decoder, _length));
|
|
||||||
_handlers.Add(new TestAlImmHandler(_codeBuffer, _decoder, _length));
|
|
||||||
_handlers.Add(new TestEaxImmHandler(_codeBuffer, _decoder, _length));
|
|
||||||
|
|
||||||
// JMP handlers
|
|
||||||
_handlers.Add(new JmpRel32Handler(_codeBuffer, _decoder, _length));
|
|
||||||
_handlers.Add(new JmpRel8Handler(_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 FloatingPointHandler(_codeBuffer, _decoder, _length));
|
_handlers.Add(new FloatingPointHandler(_codeBuffer, _decoder, _length));
|
||||||
_handlers.Add(new DataTransferHandler(_codeBuffer, _decoder, _length));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers the Group1 handlers
|
/// Registers all Group1 instruction handlers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void RegisterGroup1Handlers()
|
private void RegisterGroup1Handlers()
|
||||||
{
|
{
|
||||||
@ -122,7 +113,7 @@ public class InstructionHandlerFactory
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers the Group3 handlers
|
/// Registers all Group3 instruction handlers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void RegisterGroup3Handlers()
|
private void RegisterGroup3Handlers()
|
||||||
{
|
{
|
||||||
@ -146,20 +137,96 @@ public class InstructionHandlerFactory
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a handler that can decode the given opcode
|
/// Registers all Return instruction handlers
|
||||||
|
/// </summary>
|
||||||
|
private void RegisterReturnHandlers()
|
||||||
|
{
|
||||||
|
// Add Return handlers
|
||||||
|
_handlers.Add(new RetHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new RetImmHandler(_codeBuffer, _decoder, _length));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers all Call instruction handlers
|
||||||
|
/// </summary>
|
||||||
|
private void RegisterCallHandlers()
|
||||||
|
{
|
||||||
|
// Add Call handlers
|
||||||
|
_handlers.Add(new CallRel32Handler(_codeBuffer, _decoder, _length));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers all Jump instruction handlers
|
||||||
|
/// </summary>
|
||||||
|
private void RegisterJumpHandlers()
|
||||||
|
{
|
||||||
|
// JMP handlers
|
||||||
|
_handlers.Add(new JmpRel32Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new JmpRel8Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new ConditionalJumpHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new TwoByteConditionalJumpHandler(_codeBuffer, _decoder, _length));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers all Test instruction handlers
|
||||||
|
/// </summary>
|
||||||
|
private void RegisterTestHandlers()
|
||||||
|
{
|
||||||
|
// TEST handlers
|
||||||
|
_handlers.Add(new TestImmWithRm32Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new TestImmWithRm8Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new TestRegMem8Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new TestRegMemHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new TestAlImmHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new TestEaxImmHandler(_codeBuffer, _decoder, _length));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers all Xor instruction handlers
|
||||||
|
/// </summary>
|
||||||
|
private void RegisterXorHandlers()
|
||||||
|
{
|
||||||
|
// Add Xor handlers
|
||||||
|
_handlers.Add(new XorAlImmHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new XorEaxImmHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new XorMemRegHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new XorRegMemHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new XorImmWithRm32Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new XorImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers all Data Transfer instruction handlers
|
||||||
|
/// </summary>
|
||||||
|
private void RegisterDataTransferHandlers()
|
||||||
|
{
|
||||||
|
// Add MOV handlers
|
||||||
|
_handlers.Add(new MovRegMemHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new MovMemRegHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new MovRegImm32Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new MovRegImm8Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new MovEaxMoffsHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new MovMoffsEaxHandler(_codeBuffer, _decoder, _length));
|
||||||
|
|
||||||
|
// Add PUSH handlers
|
||||||
|
_handlers.Add(new PushRegHandler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new PushImm32Handler(_codeBuffer, _decoder, _length));
|
||||||
|
_handlers.Add(new PushImm8Handler(_codeBuffer, _decoder, _length));
|
||||||
|
|
||||||
|
// Add POP handlers
|
||||||
|
_handlers.Add(new PopRegHandler(_codeBuffer, _decoder, _length));
|
||||||
|
|
||||||
|
// Add XCHG handlers
|
||||||
|
_handlers.Add(new XchgEaxRegHandler(_codeBuffer, _decoder, _length));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the handler that can decode the given opcode
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="opcode">The opcode to decode</param>
|
/// <param name="opcode">The opcode to decode</param>
|
||||||
/// <returns>A handler that can decode the opcode, or null if no handler is found</returns>
|
/// <returns>The handler that can decode the opcode, or null if no handler can decode it</returns>
|
||||||
public IInstructionHandler? GetHandler(byte opcode)
|
public IInstructionHandler? GetHandler(byte opcode)
|
||||||
{
|
{
|
||||||
foreach (var handler in _handlers)
|
return _handlers.FirstOrDefault(h => h.CanHandle(opcode));
|
||||||
{
|
|
||||||
if (handler.CanHandle(opcode))
|
|
||||||
{
|
|
||||||
return handler;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
56
X86Disassembler/X86/Handlers/Mov/MovEaxMoffsHandler.cs
Normal file
56
X86Disassembler/X86/Handlers/Mov/MovEaxMoffsHandler.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Mov;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for MOV EAX, moffs32 instruction (0xA1) and MOV AL, moffs8 instruction (0xA0)
|
||||||
|
/// </summary>
|
||||||
|
public class MovEaxMoffsHandler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the MovEaxMoffsHandler 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 MovEaxMoffsHandler(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 == 0xA0 || opcode == 0xA1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a MOV EAX, moffs32 or MOV AL, moffs8 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 = "mov";
|
||||||
|
|
||||||
|
// Get the operand size and register name
|
||||||
|
int operandSize = (opcode == 0xA0) ? 8 : 32;
|
||||||
|
string regName = (opcode == 0xA0) ? "al" : "eax";
|
||||||
|
|
||||||
|
// Read the memory offset
|
||||||
|
uint offset = Decoder.ReadUInt32();
|
||||||
|
if (Decoder.GetPosition() > Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = $"{regName}, [0x{offset:X}]";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
70
X86Disassembler/X86/Handlers/Mov/MovMemRegHandler.cs
Normal file
70
X86Disassembler/X86/Handlers/Mov/MovMemRegHandler.cs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Mov;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for MOV r/m32, r32 instruction (0x89) and MOV r/m8, r8 instruction (0x88)
|
||||||
|
/// </summary>
|
||||||
|
public class MovMemRegHandler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the MovMemRegHandler 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 MovMemRegHandler(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 == 0x88 || opcode == 0x89;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a MOV r/m32, r32 or MOV r/m8, r8 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 = "mov";
|
||||||
|
|
||||||
|
int position = Decoder.GetPosition();
|
||||||
|
|
||||||
|
if (position >= Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine operand size (0 = 8-bit, 1 = 32-bit)
|
||||||
|
bool operandSize32 = (opcode & 0x01) != 0;
|
||||||
|
int operandSize = operandSize32 ? 32 : 8;
|
||||||
|
|
||||||
|
// Read the ModR/M byte
|
||||||
|
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
|
||||||
|
|
||||||
|
// Get register name based on size
|
||||||
|
string regName = ModRMDecoder.GetRegisterName(reg, operandSize);
|
||||||
|
|
||||||
|
// For mod == 3, both operands are registers
|
||||||
|
if (mod == 3)
|
||||||
|
{
|
||||||
|
string rmRegName = ModRMDecoder.GetRegisterName(rm, operandSize);
|
||||||
|
instruction.Operands = $"{regName}, {rmRegName}";
|
||||||
|
}
|
||||||
|
else // Memory operand
|
||||||
|
{
|
||||||
|
instruction.Operands = $"{regName}, {memOperand}";
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
56
X86Disassembler/X86/Handlers/Mov/MovMoffsEaxHandler.cs
Normal file
56
X86Disassembler/X86/Handlers/Mov/MovMoffsEaxHandler.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Mov;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for MOV moffs32, EAX instruction (0xA3) and MOV moffs8, AL instruction (0xA2)
|
||||||
|
/// </summary>
|
||||||
|
public class MovMoffsEaxHandler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the MovMoffsEaxHandler 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 MovMoffsEaxHandler(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 == 0xA2 || opcode == 0xA3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a MOV moffs32, EAX or MOV moffs8, AL 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 = "mov";
|
||||||
|
|
||||||
|
// Get the operand size and register name
|
||||||
|
int operandSize = (opcode == 0xA2) ? 8 : 32;
|
||||||
|
string regName = (opcode == 0xA2) ? "al" : "eax";
|
||||||
|
|
||||||
|
// Read the memory offset
|
||||||
|
uint offset = Decoder.ReadUInt32();
|
||||||
|
if (Decoder.GetPosition() > Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = $"[0x{offset:X}], {regName}";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
56
X86Disassembler/X86/Handlers/Mov/MovRegImm32Handler.cs
Normal file
56
X86Disassembler/X86/Handlers/Mov/MovRegImm32Handler.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Mov;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for MOV r32, imm32 instruction (0xB8-0xBF)
|
||||||
|
/// </summary>
|
||||||
|
public class MovRegImm32Handler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the MovRegImm32Handler 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 MovRegImm32Handler(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 >= 0xB8 && opcode <= 0xBF;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a MOV r32, 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 = "mov";
|
||||||
|
|
||||||
|
// Register is encoded in the low 3 bits of the opcode
|
||||||
|
int reg = opcode & 0x07;
|
||||||
|
string regName = ModRMDecoder.GetRegisterName(reg, 32);
|
||||||
|
|
||||||
|
// Read the immediate value
|
||||||
|
uint imm32 = Decoder.ReadUInt32();
|
||||||
|
if (Decoder.GetPosition() > Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = $"{regName}, 0x{imm32:X}";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
56
X86Disassembler/X86/Handlers/Mov/MovRegImm8Handler.cs
Normal file
56
X86Disassembler/X86/Handlers/Mov/MovRegImm8Handler.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Mov;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for MOV r8, imm8 instruction (0xB0-0xB7)
|
||||||
|
/// </summary>
|
||||||
|
public class MovRegImm8Handler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the MovRegImm8Handler 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 MovRegImm8Handler(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 >= 0xB0 && opcode <= 0xB7;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a MOV r8, 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 = "mov";
|
||||||
|
|
||||||
|
// Register is encoded in the low 3 bits of the opcode
|
||||||
|
int reg = opcode & 0x07;
|
||||||
|
string regName = ModRMDecoder.GetRegisterName(reg, 8);
|
||||||
|
|
||||||
|
// Read the immediate value
|
||||||
|
byte imm8 = Decoder.ReadByte();
|
||||||
|
if (Decoder.GetPosition() > Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = $"{regName}, 0x{imm8:X2}";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
70
X86Disassembler/X86/Handlers/Mov/MovRegMemHandler.cs
Normal file
70
X86Disassembler/X86/Handlers/Mov/MovRegMemHandler.cs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Mov;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for MOV r32, r/m32 instruction (0x8B) and MOV r8, r/m8 instruction (0x8A)
|
||||||
|
/// </summary>
|
||||||
|
public class MovRegMemHandler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the MovRegMemHandler 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 MovRegMemHandler(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 == 0x8A || opcode == 0x8B;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a MOV r32, r/m32 or MOV r8, r/m8 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 = "mov";
|
||||||
|
|
||||||
|
int position = Decoder.GetPosition();
|
||||||
|
|
||||||
|
if (position >= Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine operand size (0 = 8-bit, 1 = 32-bit)
|
||||||
|
bool operandSize32 = (opcode & 0x01) != 0;
|
||||||
|
int operandSize = operandSize32 ? 32 : 8;
|
||||||
|
|
||||||
|
// Read the ModR/M byte
|
||||||
|
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
|
||||||
|
|
||||||
|
// Get register name based on size
|
||||||
|
string regName = ModRMDecoder.GetRegisterName(reg, operandSize);
|
||||||
|
|
||||||
|
// For mod == 3, both operands are registers
|
||||||
|
if (mod == 3)
|
||||||
|
{
|
||||||
|
string rmRegName = ModRMDecoder.GetRegisterName(rm, operandSize);
|
||||||
|
instruction.Operands = $"{rmRegName}, {regName}";
|
||||||
|
}
|
||||||
|
else // Memory operand
|
||||||
|
{
|
||||||
|
instruction.Operands = $"{memOperand}, {regName}";
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
49
X86Disassembler/X86/Handlers/Pop/PopRegHandler.cs
Normal file
49
X86Disassembler/X86/Handlers/Pop/PopRegHandler.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Pop;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for POP r32 instruction (0x58-0x5F)
|
||||||
|
/// </summary>
|
||||||
|
public class PopRegHandler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the PopRegHandler 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 PopRegHandler(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 >= 0x58 && opcode <= 0x5F;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a POP 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 = "pop";
|
||||||
|
|
||||||
|
// Register is encoded in the low 3 bits of the opcode
|
||||||
|
int reg = opcode & 0x07;
|
||||||
|
string regName = ModRMDecoder.GetRegisterName(reg, 32);
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = regName;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
52
X86Disassembler/X86/Handlers/Push/PushImm32Handler.cs
Normal file
52
X86Disassembler/X86/Handlers/Push/PushImm32Handler.cs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Push;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for PUSH imm32 instruction (0x68)
|
||||||
|
/// </summary>
|
||||||
|
public class PushImm32Handler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the PushImm32Handler 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 PushImm32Handler(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 == 0x68;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a PUSH 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 = "push";
|
||||||
|
|
||||||
|
// Read the immediate value
|
||||||
|
uint imm32 = Decoder.ReadUInt32();
|
||||||
|
if (Decoder.GetPosition() > Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = $"0x{imm32:X}";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
52
X86Disassembler/X86/Handlers/Push/PushImm8Handler.cs
Normal file
52
X86Disassembler/X86/Handlers/Push/PushImm8Handler.cs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Push;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for PUSH imm8 instruction (0x6A)
|
||||||
|
/// </summary>
|
||||||
|
public class PushImm8Handler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the PushImm8Handler 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 PushImm8Handler(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 == 0x6A;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a PUSH 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 = "push";
|
||||||
|
|
||||||
|
// Read the immediate value
|
||||||
|
byte imm8 = Decoder.ReadByte();
|
||||||
|
if (Decoder.GetPosition() > Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = $"0x{imm8:X2}";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
49
X86Disassembler/X86/Handlers/Push/PushRegHandler.cs
Normal file
49
X86Disassembler/X86/Handlers/Push/PushRegHandler.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Push;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for PUSH r32 instruction (0x50-0x57)
|
||||||
|
/// </summary>
|
||||||
|
public class PushRegHandler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the PushRegHandler 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 PushRegHandler(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 >= 0x50 && opcode <= 0x57;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes a PUSH 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 = "push";
|
||||||
|
|
||||||
|
// Register is encoded in the low 3 bits of the opcode
|
||||||
|
int reg = opcode & 0x07;
|
||||||
|
string regName = ModRMDecoder.GetRegisterName(reg, 32);
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = regName;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
57
X86Disassembler/X86/Handlers/Xchg/XchgEaxRegHandler.cs
Normal file
57
X86Disassembler/X86/Handlers/Xchg/XchgEaxRegHandler.cs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
namespace X86Disassembler.X86.Handlers.Xchg;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for XCHG EAX, r32 instruction (0x90-0x97)
|
||||||
|
/// </summary>
|
||||||
|
public class XchgEaxRegHandler : InstructionHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the XchgEaxRegHandler 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 XchgEaxRegHandler(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 >= 0x90 && opcode <= 0x97;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes an XCHG EAX, 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)
|
||||||
|
{
|
||||||
|
// Special case for NOP (XCHG EAX, EAX)
|
||||||
|
if (opcode == 0x90)
|
||||||
|
{
|
||||||
|
instruction.Mnemonic = "nop";
|
||||||
|
instruction.Operands = "";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the mnemonic
|
||||||
|
instruction.Mnemonic = "xchg";
|
||||||
|
|
||||||
|
// Register is encoded in the low 3 bits of the opcode
|
||||||
|
int reg = opcode & 0x07;
|
||||||
|
string regName = ModRMDecoder.GetRegisterName(reg, 32);
|
||||||
|
|
||||||
|
// Set the operands
|
||||||
|
instruction.Operands = $"eax, {regName}";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user