diff --git a/X86Disassembler/X86/Handlers/FloatingPoint/LoadStoreInt16Handler.cs b/X86Disassembler/X86/Handlers/FloatingPoint/LoadStoreInt16Handler.cs index afa73ec..94d155c 100644 --- a/X86Disassembler/X86/Handlers/FloatingPoint/LoadStoreInt16Handler.cs +++ b/X86Disassembler/X86/Handlers/FloatingPoint/LoadStoreInt16Handler.cs @@ -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 diff --git a/X86Disassembler/X86/Handlers/Mov/MovMemRegHandler.cs b/X86Disassembler/X86/Handlers/Mov/MovMemRegHandler.cs index 9f01045..4a68a9a 100644 --- a/X86Disassembler/X86/Handlers/Mov/MovMemRegHandler.cs +++ b/X86Disassembler/X86/Handlers/Mov/MovMemRegHandler.cs @@ -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(); - - // Adjust the operand size based on the opcode - destinationOperand.Size = operandSize; - - // Create the source register operand - var sourceOperand = OperandFactory.CreateRegisterOperand(reg, operandSize); + Operand destinationOperand; + Operand sourceOperand; + + 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 = diff --git a/X86Disassembler/X86/Handlers/Mov/MovRegMemHandler.cs b/X86Disassembler/X86/Handlers/Mov/MovRegMemHandler.cs index 2521b2a..c6a65bc 100644 --- a/X86Disassembler/X86/Handlers/Mov/MovRegMemHandler.cs +++ b/X86Disassembler/X86/Handlers/Mov/MovRegMemHandler.cs @@ -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(); - - // Adjust the operand size based on the opcode - sourceOperand.Size = operandSize; - - // Create the destination register operand - var destinationOperand = OperandFactory.CreateRegisterOperand(reg, operandSize); + Operand sourceOperand; + Operand destinationOperand; + + 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 = diff --git a/X86Disassembler/X86/Handlers/Mov/MovRm8Imm8Handler.cs b/X86Disassembler/X86/Handlers/Mov/MovRm8Imm8Handler.cs index 17e6dc4..fd2c27e 100644 --- a/X86Disassembler/X86/Handlers/Mov/MovRm8Imm8Handler.cs +++ b/X86Disassembler/X86/Handlers/Mov/MovRm8Imm8Handler.cs @@ -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()) diff --git a/X86Disassembler/X86/Handlers/Or/OrR8Rm8Handler.cs b/X86Disassembler/X86/Handlers/Or/OrR8Rm8Handler.cs index 0067809..ac8d186 100644 --- a/X86Disassembler/X86/Handlers/Or/OrR8Rm8Handler.cs +++ b/X86Disassembler/X86/Handlers/Or/OrR8Rm8Handler.cs @@ -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 = diff --git a/X86Disassembler/X86/Handlers/Sub/SubImmFromRm16Handler.cs b/X86Disassembler/X86/Handlers/Sub/SubImmFromRm16Handler.cs index 3a2b551..0ebb666 100644 --- a/X86Disassembler/X86/Handlers/Sub/SubImmFromRm16Handler.cs +++ b/X86Disassembler/X86/Handlers/Sub/SubImmFromRm16Handler.cs @@ -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()) diff --git a/X86Disassembler/X86/Handlers/Sub/SubImmFromRm16SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Sub/SubImmFromRm16SignExtendedHandler.cs index 4e7bdbe..7318aee 100644 --- a/X86Disassembler/X86/Handlers/Sub/SubImmFromRm16SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Sub/SubImmFromRm16SignExtendedHandler.cs @@ -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()) diff --git a/X86Disassembler/X86/Handlers/Sub/SubImmFromRm8Handler.cs b/X86Disassembler/X86/Handlers/Sub/SubImmFromRm8Handler.cs index c81f2ea..45931f5 100644 --- a/X86Disassembler/X86/Handlers/Sub/SubImmFromRm8Handler.cs +++ b/X86Disassembler/X86/Handlers/Sub/SubImmFromRm8Handler.cs @@ -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()) diff --git a/X86Disassembler/X86/Handlers/Sub/SubR16Rm16Handler.cs b/X86Disassembler/X86/Handlers/Sub/SubR16Rm16Handler.cs index 29989af..2893d3f 100644 --- a/X86Disassembler/X86/Handlers/Sub/SubR16Rm16Handler.cs +++ b/X86Disassembler/X86/Handlers/Sub/SubR16Rm16Handler.cs @@ -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 = diff --git a/X86Disassembler/X86/Handlers/Sub/SubRm16R16Handler.cs b/X86Disassembler/X86/Handlers/Sub/SubRm16R16Handler.cs index 2714337..f6a5d68 100644 --- a/X86Disassembler/X86/Handlers/Sub/SubRm16R16Handler.cs +++ b/X86Disassembler/X86/Handlers/Sub/SubRm16R16Handler.cs @@ -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 =