namespace X86Disassembler.X86.Handlers.Jump;
using X86Disassembler.X86.Operands;
///
/// Handler for two-byte conditional jump instructions (0x0F 0x80-0x8F)
///
public class TwoByteConditionalJumpHandler : InstructionHandler
{
// Mnemonics for conditional jumps
private static readonly string[] ConditionalJumpMnemonics =
[
"jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "jnbe",
"js", "jns", "jp", "jnp", "jl", "jnl", "jle", "jnle"
];
// 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,
InstructionType.Js, InstructionType.Jns, InstructionType.Unknown, InstructionType.Unknown,
InstructionType.Jl, InstructionType.Jge, InstructionType.Jle, InstructionType.Jg
];
///
/// Initializes a new instance of the TwoByteConditionalJumpHandler class
///
/// The instruction decoder that owns this handler
public TwoByteConditionalJumpHandler(InstructionDecoder decoder)
: base(decoder)
{
}
///
/// 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)
{
// Two-byte conditional jumps start with 0x0F
if (opcode != 0x0F)
{
return false;
}
int position = Decoder.GetPosition();
if (!Decoder.CanReadByte())
{
return false;
}
byte secondByte = Decoder.PeakByte();
// Second byte must be in the range 0x80-0x8F
return secondByte >= 0x80 && secondByte <= 0x8F;
}
///
/// Decodes a two-byte conditional jump 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)
{
// Check if we have enough bytes for the second byte
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Get the instruction type from the table
int index = secondByte - 0x80;
instruction.Type = InstructionTypes[index];
// Check if we have enough bytes for the offset
if (!Decoder.CanReadUInt())
{
return false;
}
// Read the relative offset (32-bit)
uint offset = Decoder.ReadUInt32();
// Calculate the target address
// 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);
// Create the relative offset operand
var targetOperand = OperandFactory.CreateRelativeOffsetOperand(targetAddress);
// Set the structured operands
instruction.StructuredOperands =
[
targetOperand
];
return true;
}
}