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