2025-04-12 21:34:16 +03:00
|
|
|
namespace X86Disassembler.X86.Handlers.Jump;
|
2025-04-12 19:57:42 +03:00
|
|
|
|
2025-04-15 02:42:47 +03:00
|
|
|
using Operands;
|
2025-04-14 22:08:50 +03:00
|
|
|
|
2025-04-12 19:57:42 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Handler for two-byte conditional jump instructions (0x0F 0x80-0x8F)
|
|
|
|
/// </summary>
|
|
|
|
public class TwoByteConditionalJumpHandler : InstructionHandler
|
|
|
|
{
|
|
|
|
// Mnemonics for conditional jumps
|
2025-04-12 23:40:48 +03:00
|
|
|
private static readonly string[] ConditionalJumpMnemonics =
|
|
|
|
[
|
2025-04-12 19:57:42 +03:00
|
|
|
"jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "jnbe",
|
|
|
|
"js", "jns", "jp", "jnp", "jl", "jnl", "jle", "jnle"
|
2025-04-12 23:40:48 +03:00
|
|
|
];
|
2025-04-12 19:57:42 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Instruction types for conditional jumps
|
|
|
|
private static readonly InstructionType[] InstructionTypes =
|
|
|
|
[
|
|
|
|
InstructionType.Jo, InstructionType.Jno, InstructionType.Jb, InstructionType.Jae,
|
|
|
|
InstructionType.Jz, InstructionType.Jnz, InstructionType.Jbe, InstructionType.Ja,
|
2025-04-16 19:44:37 +03:00
|
|
|
InstructionType.Js, InstructionType.Jns, InstructionType.Jp, InstructionType.Jnp,
|
2025-04-14 22:08:50 +03:00
|
|
|
InstructionType.Jl, InstructionType.Jge, InstructionType.Jle, InstructionType.Jg
|
|
|
|
];
|
|
|
|
|
2025-04-12 19:57:42 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the TwoByteConditionalJumpHandler class
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
public TwoByteConditionalJumpHandler(InstructionDecoder decoder)
|
|
|
|
: base(decoder)
|
2025-04-12 19:57:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
|
|
|
// Two-byte conditional jumps start with 0x0F
|
2025-04-14 00:17:31 +03:00
|
|
|
if (opcode != 0x0F)
|
2025-04-12 19:57:42 +03:00
|
|
|
{
|
2025-04-14 00:17:31 +03:00
|
|
|
return false;
|
2025-04-12 19:57:42 +03:00
|
|
|
}
|
2025-04-15 02:42:47 +03:00
|
|
|
|
2025-04-14 00:17:31 +03:00
|
|
|
if (!Decoder.CanReadByte())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
byte secondByte = Decoder.PeakByte();
|
2025-04-14 00:17:31 +03:00
|
|
|
// Second byte must be in the range 0x80-0x8F
|
|
|
|
return secondByte >= 0x80 && secondByte <= 0x8F;
|
2025-04-12 19:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Decodes a two-byte conditional jump 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)
|
|
|
|
{
|
2025-04-14 00:17:31 +03:00
|
|
|
// Check if we have enough bytes for the second byte
|
|
|
|
if (!Decoder.CanReadByte())
|
|
|
|
{
|
2025-04-12 19:57:42 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the second byte of the opcode
|
2025-04-14 00:17:31 +03:00
|
|
|
byte secondByte = Decoder.ReadByte();
|
2025-04-12 19:57:42 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Get the instruction type from the table
|
2025-04-12 19:57:42 +03:00
|
|
|
int index = secondByte - 0x80;
|
2025-04-14 22:08:50 +03:00
|
|
|
instruction.Type = InstructionTypes[index];
|
2025-04-12 19:57:42 +03:00
|
|
|
|
2025-04-14 00:17:31 +03:00
|
|
|
// Check if we have enough bytes for the offset
|
|
|
|
if (!Decoder.CanReadUInt())
|
2025-04-12 19:57:42 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the relative offset (32-bit)
|
2025-04-13 23:22:30 +03:00
|
|
|
uint offset = Decoder.ReadUInt32();
|
2025-04-12 19:57:42 +03:00
|
|
|
|
|
|
|
// Calculate the target address
|
2025-04-14 00:17:31 +03:00
|
|
|
// For two-byte conditional jumps, the instruction is 6 bytes: first opcode (1) + second opcode (1) + offset (4)
|
|
|
|
uint targetAddress = (uint)(instruction.Address + 6 + offset);
|
2025-04-12 19:57:42 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Create the relative offset operand
|
|
|
|
var targetOperand = OperandFactory.CreateRelativeOffsetOperand(targetAddress);
|
|
|
|
|
|
|
|
// Set the structured operands
|
|
|
|
instruction.StructuredOperands =
|
|
|
|
[
|
|
|
|
targetOperand
|
|
|
|
];
|
2025-04-12 19:57:42 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|