0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-08-04 02:16:33 +03:00

add misc handlers, cleanup and fixes

This commit is contained in:
bird_egop
2025-04-17 20:47:51 +03:00
parent 124493cd94
commit a9d4c39717
22 changed files with 1086 additions and 63 deletions

View File

@@ -0,0 +1,71 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
/// Handler for AND AX, imm16 instruction (0x25 with 0x66 prefix)
/// </summary>
public class AndAxImmHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the AndAxImmHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public AndAxImmHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// AND AX, imm16 is encoded as 0x25 with 0x66 prefix
if (opcode != 0x25)
{
return false;
}
// Only handle when the operand size prefix is present
return Decoder.HasOperandSizePrefix();
}
/// <summary>
/// Decodes an AND AX, imm16 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)
{
// Set the instruction type
instruction.Type = InstructionType.And;
// Check if we have enough bytes for the immediate value
if (!Decoder.CanReadUShort())
{
return false;
}
// Read the immediate value
ushort imm16 = Decoder.ReadUInt16();
// Create the AX register operand
var axOperand = OperandFactory.CreateRegisterOperand(RegisterIndex.A, 16);
// Create the immediate operand
var immOperand = OperandFactory.CreateImmediateOperand(imm16);
// Set the structured operands
instruction.StructuredOperands =
[
axOperand,
immOperand
];
return true;
}
}

View File

@@ -0,0 +1,84 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
/// Handler for AND r/m16, imm16 instruction (0x81 /4 with 0x66 prefix)
/// </summary>
public class AndImmToRm16Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the AndImmToRm16Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public AndImmToRm16Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// AND r/m16, imm16 is encoded as 0x81 with 0x66 prefix
if (opcode != 0x81)
{
return false;
}
// Only handle when the operand size prefix is present
if (!Decoder.HasOperandSizePrefix())
{
return false;
}
// Check if we can read the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the reg field of the ModR/M byte is 4 (AND)
var reg = ModRMDecoder.PeakModRMReg();
return reg == 4; // 4 = AND
}
/// <summary>
/// Decodes an AND r/m16, imm16 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)
{
// Set the instruction type
instruction.Type = InstructionType.And;
// Read the ModR/M byte to get the destination operand
var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM16();
// Check if we have enough bytes for the immediate value
if (!Decoder.CanReadUShort())
{
return false;
}
// Read the immediate value
ushort imm16 = Decoder.ReadUInt16();
// Create the immediate operand
var sourceOperand = OperandFactory.CreateImmediateOperand(imm16);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}
}

View File

@@ -0,0 +1,86 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
/// Handler for AND r/m16, imm8 instruction (0x83 /4 with 0x66 prefix)
/// </summary>
public class AndImmToRm16SignExtendedHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the AndImmToRm16SignExtendedHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public AndImmToRm16SignExtendedHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// AND r/m16, imm8 is encoded as 0x83 with 0x66 prefix
if (opcode != 0x83)
{
return false;
}
// Only handle when the operand size prefix is present
if (!Decoder.HasOperandSizePrefix())
{
return false;
}
// Check if we can read the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the reg field of the ModR/M byte is 4 (AND)
var reg = ModRMDecoder.PeakModRMReg();
return reg == 4; // 4 = AND
}
/// <summary>
/// Decodes an AND r/m16, imm8 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)
{
// Set the instruction type
instruction.Type = InstructionType.And;
// Read the ModR/M byte to get the destination operand
var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM16();
// Check if we have enough bytes for the immediate value
if (!Decoder.CanReadByte())
{
return false;
}
// Read the immediate value and sign-extend it to 16 bits
sbyte imm8 = (sbyte)Decoder.ReadByte();
short signExtendedImm = imm8;
ushort imm16 = (ushort)signExtendedImm;
// Create the immediate operand
var sourceOperand = OperandFactory.CreateImmediateOperand(imm16);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}
}

View File

@@ -0,0 +1,70 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
/// Handler for AND r16, r/m16 instruction (0x23 with 0x66 prefix)
/// </summary>
public class AndR16Rm16Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the AndR16Rm16Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public AndR16Rm16Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// AND r16, r/m16 is encoded as 0x23 with 0x66 prefix
if (opcode != 0x23)
{
return false;
}
// Only handle when the operand size prefix is present
return Decoder.HasOperandSizePrefix();
}
/// <summary>
/// Decodes an AND r16, r/m16 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)
{
// Set the instruction type
instruction.Type = InstructionType.And;
// Check if we can read the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// For AND r16, r/m16 (0x23 with 0x66 prefix):
// - The reg field of the ModR/M byte specifies the destination register
// - The r/m field with mod specifies the source operand (register or memory)
var (_, reg, _, sourceOperand) = ModRMDecoder.ReadModRM16();
// Create the destination register operand with 16-bit size
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, 16);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}
}

View File

@@ -0,0 +1,70 @@
using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// <summary>
/// Handler for AND r/m16, r16 instruction (0x21 with 0x66 prefix)
/// </summary>
public class AndRm16R16Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the AndRm16R16Handler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public AndRm16R16Handler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <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)
{
// AND r/m16, r16 is encoded as 0x21 with 0x66 prefix
if (opcode != 0x21)
{
return false;
}
// Only handle when the operand size prefix is present
return Decoder.HasOperandSizePrefix();
}
/// <summary>
/// Decodes an AND r/m16, r16 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)
{
// Set the instruction type
instruction.Type = InstructionType.And;
// Check if we can read the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// For AND r/m16, r16 (0x21 with 0x66 prefix):
// - The reg field of the ModR/M byte specifies the source register
// - The r/m field with mod specifies the destination operand (register or memory)
var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM16();
// Create the source register operand with 16-bit size
var sourceOperand = OperandFactory.CreateRegisterOperand(reg, 16);
// Set the structured operands
instruction.StructuredOperands =
[
destinationOperand,
sourceOperand
];
return true;
}
}