namespace X86Disassembler.X86.Handlers.Jump;
///
/// 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"
];
///
/// Initializes a new instance of the TwoByteConditionalJumpHandler class
///
/// The buffer containing the code to decode
/// The instruction decoder that owns this handler
/// The length of the buffer
public TwoByteConditionalJumpHandler(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)
{
// Two-byte conditional jumps start with 0x0F
if (opcode == 0x0F)
{
int position = Decoder.GetPosition();
if (position < Length)
{
byte secondByte = CodeBuffer[position];
// Second byte must be in the range 0x80-0x8F
return secondByte >= 0x80 && secondByte <= 0x8F;
}
}
return false;
}
///
/// 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)
{
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the second byte of the opcode
byte secondByte = CodeBuffer[position++];
Decoder.SetPosition(position);
// Get the mnemonic from the table
int index = secondByte - 0x80;
instruction.Mnemonic = ConditionalJumpMnemonics[index];
if (position + 4 > Length)
{
return false;
}
// Read the relative offset (32-bit)
uint offset = Decoder.ReadUInt32();
// Calculate the target address
uint targetAddress = (uint)(position + offset + 4);
// Set the operands
instruction.Operands = $"0x{targetAddress:X8}";
return true;
}
}