mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-19 20:01:17 +03:00
Fixed instruction boundary detection and added JGE instruction handler
This commit is contained in:
parent
e12f5b5bdf
commit
17ef78a7a7
@ -45,15 +45,21 @@ public class CmpImmWithRm8Handler : 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();
|
// Save the original position for raw bytes calculation
|
||||||
|
int startPosition = Decoder.GetPosition();
|
||||||
|
|
||||||
if (position >= Length)
|
// 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
|
// Read the ModR/M byte
|
||||||
byte modRM = CodeBuffer[position++];
|
byte modRM = CodeBuffer[startPosition];
|
||||||
|
|
||||||
// Extract the fields from the ModR/M byte
|
// Extract the fields from the ModR/M byte
|
||||||
byte mod = (byte)((modRM & 0xC0) >> 6);
|
byte mod = (byte)((modRM & 0xC0) >> 6);
|
||||||
@ -63,65 +69,51 @@ public class CmpImmWithRm8Handler : InstructionHandler
|
|||||||
// CMP r/m8, imm8 is encoded as 80 /7
|
// CMP r/m8, imm8 is encoded as 80 /7
|
||||||
if (reg != 7)
|
if (reg != 7)
|
||||||
{
|
{
|
||||||
return false;
|
instruction.Operands = "??";
|
||||||
|
instruction.RawBytes = new byte[] { opcode, modRM };
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process SIB and displacement bytes if needed
|
// Use ModRMDecoder to decode the ModR/M byte
|
||||||
if (mod != 3 && rm == 4) // SIB byte present
|
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
|
instruction.RawBytes = rawBytesImm;
|
||||||
}
|
return true;
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the immediate byte
|
// Read the immediate byte
|
||||||
if (position >= Length)
|
byte imm8 = CodeBuffer[newPosition];
|
||||||
{
|
Decoder.SetPosition(newPosition + 1);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
byte imm8 = CodeBuffer[position++];
|
// Replace the size prefix with "byte ptr"
|
||||||
Decoder.SetPosition(position);
|
|
||||||
|
|
||||||
// Set the mnemonic
|
|
||||||
instruction.Mnemonic = "cmp";
|
|
||||||
|
|
||||||
// Get the operand string
|
|
||||||
string operand;
|
string operand;
|
||||||
if (mod != 3) // Memory operand
|
if (rmOperand.StartsWith("qword ptr "))
|
||||||
{
|
{
|
||||||
string memOperand = ModRMDecoder.DecodeModRM(mod, rm, true);
|
operand = rmOperand.Replace("qword ptr ", "byte ptr ");
|
||||||
|
}
|
||||||
// Replace the size prefix with "byte ptr"
|
else if (rmOperand.StartsWith("dword ptr "))
|
||||||
if (memOperand.StartsWith("qword ptr "))
|
{
|
||||||
{
|
operand = rmOperand.Replace("dword ptr ", "byte ptr ");
|
||||||
operand = memOperand.Replace("qword ptr ", "byte ptr ");
|
}
|
||||||
}
|
else if (mod != 3) // Memory operand without prefix
|
||||||
else if (memOperand.StartsWith("dword ptr "))
|
{
|
||||||
{
|
operand = $"byte ptr {rmOperand}";
|
||||||
operand = memOperand.Replace("dword ptr ", "byte ptr ");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
operand = $"byte ptr {memOperand}";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else // Register operand
|
else // Register operand
|
||||||
{
|
{
|
||||||
@ -131,6 +123,19 @@ public class CmpImmWithRm8Handler : InstructionHandler
|
|||||||
// Set the operands
|
// Set the operands
|
||||||
instruction.Operands = $"{operand}, 0x{imm8:X2}";
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,9 +104,6 @@ public class InstructionHandlerFactory
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void RegisterArithmeticImmediateHandlers()
|
private void RegisterArithmeticImmediateHandlers()
|
||||||
{
|
{
|
||||||
// Add the Group1SignExtendedHandler first to ensure it has priority for 0x83 opcode
|
|
||||||
_handlers.Add(new Group1SignExtendedHandler(_codeBuffer, _decoder, _length));
|
|
||||||
|
|
||||||
// ADC handlers
|
// ADC handlers
|
||||||
_handlers.Add(new AdcImmToRm32Handler(_codeBuffer, _decoder, _length));
|
_handlers.Add(new AdcImmToRm32Handler(_codeBuffer, _decoder, _length));
|
||||||
_handlers.Add(new AdcImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
|
_handlers.Add(new AdcImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
|
||||||
@ -152,6 +149,7 @@ public class InstructionHandlerFactory
|
|||||||
// JMP handlers
|
// JMP handlers
|
||||||
_handlers.Add(new JmpRel32Handler(_codeBuffer, _decoder, _length));
|
_handlers.Add(new JmpRel32Handler(_codeBuffer, _decoder, _length));
|
||||||
_handlers.Add(new JmpRel8Handler(_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 ConditionalJumpHandler(_codeBuffer, _decoder, _length));
|
||||||
_handlers.Add(new TwoByteConditionalJumpHandler(_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>
|
/// <returns>The handler that can decode the opcode, or null if no handler can decode it</returns>
|
||||||
public IInstructionHandler? GetHandler(byte opcode)
|
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
|
// For all other opcodes, use the normal handler selection logic
|
||||||
return _handlers.FirstOrDefault(h => h.CanHandle(opcode));
|
return _handlers.FirstOrDefault(h => h.CanHandle(opcode));
|
||||||
}
|
}
|
||||||
|
67
X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs
Normal file
67
X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user