mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-19 03:41:18 +03:00
Improved PUSH handlers by moving reg field check to CanHandle and adding proper boundary checking
This commit is contained in:
parent
53696a9f1c
commit
e134452eda
@ -37,6 +37,11 @@ public class PushImm32Handler : InstructionHandler
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "push";
|
||||
|
||||
if(!Decoder.CanReadUInt())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the immediate value
|
||||
uint imm32 = Decoder.ReadUInt32();
|
||||
|
||||
|
@ -37,6 +37,11 @@ public class PushImm8Handler : InstructionHandler
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "push";
|
||||
|
||||
if(!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the immediate value
|
||||
byte imm8 = Decoder.ReadByte();
|
||||
|
||||
|
@ -23,7 +23,26 @@ public class PushRm32Handler : InstructionHandler
|
||||
/// <returns>True if this handler can decode the opcode</returns>
|
||||
public override bool CanHandle(byte opcode)
|
||||
{
|
||||
return opcode == 0xFF;
|
||||
// PUSH r/m32 is encoded as FF /6
|
||||
if (opcode != 0xFF)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if we have enough bytes to read the ModR/M byte
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Peek at the ModR/M byte without advancing the position
|
||||
byte modRM = CodeBuffer[Decoder.GetPosition()];
|
||||
|
||||
// Extract the reg field (bits 3-5)
|
||||
byte reg = (byte)((modRM & 0x38) >> 3);
|
||||
|
||||
// PUSH r/m32 is encoded as FF /6 (reg field = 6)
|
||||
return reg == 6;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -34,9 +53,11 @@ public class PushRm32Handler : InstructionHandler
|
||||
/// <returns>True if the instruction was successfully decoded</returns>
|
||||
public override bool Decode(byte opcode, Instruction instruction)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "push";
|
||||
|
||||
if (position >= Length)
|
||||
// Check if we have enough bytes for the ModR/M byte
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -44,15 +65,6 @@ public class PushRm32Handler : InstructionHandler
|
||||
// Read the ModR/M byte
|
||||
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
|
||||
|
||||
// PUSH r/m32 is encoded as FF /6
|
||||
if (reg != RegisterIndex.Sp)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "push";
|
||||
|
||||
// For memory operands, set the operand
|
||||
if (mod != 3) // Memory operand
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user