0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-21 12:51:18 +03:00

Updated instruction handlers to use factory methods instead of directly setting Size property

This commit is contained in:
bird_egop 2025-04-16 01:39:23 +03:00
parent e06ea2beb3
commit d4eb920e2f
10 changed files with 89 additions and 48 deletions

View File

@ -92,7 +92,7 @@ public class LoadStoreInt16Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memoryOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, rawMemoryOperand) = ModRMDecoder.ReadModRM();
// Handle based on addressing mode
if (mod != 3) // Memory operand
@ -100,21 +100,49 @@ public class LoadStoreInt16Handler : InstructionHandler
// Set the instruction type based on the reg field
instruction.Type = MemoryInstructionTypes[(int)reg];
// Set the size based on the operation
// Create the appropriate memory operand based on the operation type and original operand type
Operand memoryOperand;
int operandSize;
// Determine the operand size based on the operation
if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D) // 16-bit integer operations
{
// Set to 16-bit for integer operations
memoryOperand.Size = 16;
operandSize = 16;
}
else if (reg == RegisterIndex.Di || reg == RegisterIndex.Bp) // 64-bit integer operations
{
// Set to 64-bit for integer operations
memoryOperand.Size = 64;
operandSize = 64;
}
else if (reg == RegisterIndex.Si || reg == RegisterIndex.Sp) // 80-bit packed BCD operations
{
// Set to 80-bit for BCD operations
memoryOperand.Size = 80;
operandSize = 80;
}
else
{
// Default to 32-bit for other operations
operandSize = 32;
}
// Create the appropriate memory operand based on the type of the raw operand
if (rawMemoryOperand is DirectMemoryOperand directMemory)
{
memoryOperand = OperandFactory.CreateDirectMemoryOperand(directMemory.Address, operandSize);
}
else if (rawMemoryOperand is BaseRegisterMemoryOperand baseMemory)
{
memoryOperand = OperandFactory.CreateBaseRegisterMemoryOperand(baseMemory.BaseRegister, operandSize);
}
else if (rawMemoryOperand is DisplacementMemoryOperand dispMemory)
{
memoryOperand = OperandFactory.CreateDisplacementMemoryOperand(dispMemory.BaseRegister, dispMemory.Displacement, operandSize);
}
else if (rawMemoryOperand is ScaledIndexMemoryOperand scaledMemory)
{
memoryOperand = OperandFactory.CreateScaledIndexMemoryOperand(scaledMemory.IndexRegister, scaledMemory.Scale, scaledMemory.BaseRegister, scaledMemory.Displacement, operandSize);
}
else
{
memoryOperand = rawMemoryOperand;
}
// Set the structured operands

View File

@ -51,13 +51,23 @@ public class MovMemRegHandler : InstructionHandler
// For MOV r/m32, r32 (0x89) or MOV r/m8, r8 (0x88):
// - The r/m field with mod specifies the destination operand (register or memory)
// - The reg field specifies the source register
var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM();
Operand destinationOperand;
Operand sourceOperand;
// Adjust the operand size based on the opcode
destinationOperand.Size = operandSize;
// Create the source register operand
var sourceOperand = OperandFactory.CreateRegisterOperand(reg, operandSize);
if (operandSize == 8)
{
// For 8-bit operands, use the 8-bit ModR/M decoder and factory methods
var (_, reg8, _, destOperand8) = ModRMDecoder.ReadModRM8();
destinationOperand = destOperand8;
sourceOperand = OperandFactory.CreateRegisterOperand8(reg8);
}
else
{
// For 32-bit operands, use the standard ModR/M decoder
var (_, regStd, _, destOperandStd) = ModRMDecoder.ReadModRM();
destinationOperand = destOperandStd;
sourceOperand = OperandFactory.CreateRegisterOperand(regStd, operandSize);
}
// Set the structured operands
instruction.StructuredOperands =

View File

@ -50,13 +50,23 @@ public class MovRegMemHandler : InstructionHandler
// For MOV r32, r/m32 (0x8B) or MOV r8, r/m8 (0x8A):
// - The reg field specifies the destination register
// - The r/m field with mod specifies the source operand (register or memory)
var (_, reg, _, sourceOperand) = ModRMDecoder.ReadModRM();
Operand sourceOperand;
Operand destinationOperand;
// Adjust the operand size based on the opcode
sourceOperand.Size = operandSize;
// Create the destination register operand
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, operandSize);
if (operandSize == 8)
{
// For 8-bit operands, use the 8-bit ModR/M decoder and factory methods
var (_, reg8, _, srcOperand8) = ModRMDecoder.ReadModRM8();
sourceOperand = srcOperand8;
destinationOperand = OperandFactory.CreateRegisterOperand8(reg8);
}
else
{
// For 32-bit operands, use the standard ModR/M decoder
var (_, regStd, _, srcOperandStd) = ModRMDecoder.ReadModRM();
sourceOperand = srcOperandStd;
destinationOperand = OperandFactory.CreateRegisterOperand(regStd, operandSize);
}
// Set the structured operands
instruction.StructuredOperands =

