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

Added support for OR r8, r/m8 instruction (opcode 0x0A) with tests

This commit is contained in:
bird_egop 2025-04-13 00:23:11 +03:00
parent 7063a4a5a8
commit 3ffaaf0057
4 changed files with 143 additions and 0 deletions

View File

@ -4,6 +4,7 @@ using X86Disassembler.X86.Handlers.Call;
using X86Disassembler.X86.Handlers.FloatingPoint;
using X86Disassembler.X86.Handlers.Jump;
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;
@ -65,6 +66,9 @@ public class InstructionHandlerFactory
// Register Xor handlers
RegisterXorHandlers();
// Register Or handlers
RegisterOrHandlers();
// Register Data Transfer handlers
RegisterDataTransferHandlers();
@ -195,6 +199,15 @@ public class InstructionHandlerFactory
_handlers.Add(new XorImmWithRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
}
/// <summary>
/// Registers all Or instruction handlers
/// </summary>
private void RegisterOrHandlers()
{
// Add Or handlers
_handlers.Add(new OrR8Rm8Handler(_codeBuffer, _decoder, _length));
}
/// <summary>
/// Registers all Data Transfer instruction handlers
/// </summary>

View File

@ -0,0 +1,75 @@
namespace X86Disassembler.X86.Handlers.Or;
/// <summary>
/// Handler for OR r8, r/m8 instruction (0x0A)
/// </summary>
public class OrR8Rm8Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the OrR8Rm8Handler 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 OrR8Rm8Handler(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 == 0x0A;
}
/// <summary>
/// Decodes an OR 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)
{
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);
// Set the mnemonic
instruction.Mnemonic = "or";
// Get the register name
string regName = GetRegister8(reg);
// For memory operands, set the operand
if (mod != 3) // Memory operand
{
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Replace dword ptr with byte ptr for 8-bit operations
operand = operand.Replace("dword ptr", "byte ptr");
instruction.Operands = $"{regName}, {operand}";
}
else // Register operand
{
string rmName = GetRegister8(rm);
instruction.Operands = $"{regName}, {rmName}";
}
return true;
}
}

View File

@ -41,6 +41,12 @@ public static class OpcodeMap
OneByteOpcodes[0xDE] = "fiadd";
OneByteOpcodes[0xDF] = "fistp";
// OR instructions
OneByteOpcodes[0x0A] = "or"; // OR r8, r/m8
OneByteOpcodes[0x0B] = "or"; // OR r32, r/m32
OneByteOpcodes[0x0C] = "or"; // OR AL, imm8
OneByteOpcodes[0x0D] = "or"; // OR EAX, imm32
// Group 1 instructions (ADD, OR, ADC, SBB, AND, SUB, XOR, CMP)
OneByteOpcodes[0x80] = "group1b";
OneByteOpcodes[0x81] = "group1d";

View File

@ -0,0 +1,49 @@
namespace X86DisassemblerTests;
using System;
using Xunit;
using X86Disassembler.X86;
/// <summary>
/// Tests for OR instruction handlers
/// </summary>
public class OrInstructionTests
{
/// <summary>
/// Tests the OR r8, r/m8 instruction (0x0A)
/// </summary>
[Fact]
public void TestOrR8Rm8()
{
// Arrange
byte[] code = { 0x0A, 0xC8 }; // OR CL, AL
// Act
Disassembler disassembler = new Disassembler(code, 0x1000);
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
Assert.Equal("or", instructions[0].Mnemonic);
Assert.Equal("cl, al", instructions[0].Operands);
}
/// <summary>
/// Tests the OR r8, m8 instruction (0x0A) with memory operand
/// </summary>
[Fact]
public void TestOrR8M8()
{
// Arrange
byte[] code = { 0x0A, 0x00 }; // OR AL, BYTE PTR [EAX]
// Act
Disassembler disassembler = new Disassembler(code, 0x1000);
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
Assert.Equal("or", instructions[0].Mnemonic);
Assert.Equal("al, byte ptr [eax]", instructions[0].Operands);
}
}