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

Simplified CmpImmWithRm32Handler and added AndImmWithRm32Handler

This commit is contained in:
bird_egop 2025-04-13 23:39:57 +03:00
parent 8d1522b6cb
commit ec70b31058
3 changed files with 111 additions and 15 deletions

View File

@ -0,0 +1,87 @@
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
/// Handler for AND r/m32, imm32 instruction (0x81 /4)
/// </summary>
public class AndImmWithRm32Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the AndImmWithRm32Handler 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 AndImmWithRm32Handler(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)
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 4 (AND)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 4; // 4 = AND
}
/// <summary>
/// Decodes an AND r/m32, 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 = "and";
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get the position after decoding the ModR/M byte
int position = Decoder.GetPosition();
// Check if we have enough bytes for the immediate value
if (position + 3 >= Length)
{
return false; // Not enough bytes for the immediate value
}
// Read the immediate value
uint imm32 = Decoder.ReadUInt32();
// Format the destination operand based on addressing mode
string destOperand;
if (mod == 3) // Register addressing mode
{
// Get 32-bit register name
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Memory addressing mode
{
// Memory operand already includes dword ptr prefix
destOperand = memOperand;
}
// Format the immediate value
string immStr = $"0x{imm32:X8}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
return true;
}
}

View File

@ -48,28 +48,36 @@ public class CmpImmWithRm32Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "cmp"; instruction.Mnemonic = "cmp";
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get the position after decoding the ModR/M byte
int position = Decoder.GetPosition(); int position = Decoder.GetPosition();
if (position >= Length) // Check if we have enough bytes for the immediate value
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)
{ {
return false; return false; // Not enough bytes for the immediate value
} }
// Read the immediate value in little-endian format // Read the immediate value
var imm = Decoder.ReadUInt32(); uint imm32 = Decoder.ReadUInt32();
// Format the immediate value as expected by the tests (0x12345678) // Format the destination operand based on addressing mode
// Note: The bytes are reversed to match the expected format in the tests string destOperand;
string immStr = $"0x{imm:X8}"; if (mod == 3) // Register addressing mode
{
// Get 32-bit register name
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Memory addressing mode
{
// Memory operand already includes dword ptr prefix
destOperand = memOperand;
}
// Format the immediate value
string immStr = $"0x{imm32:X8}";
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -373,6 +373,7 @@ public class InstructionHandlerFactory
{ {
// Add AND handlers // Add AND handlers
_handlers.Add(new AndImmToRm8Handler(_codeBuffer, _decoder, _length)); _handlers.Add(new AndImmToRm8Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AndImmWithRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AndImmToRm32Handler(_codeBuffer, _decoder, _length)); _handlers.Add(new AndImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AndImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length)); _handlers.Add(new AndImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));