2025-04-14 22:08:50 +03:00
|
|
|
using X86Disassembler.X86.Operands;
|
|
|
|
|
2025-04-13 16:00:46 +03:00
|
|
|
namespace X86Disassembler.X86.Handlers.Push;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handler for PUSH r/m32 instruction (0xFF /6)
|
|
|
|
/// </summary>
|
|
|
|
public class PushRm32Handler : InstructionHandler
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the PushRm32Handler class
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
2025-04-14 22:08:50 +03:00
|
|
|
public PushRm32Handler(InstructionDecoder decoder)
|
|
|
|
: base(decoder)
|
2025-04-13 16:00:46 +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)
|
|
|
|
{
|
2025-04-14 00:33:39 +03:00
|
|
|
// PUSH r/m32 is encoded as FF /6
|
|
|
|
if (opcode != 0xFF)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have enough bytes to read the ModR/M byte
|
|
|
|
if (!Decoder.CanReadByte())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2025-04-15 02:42:47 +03:00
|
|
|
|
|
|
|
var reg = ModRMDecoder.PeakModRMReg();
|
2025-04-14 00:33:39 +03:00
|
|
|
|
|
|
|
// PUSH r/m32 is encoded as FF /6 (reg field = 6)
|
|
|
|
return reg == 6;
|
2025-04-13 16:00:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Decodes a PUSH r/m32 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.Push;
|
2025-04-14 00:33:39 +03:00
|
|
|
|
|
|
|
// Check if we have enough bytes for the ModR/M byte
|
|
|
|
if (!Decoder.CanReadByte())
|
2025-04-13 16:00:46 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the ModR/M byte
|
2025-04-14 22:08:50 +03:00
|
|
|
// For PUSH r/m32 (FF /6):
|
|
|
|
// - The r/m field with mod specifies the operand (register or memory)
|
2025-04-15 02:42:47 +03:00
|
|
|
var (_, _, _, operand) = ModRMDecoder.ReadModRM();
|
2025-04-13 16:00:46 +03:00
|
|
|
|
2025-04-14 22:08:50 +03:00
|
|
|
// Set the structured operands
|
|
|
|
// PUSH has only one operand
|
|
|
|
instruction.StructuredOperands =
|
|
|
|
[
|
|
|
|
operand
|
|
|
|
];
|
2025-04-13 16:00:46 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|