mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-19 16:08:02 +03:00
Added support for LEA instruction (opcode 0x8D) with tests
This commit is contained in:
@ -3,14 +3,15 @@ using X86Disassembler.X86.Handlers.ArithmeticUnary;
|
||||
using X86Disassembler.X86.Handlers.Call;
|
||||
using X86Disassembler.X86.Handlers.FloatingPoint;
|
||||
using X86Disassembler.X86.Handlers.Jump;
|
||||
using X86Disassembler.X86.Handlers.Lea;
|
||||
using X86Disassembler.X86.Handlers.Mov;
|
||||
using X86Disassembler.X86.Handlers.Or;
|
||||
using X86Disassembler.X86.Handlers.Pop;
|
||||
using X86Disassembler.X86.Handlers.Push;
|
||||
using X86Disassembler.X86.Handlers.Ret;
|
||||
using X86Disassembler.X86.Handlers.Test;
|
||||
using X86Disassembler.X86.Handlers.Xchg;
|
||||
using X86Disassembler.X86.Handlers.Xor;
|
||||
using X86Disassembler.X86.Handlers.Xchg;
|
||||
|
||||
namespace X86Disassembler.X86.Handlers;
|
||||
|
||||
@ -69,6 +70,9 @@ public class InstructionHandlerFactory
|
||||
// Register Or handlers
|
||||
RegisterOrHandlers();
|
||||
|
||||
// Register Lea handlers
|
||||
RegisterLeaHandlers();
|
||||
|
||||
// Register Data Transfer handlers
|
||||
RegisterDataTransferHandlers();
|
||||
|
||||
@ -212,6 +216,15 @@ public class InstructionHandlerFactory
|
||||
_handlers.Add(new OrImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all Lea instruction handlers
|
||||
/// </summary>
|
||||
private void RegisterLeaHandlers()
|
||||
{
|
||||
// Add Lea handlers
|
||||
_handlers.Add(new LeaR32MHandler(_codeBuffer, _decoder, _length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all Data Transfer instruction handlers
|
||||
/// </summary>
|
||||
|
76
X86Disassembler/X86/Handlers/Lea/LeaR32MHandler.cs
Normal file
76
X86Disassembler/X86/Handlers/Lea/LeaR32MHandler.cs
Normal file
@ -0,0 +1,76 @@
|
||||
namespace X86Disassembler.X86.Handlers.Lea;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for LEA r32, m instruction (0x8D)
|
||||
/// </summary>
|
||||
public class LeaR32MHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the LeaR32MHandler 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 LeaR32MHandler(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 == 0x8D;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a LEA r32, m 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 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);
|
||||
|
||||
// LEA only works with memory operands, not registers
|
||||
if (mod == 3)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "lea";
|
||||
|
||||
// Get the register name
|
||||
string regName = GetRegister32(reg);
|
||||
|
||||
// Get the memory operand without the size prefix
|
||||
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
|
||||
|
||||
// Remove the "dword ptr" prefix for LEA instructions
|
||||
operand = operand.Replace("dword ptr ", "");
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{regName}, {operand}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -47,6 +47,9 @@ public static class OpcodeMap
|
||||
OneByteOpcodes[0x0C] = "or"; // OR AL, imm8
|
||||
OneByteOpcodes[0x0D] = "or"; // OR EAX, imm32
|
||||
|
||||
// LEA instruction
|
||||
OneByteOpcodes[0x8D] = "lea"; // LEA r32, m
|
||||
|
||||
// Group 1 instructions (ADD, OR, ADC, SBB, AND, SUB, XOR, CMP)
|
||||
OneByteOpcodes[0x80] = "group1b";
|
||||
OneByteOpcodes[0x81] = "group1d";
|
||||
|
Reference in New Issue
Block a user