2025-04-14 22:08:50 +03:00
|
|
|
using X86Disassembler.X86.Operands;
|
|
|
|
|
2025-04-13 19:26:08 +03:00
|
|
|
namespace X86Disassembler.X86.Handlers.Xor;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handler for XOR r/m16, imm8 (sign-extended) instruction (0x83 /6 with 0x66 prefix)
|
|
|
|
/// </summary>
|
|
|
|
public class XorImmWithRm16SignExtendedHandler : InstructionHandler
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the XorImmWithRm16SignExtendedHandler class
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
public XorImmWithRm16SignExtendedHandler(InstructionDecoder decoder)
|
|
|
|
: base(decoder)
|
2025-04-13 19:26:08 +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)
|
|
|
|
{
|
|
|
|
if (opcode != 0x83 || !Decoder.HasOperandSizePrefix())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if the reg field of the ModR/M byte is 6 (XOR)
|
2025-04-14 01:08:14 +03:00
|
|
|
if (!Decoder.CanReadByte())
|
2025-04-13 19:26:08 +03:00
|
|
|
return false;
|
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
byte modRM = Decoder.PeakByte();
|
2025-04-13 19:26:08 +03:00
|
|
|
byte reg = (byte)((modRM & 0x38) >> 3);
|
|
|
|
|
|
|
|
return reg == 6; // 6 = XOR
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Decodes a XOR r/m16, imm8 (sign-extended) 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 22:08:50 +03:00
|
|
|
// Set the instruction type
|
|
|
|
instruction.Type = InstructionType.Xor;
|
2025-04-13 19:26:08 +03:00
|
|
|
|
2025-04-14 01:08:14 +03:00
|
|
|
if (!Decoder.CanReadByte())
|
2025-04-13 19:26:08 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the ModR/M byte
|
2025-04-14 22:08:50 +03:00
|
|
|
// For XOR r/m16, imm8 (sign-extended) (0x83 /6 with 0x66 prefix):
|
|
|
|
// - The r/m field with mod specifies the destination operand (register or memory)
|
|
|
|
// - The immediate value is the source operand (sign-extended from 8 to 16 bits)
|
|
|
|
var (mod, reg, rm, destinationOperand) = ModRMDecoder.ReadModRM();
|
2025-04-13 23:06:52 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Adjust the operand size to 16-bit
|
|
|
|
destinationOperand.Size = 16;
|
2025-04-13 19:26:08 +03:00
|
|
|
|
|
|
|
// Read the immediate value (sign-extended from 8 to 16 bits)
|
2025-04-14 01:08:14 +03:00
|
|
|
if (!Decoder.CanReadByte())
|
2025-04-13 19:26:08 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2025-04-14 01:08:14 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Read the immediate value as a signed byte and automatically sign-extend it to short
|
2025-04-13 19:35:28 +03:00
|
|
|
short imm16 = (sbyte)Decoder.ReadByte();
|
2025-04-13 19:26:08 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Create the source immediate operand with the sign-extended value
|
|
|
|
var sourceOperand = OperandFactory.CreateImmediateOperand(imm16, 16);
|
2025-04-13 19:26:08 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Set the structured operands
|
|
|
|
instruction.StructuredOperands =
|
|
|
|
[
|
|
|
|
destinationOperand,
|
|
|
|
sourceOperand
|
|
|
|
];
|
2025-04-13 19:26:08 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|