diff --git a/X86Disassembler/X86/Handlers/Jump/ConditionalJumpHandler.cs b/X86Disassembler/X86/Handlers/Jump/ConditionalJumpHandler.cs index c1e645e..ecbd763 100644 --- a/X86Disassembler/X86/Handlers/Jump/ConditionalJumpHandler.cs +++ b/X86Disassembler/X86/Handlers/Jump/ConditionalJumpHandler.cs @@ -46,30 +46,18 @@ public class ConditionalJumpHandler : InstructionHandler int index = opcode - 0x70; instruction.Mnemonic = Mnemonics[index]; - // Get the current position in the code buffer - int position = Decoder.GetPosition(); - - if (position >= Length) + // Check if we can read the offset byte + if (!Decoder.CanReadByte()) { return false; } - // Read the relative offset - sbyte offset = (sbyte)CodeBuffer[position]; - - // According to x86 architecture, the jump offset is relative to the instruction following the jump - // For a conditional jump, the instruction is 2 bytes: opcode (1 byte) + offset (1 byte) - - // Calculate the target address: - // 1. Start with the current position (where the offset byte is) - // 2. Add 1 to account for the size of the offset byte itself - // 3. Add the offset value + // Read the offset and calculate target address + int position = Decoder.GetPosition(); + sbyte offset = (sbyte)Decoder.ReadByte(); int targetAddress = position + 1 + offset; - // Move the decoder position past the offset byte - Decoder.SetPosition(position + 1); - - // Set the operands to the calculated target address + // Format the target address as a hexadecimal value instruction.Operands = $"0x{targetAddress:X8}"; return true; diff --git a/X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs b/X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs index 912976b..550156c 100644 --- a/X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs +++ b/X86Disassembler/X86/Handlers/Jump/JgeRel8Handler.cs @@ -34,34 +34,26 @@ public class JgeRel8Handler : InstructionHandler /// 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) + // Check if we can read the offset byte + if (!Decoder.CanReadByte()) { instruction.Operands = "??"; - instruction.RawBytes = new byte[] { opcode }; return true; } - // Read the relative offset - sbyte offset = (sbyte)CodeBuffer[startPosition]; - Decoder.SetPosition(startPosition + 1); + // Read the offset and calculate target address + int position = Decoder.GetPosition(); + sbyte offset = (sbyte)Decoder.ReadByte(); - // 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); + // Calculate target address (instruction address + instruction length + offset) + uint targetAddress = (uint)(instruction.Address + 2 + offset); - // Set the operands + // Format the target address instruction.Operands = $"0x{targetAddress:X8}"; - // Set the raw bytes - instruction.RawBytes = new byte[] { opcode, (byte)offset }; - return true; } } diff --git a/X86Disassembler/X86/Handlers/Jump/JmpRel8Handler.cs b/X86Disassembler/X86/Handlers/Jump/JmpRel8Handler.cs index 94184bd..bfeb0a5 100644 --- a/X86Disassembler/X86/Handlers/Jump/JmpRel8Handler.cs +++ b/X86Disassembler/X86/Handlers/Jump/JmpRel8Handler.cs @@ -11,7 +11,7 @@ public class JmpRel8Handler : InstructionHandler /// The buffer containing the code to decode /// The instruction decoder that owns this handler /// The length of the buffer - public JmpRel8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length) + public JmpRel8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length) : base(codeBuffer, decoder, length) { } @@ -34,37 +34,25 @@ public class JmpRel8Handler : InstructionHandler /// 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 = "jmp"; - // Check if we have enough bytes for the offset - if (startPosition >= Length) + // Check if we can read the offset byte + if (!Decoder.CanReadByte()) { - // Not enough bytes for the offset - instruction.Operands = "??"; - instruction.RawBytes = new byte[] { opcode }; return true; } - // Read the relative offset - sbyte offset = (sbyte)CodeBuffer[startPosition]; + // Read the offset and calculate target address + int position = Decoder.GetPosition(); + sbyte offset = (sbyte)Decoder.ReadByte(); - // Advance the decoder position past the offset byte - Decoder.SetPosition(startPosition + 1); + // Calculate target address (instruction address + instruction length + offset) + uint targetAddress = (uint)(instruction.Address + 2 + offset); - // Calculate the target address - // The target is relative to the next instruction (after the JMP instruction) - uint targetAddress = (uint)(instruction.Address + offset + 2); - - // Set the operands + // Format the target address instruction.Operands = $"0x{targetAddress:X8}"; - // Set the raw bytes - instruction.RawBytes = new byte[] { opcode, (byte)offset }; - return true; } }