View File

@ -47,7 +47,7 @@ public class MovRm8Imm8Handler : InstructionHandler
// For MOV r/m8, imm8 (0xC6):
// - The r/m field with mod specifies the destination operand (register or memory)
// - The immediate value is the source operand
var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM();
var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM8();
// MOV r/m8, imm8 only uses reg=0
if (reg != 0)
@ -55,8 +55,7 @@ public class MovRm8Imm8Handler : InstructionHandler
return false;
}
// Adjust the operand size to 8-bit
destinationOperand.Size = 8;
// Note: The operand size is already set to 8-bit by the ReadModRM8 method
// Read the immediate value
if (!Decoder.CanReadByte())

View File

@ -46,13 +46,12 @@ public class OrR8Rm8Handler : InstructionHandler
// For OR r8, r/m8 (0x0A):
// - The reg field specifies the destination register
// - The r/m field with mod specifies the source operand (register or memory)
var (_, reg, _, sourceOperand) = ModRMDecoder.ReadModRM();
var (_, reg, _, sourceOperand) = ModRMDecoder.ReadModRM8();
// Adjust the operand size to 8-bit
sourceOperand.Size = 8;
// Note: The operand size is already set to 8-bit by the ReadModRM8 method
// Create the destination register operand
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, 8);
// Create the destination register operand using the 8-bit register type
var destinationOperand = OperandFactory.CreateRegisterOperand8(reg);
// Set the structured operands
instruction.StructuredOperands =

View File

@ -62,10 +62,9 @@ public class SubImmFromRm16Handler : InstructionHandler
// For SUB r/m16, imm16 (0x81 /5 with 0x66 prefix):
// - The r/m field with mod specifies the destination operand (register or memory)
// - The immediate value is the source operand
var (_, _, _, destinationOperand) = ModRMDecoder.ReadModRM();
var (_, _, _, destinationOperand) = ModRMDecoder.ReadModRM16();
// Adjust the operand size to 16-bit
destinationOperand.Size = 16;
// Note: The operand size is already set to 16-bit by the ReadModRM16 method
// Check if we have enough bytes for the immediate value
if (!Decoder.CanReadUShort())

View File

@ -62,10 +62,9 @@ public class SubImmFromRm16SignExtendedHandler : InstructionHandler
// For SUB r/m16, imm8 (0x83 /5 with 0x66 prefix and sign extension):
// - 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 (_, _, _, destinationOperand) = ModRMDecoder.ReadModRM();
var (_, _, _, destinationOperand) = ModRMDecoder.ReadModRM16();
// Adjust the operand size to 16-bit
destinationOperand.Size = 16;
// Note: The operand size is already set to 16-bit by the ReadModRM16 method
// Check if we have enough bytes for the immediate value
if (!Decoder.CanReadByte())

View File

@ -49,8 +49,7 @@ public class SubImmFromRm8Handler : InstructionHandler
// Read the ModR/M byte, specifying that we're dealing with 8-bit operands
var (_, _, _, destinationOperand) = ModRMDecoder.ReadModRM8();
// Ensure the destination operand has the correct size (8-bit)
destinationOperand.Size = 8;
// Note: The operand size is already set to 8-bit by the ReadModRM8 method
// Read the immediate byte
if (!Decoder.CanReadByte())

View File

@ -43,14 +43,13 @@ public class SubR16Rm16Handler : InstructionHandler
return false;
}
// Read the ModR/M byte
var (_, reg, _, sourceOperand) = ModRMDecoder.ReadModRM();
// Read the ModR/M byte, specifying that we're dealing with 16-bit operands
var (_, reg, _, sourceOperand) = ModRMDecoder.ReadModRM16();
// Ensure the source operand has the correct size (16-bit)
sourceOperand.Size = 16;
// Note: The operand size is already set to 16-bit by the ReadModRM16 method
// Create the destination register operand (16-bit)
var destinationOperand = OperandFactory.CreateRegisterOperand((RegisterIndex)reg, 16);
var destinationOperand = OperandFactory.CreateRegisterOperand(reg, 16);
// Set the structured operands
instruction.StructuredOperands =

View File

@ -43,14 +43,13 @@ public class SubRm16R16Handler : InstructionHandler
return false;
}
// Read the ModR/M byte
var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM();
// Read the ModR/M byte, specifying that we're dealing with 16-bit operands
var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM16();
// Ensure the destination operand has the correct size (16-bit)
destinationOperand.Size = 16;
// Note: The operand size is already set to 16-bit by the ReadModRM16 method
// Create the source register operand (16-bit)
var sourceOperand = OperandFactory.CreateRegisterOperand((RegisterIndex)reg, 16);
var sourceOperand = OperandFactory.CreateRegisterOperand(reg, 16);
// Set the structured operands
instruction.StructuredOperands =