diff --git a/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm8Handler.cs b/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm8Handler.cs
index fe44e21..1dec7dd 100644
--- a/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm8Handler.cs
+++ b/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm8Handler.cs
@@ -45,15 +45,21 @@ public class CmpImmWithRm8Handler : InstructionHandler
/// True if the instruction was successfully decoded
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;
}
diff --git a/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs b/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs
index 0e88950..72fa622 100644
--- a/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs
+++ b/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs
@@ -104,9 +104,6 @@ public class InstructionHandlerFactory
///
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
/// The handler that can decode the opcode, or null if no handler can decode it
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));
}
diff --git a/X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs b/X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs
new file mode 100644
index 0000000..912976b
--- /dev/null
+++ b/X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs
@@ -0,0 +1,67 @@
+namespace X86Disassembler.X86.Handlers.Jump;
+
+///
+/// Handler for JGE rel8 instruction (0x7D)
+///
+public class JgeRel8Handler : InstructionHandler
+{
+ ///
+ /// Initializes a new instance of the JgeRel8Handler class
+ ///
+ /// The buffer containing the code to decode
+ /// The instruction decoder that owns this handler
+ /// The length of the buffer
+ public JgeRel8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
+ : base(codeBuffer, decoder, length)
+ {
+ }
+
+ ///
+ /// Checks if this handler can decode the given opcode
+ ///
+ /// The opcode to check
+ /// True if this handler can decode the opcode
+ public override bool CanHandle(byte opcode)
+ {
+ return opcode == 0x7D;
+ }
+
+ ///
+ /// Decodes a JGE rel8 instruction
+ ///
+ /// The opcode of the instruction
+ /// The instruction object to populate
+ /// True if the instruction was successfully decoded
+ 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;
+ }
+}