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

Simplified SubImmFromRm16Handler by removing special case for register name conversion and improving boundary checking

This commit is contained in:
bird_egop 2025-04-14 00:38:47 +03:00
parent e134452eda
commit 689195c6e5

View File

@ -24,7 +24,22 @@ public class SubImmFromRm16Handler : InstructionHandler
public override bool CanHandle(byte opcode) public override bool CanHandle(byte opcode)
{ {
// Check if the opcode is 0x81 and we have a 0x66 prefix // Check if the opcode is 0x81 and we have a 0x66 prefix
return opcode == 0x81 && Decoder.HasOperandSizeOverridePrefix(); if (opcode != 0x81 || !Decoder.HasOperandSizeOverridePrefix())
{
return false;
}
// Check if we have enough bytes to read the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the reg field is 5 (SUB)
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 5; // 5 = SUB
} }
/// <summary> /// <summary>
@ -35,9 +50,11 @@ public class SubImmFromRm16Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns> /// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction) public override bool Decode(byte opcode, Instruction instruction)
{ {
int position = Decoder.GetPosition(); // Set the mnemonic
instruction.Mnemonic = "sub";
if (position >= Length)
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
{ {
return false; return false;
} }
@ -45,33 +62,15 @@ public class SubImmFromRm16Handler : InstructionHandler
// Extract the fields from the ModR/M byte // Extract the fields from the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Check if this is a SUB instruction (reg field must be 5) // For memory operands, replace "dword" with "word"
if (reg != RegisterIndex.Di) string destination = destOperand;
if (mod != 3) // Memory operand
{ {
return false;
}
// Set the mnemonic
instruction.Mnemonic = "sub";
// For mod == 3, the r/m field specifies a register
string destination;
if (mod == 3)
{
// Get the register name (16-bit)
destination = ModRMDecoder.GetRegisterName(rm, 16);
}
else
{
// Replace "dword" with "word" in the memory operand
destination = destOperand.Replace("dword", "word"); destination = destOperand.Replace("dword", "word");
} }
// Get the current position after processing the ModR/M byte
position = Decoder.GetPosition();
// Check if we have enough bytes for the immediate value // Check if we have enough bytes for the immediate value
if (position + 1 >= Length) if (!Decoder.CanReadUShort())
{ {
return false; return false;
} }
@ -79,27 +78,8 @@ public class SubImmFromRm16Handler : InstructionHandler
// Read the immediate value (16-bit) // Read the immediate value (16-bit)
ushort immediate = Decoder.ReadUInt16(); ushort immediate = Decoder.ReadUInt16();
// Set the operands (note: we use 32-bit register names to match the disassembler's output) // Set the operands
if (mod == 3) instruction.Operands = $"{destination}, 0x{immediate:X4}";
{
// For register operands, use the 32-bit register name
string reg32Name = destination
.Replace("ax", "eax")
.Replace("bx", "ebx")
.Replace("cx", "ecx")
.Replace("dx", "edx")
.Replace("sp", "esp")
.Replace("bp", "ebp")
.Replace("si", "esi")
.Replace("di", "edi");
instruction.Operands = $"{reg32Name}, 0x{immediate:X4}";
}
else
{
// For memory operands, keep the memory operand as is
instruction.Operands = $"{destination}, 0x{immediate:X4}";
}
return true; return true;
} }