namespace X86Disassembler.X86.Handlers.Mov;
///
/// Handler for MOV r/m8, imm8 instruction (0xC6)
///
public class MovRm8Imm8Handler : InstructionHandler
{
///
/// Initializes a new instance of the MovRm8Imm8Handler class
///
/// The buffer containing the code to decode
/// The instruction decoder that owns this handler
/// The length of the buffer
public MovRm8Imm8Handler(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)
{
return opcode == 0xC6;
}
///
/// Decodes a MOV r/m8, imm8 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)
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// MOV r/m8, imm8 only uses reg=0
if (reg != 0)
{
return false;
}
// For direct register addressing (mod == 3), use 8-bit register names
if (mod == 3)
{
// Use 8-bit register names for direct register addressing
destOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
else
{
// Replace the size prefix with "byte ptr" for memory operands
destOperand = destOperand.Replace("dword ptr", "byte ptr");
}
// Read the immediate value
if (!Decoder.CanReadByte())
{
return false;
}
byte imm8 = Decoder.ReadByte();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
return true;
}
}