0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-20 04:11:17 +03:00

Added support for INC r32 instructions (0x40-0x47) with tests

This commit is contained in:
bird_egop 2025-04-13 00:55:20 +03:00
parent 7d23af32fa
commit 402cdc68fb
5 changed files with 142 additions and 14 deletions

View File

@ -0,0 +1,49 @@
namespace X86Disassembler.X86.Handlers.Inc;
/// <summary>
/// Handler for INC r32 instructions (0x40-0x47)
/// </summary>
public class IncRegHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the IncRegHandler 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 IncRegHandler(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)
{
// INC EAX = 0x40, INC ECX = 0x41, ..., INC EDI = 0x47
return opcode >= 0x40 && opcode <= 0x47;
}
/// <summary>
/// Decodes an INC 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)
{
// Calculate the register index (0 for EAX, 1 for ECX, etc.)
byte reg = (byte)(opcode - 0x40);
// Set the mnemonic
instruction.Mnemonic = "inc";
// Set the operand (register name)
instruction.Operands = GetRegister32(reg);
return true;
}
}

View File

@ -5,7 +5,7 @@ using X86Disassembler.X86.Handlers.Call;
using X86Disassembler.X86.Handlers.Cmp;
using X86Disassembler.X86.Handlers.Dec;
using X86Disassembler.X86.Handlers.FloatingPoint;
using X86Disassembler.X86.Handlers.Group5;
using X86Disassembler.X86.Handlers.Inc;
using X86Disassembler.X86.Handlers.Jump;
using X86Disassembler.X86.Handlers.Lea;
using X86Disassembler.X86.Handlers.Mov;
@ -83,12 +83,12 @@ public class InstructionHandlerFactory
// Register Dec handlers
RegisterDecHandlers();
// Register Inc handlers
RegisterIncHandlers();
// Register Add handlers
RegisterAddHandlers();
// Register Group5 handlers
RegisterGroup5Handlers();
// Register Data Transfer handlers
RegisterDataTransferHandlers();
@ -172,6 +172,7 @@ public class InstructionHandlerFactory
{
// Add Call handlers
_handlers.Add(new CallRel32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new CallRm32Handler(_codeBuffer, _decoder, _length));
}
/// <summary>
@ -259,25 +260,25 @@ public class InstructionHandlerFactory
_handlers.Add(new DecRegHandler(_codeBuffer, _decoder, _length));
}
/// <summary>
/// Registers all Inc instruction handlers
/// </summary>
private void RegisterIncHandlers()
{
// Add Inc handlers
_handlers.Add(new IncRegHandler(_codeBuffer, _decoder, _length));
}
/// <summary>
/// Registers all Add instruction handlers
/// </summary>
private void RegisterAddHandlers()
{
// Add Add handlers
// Add ADD handlers
_handlers.Add(new AddR32Rm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AddRm32R32Handler(_codeBuffer, _decoder, _length));
}
/// <summary>
/// Registers all Group5 instruction handlers
/// </summary>
private void RegisterGroup5Handlers()
{
// Add Group5 handlers
_handlers.Add(new CallRm32Handler(_codeBuffer, _decoder, _length));
}
/// <summary>
/// Registers all Data Transfer instruction handlers
/// </summary>

View File

@ -72,6 +72,16 @@ public static class OpcodeMap
OneByteOpcodes[0x4E] = "dec"; // DEC ESI
OneByteOpcodes[0x4F] = "dec"; // DEC EDI
// INC instructions
OneByteOpcodes[0x40] = "inc"; // INC EAX
OneByteOpcodes[0x41] = "inc"; // INC ECX
OneByteOpcodes[0x42] = "inc"; // INC EDX
OneByteOpcodes[0x43] = "inc"; // INC EBX
OneByteOpcodes[0x44] = "inc"; // INC ESP
OneByteOpcodes[0x45] = "inc"; // INC EBP
OneByteOpcodes[0x46] = "inc"; // INC ESI
OneByteOpcodes[0x47] = "inc"; // INC EDI
// ADD instructions
OneByteOpcodes[0x00] = "add"; // ADD r/m8, r8
OneByteOpcodes[0x01] = "add"; // ADD r/m32, r32

View File

@ -0,0 +1,68 @@
namespace X86DisassemblerTests;
using System;
using Xunit;
using X86Disassembler.X86;
/// <summary>
/// Tests for INC instruction handlers
/// </summary>
public class IncInstructionTests
{
/// <summary>
/// Tests the INC EAX instruction (0x40)
/// </summary>
[Fact]
public void TestIncEax()
{
// Arrange
byte[] code = { 0x40 }; // INC EAX
// Act
Disassembler disassembler = new Disassembler(code, 0x1000);
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
Assert.Equal("inc", instructions[0].Mnemonic);
Assert.Equal("eax", instructions[0].Operands);
}
/// <summary>
/// Tests the INC ECX instruction (0x41)
/// </summary>
[Fact]
public void TestIncEcx()
{
// Arrange
byte[] code = { 0x41 }; // INC ECX
// Act
Disassembler disassembler = new Disassembler(code, 0x1000);
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
Assert.Equal("inc", instructions[0].Mnemonic);
Assert.Equal("ecx", instructions[0].Operands);
}
/// <summary>
/// Tests the INC EDI instruction (0x47)
/// </summary>
[Fact]
public void TestIncEdi()
{
// Arrange
byte[] code = { 0x47 }; // INC EDI
// Act
Disassembler disassembler = new Disassembler(code, 0x1000);
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
Assert.Equal("inc", instructions[0].Mnemonic);
Assert.Equal("edi", instructions[0].Operands);
}
}