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

Fixed instruction boundary detection and added JGE instruction handler

This commit is contained in:
bird_egop 2025-04-13 03:19:42 +03:00
parent e12f5b5bdf
commit 17ef78a7a7
3 changed files with 126 additions and 63 deletions

View File

@ -45,15 +45,21 @@ public class CmpImmWithRm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
int position = Decoder.GetPosition();
if (position >= Length)
// Save the original position for raw bytes calculation
int startPosition = Decoder.GetPosition();
// Set the mnemonic
instruction.Mnemonic = "cmp";
if (startPosition >= Length)
{
return false;
instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode };
return true;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
byte modRM = CodeBuffer[startPosition];
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
@ -63,65 +69,51 @@ public class CmpImmWithRm8Handler : InstructionHandler
// CMP r/m8, imm8 is encoded as 80 /7
if (reg != 7)
{
return false;
instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode, modRM };
return true;
}
// Process SIB and displacement bytes if needed
if (mod != 3 && rm == 4) // SIB byte present
// Use ModRMDecoder to decode the ModR/M byte
var (_, _, _, rmOperand) = ModRMDecoder.ReadModRM(false);
// Get the position after decoding the ModR/M byte
int newPosition = Decoder.GetPosition();
// Check if we have enough bytes for the immediate value
if (newPosition >= Length)
{
if (position >= Length)
instruction.Operands = "??";
byte[] rawBytesImm = new byte[newPosition - startPosition + 1]; // +1 for opcode
rawBytesImm[0] = opcode;
for (int i = 0; i < newPosition - startPosition; i++)
{
return false;
if (startPosition + i < Length)
{
rawBytesImm[i + 1] = CodeBuffer[startPosition + i];
}
}
position++; // Skip SIB byte
}
// Handle displacement
if ((mod == 1 && position >= Length) || (mod == 2 && position + 3 >= Length))
{
return false;
}
if (mod == 1) // 8-bit displacement
{
position++;
}
else if (mod == 2) // 32-bit displacement
{
position += 4;
instruction.RawBytes = rawBytesImm;
return true;
}
// Read the immediate byte
if (position >= Length)
{
return false;
}
byte imm8 = CodeBuffer[newPosition];
Decoder.SetPosition(newPosition + 1);
byte imm8 = CodeBuffer[position++];
Decoder.SetPosition(position);
// Set the mnemonic
instruction.Mnemonic = "cmp";
// Get the operand string
// Replace the size prefix with "byte ptr"
string operand;
if (mod != 3) // Memory operand
if (rmOperand.StartsWith("qword ptr "))
{
string memOperand = ModRMDecoder.DecodeModRM(mod, rm, true);
// Replace the size prefix with "byte ptr"
if (memOperand.StartsWith("qword ptr "))
{
operand = memOperand.Replace("qword ptr ", "byte ptr ");
}
else if (memOperand.StartsWith("dword ptr "))
{
operand = memOperand.Replace("dword ptr ", "byte ptr ");
}
else
{
operand = $"byte ptr {memOperand}";
}
operand = rmOperand.Replace("qword ptr ", "byte ptr ");
}
else if (rmOperand.StartsWith("dword ptr "))
{
operand = rmOperand.Replace("dword ptr ", "byte ptr ");
}
else if (mod != 3) // Memory operand without prefix
{
operand = $"byte ptr {rmOperand}";
}
else // Register operand
{
@ -130,6 +122,19 @@ public class CmpImmWithRm8Handler : InstructionHandler
// Set the operands
instruction.Operands = $"{operand}, 0x{imm8:X2}";
// Set the raw bytes
byte[] rawBytes = new byte[newPosition - startPosition + 2]; // +1 for opcode, +1 for immediate
rawBytes[0] = opcode;
for (int i = 0; i < newPosition - startPosition; i++)
{
if (startPosition + i < Length)
{
rawBytes[i + 1] = CodeBuffer[startPosition + i];
}
}
rawBytes[rawBytes.Length - 1] = imm8;
instruction.RawBytes = rawBytes;
return true;
}

View File

@ -104,9 +104,6 @@ public class InstructionHandlerFactory
/// </summary>
private void RegisterArithmeticImmediateHandlers()
{
// Add the Group1SignExtendedHandler first to ensure it has priority for 0x83 opcode
_handlers.Add(new Group1SignExtendedHandler(_codeBuffer, _decoder, _length));
// ADC handlers
_handlers.Add(new AdcImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AdcImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
@ -152,6 +149,7 @@ public class InstructionHandlerFactory
// JMP handlers
_handlers.Add(new JmpRel32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new JmpRel8Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new JgeRel8Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new ConditionalJumpHandler(_codeBuffer, _decoder, _length));
_handlers.Add(new TwoByteConditionalJumpHandler(_codeBuffer, _decoder, _length));
}
@ -358,13 +356,6 @@ public class InstructionHandlerFactory
/// <returns>The handler that can decode the opcode, or null if no handler can decode it</returns>
public IInstructionHandler? GetHandler(byte opcode)
{
// Special case for 0x83 opcode (Group 1 instructions with sign-extended immediate)
if (opcode == 0x83)
{
// Return the Group1SignExtendedHandler directly for 0x83 opcode
return new ArithmeticImmediate.Group1SignExtendedHandler(_codeBuffer, _decoder, _length);
}
// For all other opcodes, use the normal handler selection logic
return _handlers.FirstOrDefault(h => h.CanHandle(opcode));
}

View File

@ -0,0 +1,67 @@
namespace X86Disassembler.X86.Handlers.Jump;
/// <summary>
/// Handler for JGE rel8 instruction (0x7D)
/// </summary>
public class JgeRel8Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the JgeRel8Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public JgeRel8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
return opcode == 0x7D;
}
/// <summary>
/// Decodes a JGE rel8 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Save the original position for raw bytes calculation
int startPosition = Decoder.GetPosition();
// Set the mnemonic
instruction.Mnemonic = "jge";
if (startPosition >= Length)
{
instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode };
return true;
}
// Read the relative offset
sbyte offset = (sbyte)CodeBuffer[startPosition];
Decoder.SetPosition(startPosition + 1);
// Calculate the target address
// The target is calculated from the address of the next instruction (EIP + 2)
// EIP + 2 + offset
uint targetAddress = (uint)(instruction.Address + offset + 2);
// Set the operands
instruction.Operands = $"0x{targetAddress:X8}";
// Set the raw bytes
instruction.RawBytes = new byte[] { opcode, (byte)offset };
return true;
}
}