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

Simplified AndImmToRm32SignExtendedHandler for better maintainability and consistency

This commit is contained in:
bird_egop 2025-04-13 23:18:38 +03:00
parent f19f2254fe
commit 0ea3294c61

View File

@ -54,41 +54,50 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
// Set the mnemonic
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Read immediate value
// 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 >= Length)
{
// Incomplete instruction
if (mod == 3)
{
string rmRegName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{rmRegName}, ??";
}
else
{
instruction.Operands = $"{memOperand}, ??";
}
return true;
return false; // Not enough bytes for the immediate value
}
// Read and sign-extend the immediate value
uint imm32 = (uint)(sbyte)Decoder.ReadByte();
// Read the immediate value as a signed byte and automatically sign-extend it to int
int signExtendedImm = (sbyte)Decoder.ReadByte();
// Set operands
if (mod == 3)
// Format the destination operand based on addressing mode
string destOperand;
if (mod == 3) // Register addressing mode
{
string rmRegName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{rmRegName}, 0x{imm32:X8}";
// 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;
if (signExtendedImm < 0)
{
// For negative values, use the full 32-bit representation
immStr = $"0x{(uint)signExtendedImm:X8}";
}
else
{
instruction.Operands = $"{memOperand}, 0x{imm32:X8}";
// For positive values, use the regular format with leading zeros
immStr = $"0x{signExtendedImm:X8}";
}
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
return true;
}
}