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:
parent
f19f2254fe
commit
0ea3294c61
@ -54,41 +54,50 @@ 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)
|
|
||||||
{
|
|
||||||
string rmRegName = ModRMDecoder.GetRegisterName(rm, 32);
|
|
||||||
instruction.Operands = $"{rmRegName}, ??";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
instruction.Operands = $"{memOperand}, ??";
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read and sign-extend the immediate value
|
// Read the immediate value as a signed byte and automatically sign-extend it to int
|
||||||
uint imm32 = (uint)(sbyte)Decoder.ReadByte();
|
int signExtendedImm = (sbyte)Decoder.ReadByte();
|
||||||
|
|
||||||
// Set operands
|
// Format the destination operand based on addressing mode
|
||||||
if (mod == 3)
|
string destOperand;
|
||||||
|
if (mod == 3) // Register addressing mode
|
||||||
{
|
{
|
||||||
string rmRegName = ModRMDecoder.GetRegisterName(rm, 32);
|
// Get 32-bit register name
|
||||||
instruction.Operands = $"{rmRegName}, 0x{imm32:X8}";
|
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}, 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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user