0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-21 04:41:18 +03:00

Added CmpAlImmHandler for CMP AL, imm8 instruction (0x3C) with tests

This commit is contained in:
bird_egop 2025-04-13 01:30:42 +03:00
parent 9cad5ff95c
commit c701fdb435
9 changed files with 130 additions and 16 deletions

View File

@ -0,0 +1,56 @@
namespace X86Disassembler.X86.Handlers.Cmp;
/// <summary>
/// Handler for CMP AL, imm8 instruction (0x3C)
/// </summary>
public class CmpAlImmHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the CmpAlImmHandler 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 CmpAlImmHandler(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 == 0x3C;
}
/// <summary>
/// Decodes a CMP 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 = "cmp";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the immediate value
byte imm8 = CodeBuffer[position++];
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"al, 0x{imm8:X2}";
return true;
}
}

View File

@ -16,7 +16,6 @@ using X86Disassembler.X86.Handlers.Ret;
using X86Disassembler.X86.Handlers.Test;
using X86Disassembler.X86.Handlers.Xchg;
using X86Disassembler.X86.Handlers.Xor;
using System.Linq;
namespace X86Disassembler.X86.Handlers;
@ -99,11 +98,6 @@ public class InstructionHandlerFactory
/// </summary>
private void RegisterArithmeticImmediateHandlers()
{
// ADD handlers
_handlers.Add(new AddImmToRm8Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AddImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AddImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// ADC handlers
_handlers.Add(new AdcImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AdcImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
@ -119,14 +113,6 @@ public class InstructionHandlerFactory
// SUB handlers
_handlers.Add(new SubImmFromRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new SubImmFromRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// XOR handlers
_handlers.Add(new XorImmWithRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new XorImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
// CMP handlers
_handlers.Add(new CmpImmWithRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new CmpImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
}
/// <summary>
@ -185,8 +171,10 @@ public class InstructionHandlerFactory
_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));
// Add XOR immediate handlers
_handlers.Add(new Xor.XorImmWithRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new Xor.XorImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
}
/// <summary>
@ -222,6 +210,11 @@ public class InstructionHandlerFactory
// Add Cmp handlers
_handlers.Add(new CmpR32Rm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new CmpImmWithRm8Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new CmpAlImmHandler(_codeBuffer, _decoder, _length));
// Add CMP immediate handlers from ArithmeticImmediate namespace
_handlers.Add(new CmpImmWithRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new CmpImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
}
/// <summary>
@ -250,6 +243,11 @@ public class InstructionHandlerFactory
// Add ADD handlers
_handlers.Add(new AddR32Rm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AddRm32R32Handler(_codeBuffer, _decoder, _length));
// Add ADD immediate handlers from ArithmeticImmediate namespace
_handlers.Add(new AddImmToRm8Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AddImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AddImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
}
/// <summary>

View File

@ -0,0 +1,54 @@
namespace X86DisassemblerTests;
using System;
using Xunit;
using X86Disassembler.X86;
using X86Disassembler.X86.Handlers;
using X86Disassembler.X86.Handlers.Cmp;
/// <summary>
/// Tests for CMP instruction handlers
/// </summary>
public class CmpInstructionHandlerTests
{
/// <summary>
/// Tests the CmpAlImmHandler for decoding CMP AL, imm8 instructions
/// </summary>
[Fact]
public void CmpAlImmHandler_DecodesCmpAlImm8_Correctly()
{
// Arrange
// CMP AL, 0x03 (3C 03)
byte[] codeBuffer = new byte[] { 0x3C, 0x03 };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
// Act
var instruction = decoder.DecodeInstruction();
// Assert
Assert.NotNull(instruction);
Assert.Equal("cmp", instruction.Mnemonic);
// The handler should produce "al, 0xXX" as the operands
Assert.Equal("al, 0x03", instruction.Operands);
}
/// <summary>
/// Tests the CmpAlImmHandler with a different immediate value
/// </summary>
[Fact]
public void CmpAlImmHandler_DecodesCmpAlImm8_WithDifferentValue()
{
// Arrange
// CMP AL, 0xFF (3C FF)
byte[] codeBuffer = new byte[] { 0x3C, 0xFF };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
// Act
var instruction = decoder.DecodeInstruction();
// Assert
Assert.NotNull(instruction);
Assert.Equal("cmp", instruction.Mnemonic);
Assert.Equal("al, 0xFF", instruction.Operands);
}
}

View File

@ -0,0 +1,6 @@
namespace X86DisassemblerTests;
public class InstructionHandlerFactoryTests
{
}