0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 11:51:17 +03:00

nice big refactor

This commit is contained in:
bird_egop 2025-04-13 23:06:52 +03:00
parent 59df064ca4
commit 11a2cfada4
92 changed files with 981 additions and 1509 deletions

View File

@ -1,6 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAssemblyCodeArray_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003Fa1_003Fbc9d4e81_003FAssemblyCodeArray_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAssemblyCodeArray_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003Fa1_003Fbc9d4e81_003FAssemblyCodeArray_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAssemblyCodeMemory_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003F6e_003F09b667c6_003FAssemblyCodeMemory_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAssemblyCodeMemory_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003F6e_003F09b667c6_003FAssemblyCodeMemory_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACollectionAsserts_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F435f965090c5b89f0c5efb49ac3c5a72367d90599314191af25a832d0942f_003FCollectionAsserts_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACsvReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Ff7b87edd534764eebf2388a77d49e5cd9c6d49eb6788dca9b1c07d4545412715_003FCsvReader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACsvReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Ff7b87edd534764eebf2388a77d49e5cd9c6d49eb6788dca9b1c07d4545412715_003FCsvReader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADefaultTypeConverter_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F64b864a5d465bc24fc4b55e1026aba213beb1733ef631abeca5a9f25357eda_003FDefaultTypeConverter_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADefaultTypeConverter_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F64b864a5d465bc24fc4b55e1026aba213beb1733ef631abeca5a9f25357eda_003FDefaultTypeConverter_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADisassembler_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003Fd4_003Fad0818f9_003FDisassembler_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADisassembler_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003Fd4_003Fad0818f9_003FDisassembler_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
@ -10,6 +11,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMatrix4x4_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fed6aa59cd75423c5b655901d6ec4fb4be48ab669fa6fb01b3a7a7f31be95_003FMatrix4x4_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMatrix4x4_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fed6aa59cd75423c5b655901d6ec4fb4be48ab669fa6fb01b3a7a7f31be95_003FMatrix4x4_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMemory_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FLocal_003FSymbols_003Fsrc_003Fdotnet_003Fruntime_003F5535e31a712343a63f5d7d796cd874e563e5ac14_003Fsrc_003Flibraries_003FSystem_002EPrivate_002ECoreLib_003Fsrc_003FSystem_003FMemory_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMemory_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FLocal_003FSymbols_003Fsrc_003Fdotnet_003Fruntime_003F5535e31a712343a63f5d7d796cd874e563e5ac14_003Fsrc_003Flibraries_003FSystem_002EPrivate_002ECoreLib_003Fsrc_003FSystem_003FMemory_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASingle_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc99a63bcf3d2a18c20ee19e58ac875ab1edf2a147c8b92ffeed185ab8a44b4_003FSingle_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASingle_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc99a63bcf3d2a18c20ee19e58ac875ab1edf2a147c8b92ffeed185ab8a44b4_003FSingle_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStringAsserts_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F999ae9cc4ab7b7cfbc5080803e994426e97fd9d87c5b1f44544a799bc114_003FStringAsserts_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003Aud_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003F15_003F87bd9007_003Fud_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003Aud_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003F15_003F87bd9007_003Fud_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003Audis86_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003F95_003F953bbb0f_003Fudis86_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003Audis86_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003F95_003F953bbb0f_003Fudis86_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">C:\Users\Admin\AppData\Local\JetBrains\Rider2024.3\resharper-host\temp\Rider\vAny\CoverageData\_ParkanPlayground.1073341822\Snapshot\snapshot.utdcvr</s:String> <s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">C:\Users\Admin\AppData\Local\JetBrains\Rider2024.3\resharper-host\temp\Rider\vAny\CoverageData\_ParkanPlayground.1073341822\Snapshot\snapshot.utdcvr</s:String>

View File

@ -32,7 +32,7 @@ public class AdcImmToRm32Handler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 2; // 2 = ADC return reg == 2; // 2 = ADC
} }
@ -56,16 +56,7 @@ public class AdcImmToRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 2 for ADC
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)
@ -74,14 +65,11 @@ public class AdcImmToRm32Handler : InstructionHandler
} }
// Read the immediate value in little-endian format // Read the immediate value in little-endian format
byte b0 = CodeBuffer[position]; var imm = Decoder.ReadUInt32();
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
// Format the immediate value as expected by the tests (0x12345678) // Format the immediate value as expected by the tests (0x12345678)
// Note: The bytes are reversed to match the expected format in the tests // Note: The bytes are reversed to match the expected format in the tests
string immStr = $"0x{b3:X2}{b2:X2}{b1:X2}{b0:X2}"; string immStr = $"0x{imm:X8}";
// Advance the position past the immediate value // Advance the position past the immediate value
position += 4; position += 4;

View File

@ -32,7 +32,7 @@ public class AdcImmToRm32SignExtendedHandler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 2; // 2 = ADC return reg == 2; // 2 = ADC
} }
@ -56,16 +56,7 @@ public class AdcImmToRm32SignExtendedHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 2 for ADC
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)
@ -73,9 +64,8 @@ public class AdcImmToRm32SignExtendedHandler : InstructionHandler
return false; return false;
} }
sbyte imm8 = (sbyte)CodeBuffer[position]; // Sign-extend to 32 bits
int imm32 = imm8; // Sign-extend to 32 bits int imm32 = (sbyte) Decoder.ReadByte();
Decoder.SetPosition(position + 1);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}"; instruction.Operands = $"{destOperand}, 0x{imm32:X8}";

View File

@ -68,17 +68,7 @@ public class AddEaxImmHandler : InstructionHandler
} }
// Read the 32-bit immediate value // Read the 32-bit immediate value
uint imm32 = 0; uint imm32 = Decoder.ReadUInt32();
for (int i = 0; i < 4; i++)
{
if (startPosition + i < Length)
{
imm32 |= (uint)(CodeBuffer[startPosition + i] << (i * 8));
}
}
// Advance the decoder position
Decoder.SetPosition(startPosition + 4);
// Set the operands // Set the operands
instruction.Operands = $"eax, 0x{imm32:X8}"; instruction.Operands = $"eax, 0x{imm32:X8}";

View File

@ -56,16 +56,7 @@ public class AddImmToRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for ADD
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)
@ -74,18 +65,11 @@ public class AddImmToRm32Handler : InstructionHandler
} }
// Read the immediate value in little-endian format // Read the immediate value in little-endian format
byte b0 = CodeBuffer[position]; var imm = Decoder.ReadUInt32();
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
// Format the immediate value as expected by the tests (0x12345678) // Format the immediate value as expected by the tests (0x12345678)
// Note: The bytes are reversed to match the expected format in the tests // Note: The bytes are reversed to match the expected format in the tests
string immStr = $"0x{b3:X2}{b2:X2}{b1:X2}{b0:X2}"; string immStr = $"0x{imm:X8}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -59,24 +59,19 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[startPosition]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for ADD
byte rm = (byte)(modRM & 0x07);
// Track the bytes needed for this instruction // Track the bytes needed for this instruction
int bytesNeeded = 1; // ModR/M byte int bytesNeeded = 1; // ModR/M byte
// Process SIB byte if needed // Process SIB byte if needed
byte sib = 0; byte sib = 0;
if (mod != 3 && rm == 4) // SIB byte present if (mod != 3 && rm == RegisterIndex.Si) // SIB byte present
{ {
if (startPosition + bytesNeeded >= Length) if (startPosition + bytesNeeded >= Length)
{ {
instruction.Operands = "??"; instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode, modRM }; instruction.RawBytes = new byte[] { opcode, CodeBuffer[startPosition] };
return true; return true;
} }
sib = CodeBuffer[startPosition + bytesNeeded]; sib = CodeBuffer[startPosition + bytesNeeded];
@ -85,7 +80,7 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
// Handle displacement // Handle displacement
int dispSize = 0; int dispSize = 0;
if (mod == 0 && rm == 5) // 32-bit displacement if (mod == 0 && rm == RegisterIndex.Di) // 32-bit displacement
{ {
dispSize = 4; dispSize = 4;
} }
@ -102,21 +97,15 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
if (startPosition + bytesNeeded + dispSize >= Length) if (startPosition + bytesNeeded + dispSize >= Length)
{ {
instruction.Operands = "??"; instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode, modRM }; instruction.RawBytes = new byte[] { opcode, CodeBuffer[startPosition] };
return true; return true;
} }
bytesNeeded += dispSize; // Add displacement bytes bytesNeeded += dispSize; // Add displacement bytes
// Use ModRMDecoder to decode the destination operand
var modRMDecoder = new ModRMDecoder(CodeBuffer, Decoder, Length);
// Set the decoder position to after the ModR/M byte // Set the decoder position to after the ModR/M byte
Decoder.SetPosition(startPosition + 1); Decoder.SetPosition(startPosition + 1);
// Decode the destination operand
string destOperand = modRMDecoder.DecodeModRM(mod, rm, false);
// Get the position after decoding the ModR/M byte // Get the position after decoding the ModR/M byte
int newPosition = Decoder.GetPosition(); int newPosition = Decoder.GetPosition();
@ -141,15 +130,21 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
return true; return true;
} }
// Read the immediate value as a signed byte and sign-extend it // Read the immediate value as a signed byte and automatically sign-extend it to int
sbyte imm8 = (sbyte)CodeBuffer[newPosition]; int signExtendedImm = (sbyte)Decoder.ReadByte();
newPosition++; // Advance past the immediate byte
// Set the decoder position // Format the immediate value as a 32-bit hex value
Decoder.SetPosition(newPosition); string immStr;
if (signExtendedImm < 0)
// Format the immediate value as a 32-bit hex value (sign-extended) {
string immStr = $"0x{(uint)imm8:X8}"; // For negative values, use the full 32-bit representation (0xFFFFFFxx)
immStr = $"0x{(uint)signExtendedImm:X8}";
}
else
{
// For positive values, use the regular format
immStr = $"0x{signExtendedImm:X8}";
}
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -32,7 +32,7 @@ public class AddImmToRm8Handler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 0; // 0 = ADD return reg == 0; // 0 = ADD
} }
@ -56,27 +56,14 @@ public class AddImmToRm8Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for ADD
byte rm = (byte)(modRM & 0x07);
// For direct register addressing (mod == 3), use 8-bit register names // For direct register addressing (mod == 3), use 8-bit register names
string destOperand;
if (mod == 3) if (mod == 3)
{ {
// Use 8-bit register names for direct register addressing // Use 8-bit register names for direct register addressing
destOperand = GetRegister8(rm); destOperand = ModRMDecoder.GetRegisterName(rm, 8);
} }
else
{
// Use ModR/M decoder for memory addressing
destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
}
Decoder.SetPosition(position);
// Read the immediate value // Read the immediate value
if (position >= Length) if (position >= Length)
@ -84,8 +71,7 @@ public class AddImmToRm8Handler : InstructionHandler
return false; return false;
} }
byte imm8 = CodeBuffer[position++]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}"; instruction.Operands = $"{destOperand}, 0x{imm8:X2}";

View File

@ -42,19 +42,13 @@ public class AddR32Rm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "add"; instruction.Mnemonic = "add";
// Get the register name // Get the register name
string regName = GetRegister32(reg); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
@ -64,7 +58,7 @@ public class AddR32Rm32Handler : InstructionHandler
} }
else // Register operand else // Register operand
{ {
string rmName = GetRegister32(rm); string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{regName}, {rmName}"; instruction.Operands = $"{regName}, {rmName}";
} }

View File

@ -42,19 +42,13 @@ public class AddRm32R32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "add"; instruction.Mnemonic = "add";
// Get the register name // Get the register name
string regName = GetRegister32(reg); string regName = ModRMDecoder.GetRegisterName(reg, 32);;
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
@ -64,7 +58,7 @@ public class AddRm32R32Handler : InstructionHandler
} }
else // Register operand else // Register operand
{ {
string rmName = GetRegister32(rm); string rmName = ModRMDecoder.GetRegisterName(rm, 32);;
instruction.Operands = $"{rmName}, {regName}"; instruction.Operands = $"{rmName}, {regName}";
} }

View File

@ -56,16 +56,7 @@ public class AndImmToRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 4 for AND
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)
@ -74,18 +65,11 @@ public class AndImmToRm32Handler : InstructionHandler
} }
// Read the immediate value in little-endian format // Read the immediate value in little-endian format
byte b0 = CodeBuffer[position]; var imm = Decoder.ReadUInt32();
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
// Format the immediate value as expected by the tests (0x12345678) // Format the immediate value as expected by the tests (0x12345678)
// Note: The bytes are reversed to match the expected format in the tests // Note: The bytes are reversed to match the expected format in the tests
string immStr = $"0x{b3:X2}{b2:X2}{b1:X2}{b0:X2}"; string immStr = $"0x{imm:X8}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -76,9 +76,7 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
} }
// Read and sign-extend the immediate value // Read and sign-extend the immediate value
byte imm8 = Decoder.ReadByte(); uint imm32 = (uint)(sbyte)Decoder.ReadByte();
int signExtended = (sbyte)imm8;
uint imm32 = (uint)signExtended;
// Set operands // Set operands
if (mod == 3) if (mod == 3)

View File

@ -56,19 +56,10 @@ public class DivRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 6 for DIV
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = destOperand;
return true; return true;
} }

View File

@ -56,19 +56,10 @@ public class IdivRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 7 for IDIV
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = destOperand;
return true; return true;
} }

View File

@ -56,19 +56,10 @@ public class ImulRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 5 for IMUL
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = destOperand;
return true; return true;
} }

View File

@ -56,19 +56,10 @@ public class MulRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 4 for MUL
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = destOperand;
return true; return true;
} }

View File

@ -56,19 +56,10 @@ public class NegRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 3 for NEG
byte rm = (byte)(modRM & 0x07);
// Decode the operand
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = destOperand;
return true; return true;
} }

View File

@ -54,15 +54,10 @@ public class NotRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 2 for NOT
byte rm = (byte)(modRM & 0x07);
// Verify this is a NOT instruction // Verify this is a NOT instruction
if (reg != 2) if (reg != RegisterIndex.C)
{ {
return false; return false;
} }
@ -73,19 +68,13 @@ public class NotRm32Handler : InstructionHandler
Decoder.SetPosition(position); Decoder.SetPosition(position);
// For direct register addressing (mod == 3), the r/m field specifies a register // For direct register addressing (mod == 3), the r/m field specifies a register
string operand;
if (mod == 3) if (mod == 3)
{ {
operand = GetRegister32(rm); destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else
{
// Use the ModR/M decoder for memory addressing
operand = ModRMDecoder.DecodeModRM(mod, rm, false);
} }
// Set the operands // Set the operands
instruction.Operands = operand; instruction.Operands = destOperand;
return true; return true;
} }

View File

@ -42,16 +42,10 @@ public class CallRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// CALL r/m32 is encoded as FF /2 // CALL r/m32 is encoded as FF /2
if (reg != 2) if (reg != RegisterIndex.C)
{ {
return false; return false;
} }
@ -59,18 +53,15 @@ public class CallRm32Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "call"; instruction.Mnemonic = "call";
// For memory operands, set the operand // For register operands, set the operand
if (mod != 3) // Memory operand if (mod == 3)
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); // Register operand
instruction.Operands = operand; destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Register operand
{
string rmName = GetRegister32(rm);
instruction.Operands = rmName;
} }
instruction.Operands = destOperand;
return true; return true;
} }
} }

View File

@ -45,7 +45,7 @@ public class CmpAlImmHandler : InstructionHandler
} }
// Read the immediate value // Read the immediate value
byte imm8 = CodeBuffer[position++]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position); Decoder.SetPosition(position);
// Set the operands // Set the operands

View File

@ -32,7 +32,7 @@ public class CmpImmWithRm32Handler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP return reg == 7; // 7 = CMP
} }
@ -56,16 +56,7 @@ public class CmpImmWithRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 7 for CMP
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)
@ -74,18 +65,11 @@ public class CmpImmWithRm32Handler : InstructionHandler
} }
// Read the immediate value in little-endian format // Read the immediate value in little-endian format
byte b0 = CodeBuffer[position]; var imm = Decoder.ReadUInt32();
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
// Format the immediate value as expected by the tests (0x12345678) // Format the immediate value as expected by the tests (0x12345678)
// Note: The bytes are reversed to match the expected format in the tests // Note: The bytes are reversed to match the expected format in the tests
string immStr = $"0x{b3:X2}{b2:X2}{b1:X2}{b0:X2}"; string immStr = $"0x{imm:X8}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -32,7 +32,7 @@ public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP return reg == 7; // 7 = CMP
} }
@ -56,16 +56,7 @@ public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 7 for CMP
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position >= Length) if (position >= Length)
@ -74,11 +65,11 @@ public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
} }
// Read the immediate value as a signed byte and sign-extend it // Read the immediate value as a signed byte and sign-extend it
sbyte imm8 = (sbyte)CodeBuffer[position++]; sbyte imm8 = (sbyte) Decoder.ReadByte();
Decoder.SetPosition(position); Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, 0x{(uint)imm8:X2}"; instruction.Operands = $"{destOperand}, 0x{(uint) imm8:X2}";
return true; return true;
} }

View File

@ -32,7 +32,7 @@ public class CmpImmWithRm8Handler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP return reg == 7; // 7 = CMP
} }
@ -54,29 +54,20 @@ public class CmpImmWithRm8Handler : InstructionHandler
if (startPosition >= Length) if (startPosition >= Length)
{ {
instruction.Operands = "??"; instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode }; instruction.RawBytes = new byte[] {opcode};
return true; return true;
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[startPosition]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// CMP r/m8, imm8 is encoded as 80 /7 // CMP r/m8, imm8 is encoded as 80 /7
if (reg != 7) if (reg != RegisterIndex.Bp)
{ {
instruction.Operands = "??"; instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode, modRM };
return true; return true;
} }
// Use ModRMDecoder to decode the ModR/M byte
var (_, _, _, rmOperand) = ModRMDecoder.ReadModRM(false);
// Get the position after decoding the ModR/M byte // Get the position after decoding the ModR/M byte
int newPosition = Decoder.GetPosition(); int newPosition = Decoder.GetPosition();
@ -93,31 +84,31 @@ public class CmpImmWithRm8Handler : InstructionHandler
rawBytesImm[i + 1] = CodeBuffer[startPosition + i]; rawBytesImm[i + 1] = CodeBuffer[startPosition + i];
} }
} }
instruction.RawBytes = rawBytesImm; instruction.RawBytes = rawBytesImm;
return true; return true;
} }
// Read the immediate byte // Read the immediate byte
byte imm8 = CodeBuffer[newPosition]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(newPosition + 1);
// Replace the size prefix with "byte ptr" // Replace the size prefix with "byte ptr"
string operand; string operand;
if (rmOperand.StartsWith("qword ptr ")) if (destOperand.StartsWith("qword ptr "))
{ {
operand = rmOperand.Replace("qword ptr ", "byte ptr "); operand = destOperand.Replace("qword ptr ", "byte ptr ");
} }
else if (rmOperand.StartsWith("dword ptr ")) else if (destOperand.StartsWith("dword ptr "))
{ {
operand = rmOperand.Replace("dword ptr ", "byte ptr "); operand = destOperand.Replace("dword ptr ", "byte ptr ");
} }
else if (mod != 3) // Memory operand without prefix else if (mod != 3) // Memory operand without prefix
{ {
operand = $"byte ptr {rmOperand}"; operand = $"byte ptr {destOperand}";
} }
else // Register operand else // Register operand
{ {
operand = GetRegister8(rm); operand = ModRMDecoder.GetRegisterName(rm, 8);
} }
// Set the operands // Set the operands
@ -133,6 +124,7 @@ public class CmpImmWithRm8Handler : InstructionHandler
rawBytes[i + 1] = CodeBuffer[startPosition + i]; rawBytes[i + 1] = CodeBuffer[startPosition + i];
} }
} }
rawBytes[rawBytes.Length - 1] = imm8; rawBytes[rawBytes.Length - 1] = imm8;
instruction.RawBytes = rawBytes; instruction.RawBytes = rawBytes;

View File

@ -42,32 +42,23 @@ public class CmpR32Rm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "cmp"; instruction.Mnemonic = "cmp";
// Get the register name // Get the register name
string regName = GetRegister32(reg); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand // For register operands, set the operand
if (mod != 3) // Memory operand if (mod == 3)
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); // Register operand
instruction.Operands = $"{regName}, {operand}"; destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Register operand
{
string rmName = GetRegister32(rm);
instruction.Operands = $"{regName}, {rmName}";
} }
instruction.Operands = $"{regName}, {destOperand}";
return true; return true;
} }
} }

View File

@ -37,118 +37,34 @@ public class CmpRm32R32Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "cmp"; instruction.Mnemonic = "cmp";
int position = Decoder.GetPosition(); // Save the original position to properly handle the ModR/M byte
int originalPosition = Decoder.GetPosition();
if (position >= Length) if (originalPosition >= Length)
{ {
return false; return false;
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6); // Top 2 bits
byte reg = (byte)((modRM & 0x38) >> 3); // Middle 3 bits
byte rm = (byte)(modRM & 0x07); // Bottom 3 bits
// Get the register name for the reg field // Get the register name for the reg field
string regName = GetRegister32(reg); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Handle the different addressing modes // Use the destOperand directly from ModRMDecoder
string rmOperand; string rmOperand = destOperand;
if (mod == 3) // Direct register addressing // If it's a direct register operand, we need to remove the size prefix
if (mod == 3)
{ {
// Get the register name for the r/m field rmOperand = ModRMDecoder.GetRegisterName(rm, 32);
rmOperand = GetRegister32(rm);
} }
else // Memory addressing else if (rmOperand.StartsWith("dword ptr "))
{ {
// Handle SIB byte if needed // Remove the "dword ptr " prefix as we'll handle the operands differently
if (mod != 3 && rm == 4) // SIB byte present rmOperand = rmOperand.Substring(10);
{
if (position >= Length)
{
return false;
}
byte sib = CodeBuffer[position++];
// Extract the fields from the SIB byte
byte scale = (byte)((sib & 0xC0) >> 6);
byte index = (byte)((sib & 0x38) >> 3);
byte base_ = (byte)(sib & 0x07);
// TODO: Handle SIB byte properly
rmOperand = $"[complex addressing]";
}
else if (mod == 0 && rm == 5) // Displacement only addressing
{
if (position + 3 >= Length)
{
return false;
}
// Read the 32-bit displacement
uint disp = (uint)(CodeBuffer[position] |
(CodeBuffer[position + 1] << 8) |
(CodeBuffer[position + 2] << 16) |
(CodeBuffer[position + 3] << 24));
position += 4;
rmOperand = $"[0x{disp:X8}]";
}
else // Simple addressing modes
{
string baseReg = GetRegister32(rm);
if (mod == 0) // No displacement
{
rmOperand = $"[{baseReg}]";
}
else // Displacement
{
uint disp;
if (mod == 1) // 8-bit displacement
{
if (position >= Length)
{
return false;
}
// Sign-extend the 8-bit displacement
sbyte dispByte = (sbyte)CodeBuffer[position++];
disp = (uint)(int)dispByte;
// Format the displacement
string dispStr = dispByte < 0 ? $"-0x{-dispByte:X2}" : $"0x{dispByte:X2}";
rmOperand = $"[{baseReg}+{dispStr}]";
}
else // 32-bit displacement
{
if (position + 3 >= Length)
{
return false;
}
// Read the 32-bit displacement
disp = (uint)(CodeBuffer[position] |
(CodeBuffer[position + 1] << 8) |
(CodeBuffer[position + 2] << 16) |
(CodeBuffer[position + 3] << 24));
position += 4;
rmOperand = $"[{baseReg}+0x{disp:X8}]";
}
}
}
} }
// Update the decoder position
Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{rmOperand}, {regName}"; instruction.Operands = $"{rmOperand}, {regName}";

View File

@ -36,13 +36,13 @@ public class DecRegHandler : InstructionHandler
public override bool Decode(byte opcode, Instruction instruction) public override bool Decode(byte opcode, Instruction instruction)
{ {
// Calculate the register index (0 for EAX, 1 for ECX, etc.) // Calculate the register index (0 for EAX, 1 for ECX, etc.)
byte reg = (byte)(opcode - 0x48); RegisterIndex reg = (RegisterIndex)(opcode - 0x48);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "dec"; instruction.Mnemonic = "dec";
// Set the operand (register name) // Set the operand (register name)
instruction.Operands = GetRegister32(reg); instruction.Operands = ModRMDecoder.GetRegisterName(reg, 32);
return true; return true;
} }

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary> /// <summary>
/// Handler for floating-point operations on float32 (D8 opcode) /// Handler for floating-point operations on float32 (D8 opcode)
/// </summary> /// </summary>
public class Float32OperationHandler : FloatingPointBaseHandler public class Float32OperationHandler : InstructionHandler
{ {
// D8 opcode - operations on float32 // D8 opcode - operations on float32
private static readonly string[] Mnemonics = private static readonly string[] Mnemonics =
@ -55,27 +55,20 @@ public class Float32OperationHandler : FloatingPointBaseHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte) ((modRM & 0xC0) >> 6);
byte reg = (byte) ((modRM & 0x38) >> 3);
byte rm = (byte) (modRM & 0x07);
// Set the mnemonic based on the opcode and reg field // Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = Mnemonics[reg]; instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); instruction.Operands = destOperand;
instruction.Operands = operand;
} }
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// For register operands, we need to handle the stack registers // For register operands, we need to handle the stack registers
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
return true; return true;

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary> /// <summary>
/// Handler for floating-point operations on float64 (DC opcode) /// Handler for floating-point operations on float64 (DC opcode)
/// </summary> /// </summary>
public class Float64OperationHandler : FloatingPointBaseHandler public class Float64OperationHandler : InstructionHandler
{ {
// DC opcode - operations on float64 // DC opcode - operations on float64
private static readonly string[] Mnemonics = private static readonly string[] Mnemonics =
@ -55,27 +55,20 @@ public class Float64OperationHandler : FloatingPointBaseHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(true); // true for 64-bit operand
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Set the mnemonic based on the opcode and reg field // Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = Mnemonics[reg]; instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, true); // true for 64-bit operand instruction.Operands = destOperand;
instruction.Operands = operand;
} }
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// For DC C0-DC FF, the operands are reversed: ST(i), ST(0) // For DC C0-DC FF, the operands are reversed: ST(i), ST(0)
instruction.Operands = $"st({rm}), st(0)"; instruction.Operands = $"st({(int)rm}), st(0)";
} }
return true; return true;

View File

@ -1,18 +0,0 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary>
/// Base class for floating-point instruction handlers
/// </summary>
public abstract class FloatingPointBaseHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FloatingPointBaseHandler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
protected FloatingPointBaseHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
}

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary> /// <summary>
/// Handler for floating-point operations on int16 (DE opcode) /// Handler for floating-point operations on int16 (DE opcode)
/// </summary> /// </summary>
public class Int16OperationHandler : FloatingPointBaseHandler public class Int16OperationHandler : InstructionHandler
{ {
// DE opcode - operations on int16 // DE opcode - operations on int16
private static readonly string[] Mnemonics = private static readonly string[] Mnemonics =
@ -55,67 +55,59 @@ public class Int16OperationHandler : FloatingPointBaseHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte) ((modRM & 0xC0) >> 6);
byte reg = (byte) ((modRM & 0x38) >> 3);
byte rm = (byte) (modRM & 0x07);
// Set the mnemonic based on the opcode and reg field // Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = Mnemonics[reg]; instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
// Need to modify the default dword ptr to word ptr for 16-bit integers // Need to modify the default dword ptr to word ptr for 16-bit integers
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); instruction.Operands = destOperand.Replace("dword ptr", "word ptr");
operand = operand.Replace("dword ptr", "word ptr");
instruction.Operands = operand;
} }
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// Special handling for register-register operations // Special handling for register-register operations
if (reg == 0) // FADDP if (reg == RegisterIndex.A) // FADDP
{ {
instruction.Mnemonic = "faddp"; instruction.Mnemonic = "faddp";
instruction.Operands = $"st({rm}), st(0)"; instruction.Operands = $"st({(int)rm}), st(0)";
} }
else if (reg == 1) // FMULP else if (reg == RegisterIndex.B) // FMULP
{ {
instruction.Mnemonic = "fmulp"; instruction.Mnemonic = "fmulp";
instruction.Operands = $"st({rm}), st(0)"; instruction.Operands = $"st({(int)rm}), st(0)";
} }
else if (reg == 2 && rm == 1) // FCOMP else if (reg == RegisterIndex.C && rm == RegisterIndex.B) // FCOMP
{ {
instruction.Mnemonic = "fcomp"; instruction.Mnemonic = "fcomp";
instruction.Operands = ""; instruction.Operands = "";
} }
else if (reg == 3 && rm == 1) // FCOMPP else if (reg == RegisterIndex.D && rm == RegisterIndex.B) // FCOMPP
{ {
instruction.Mnemonic = "fcompp"; instruction.Mnemonic = "fcompp";
instruction.Operands = ""; instruction.Operands = "";
} }
else if (reg == 4) // FSUBP else if (reg == RegisterIndex.Si) // FSUBP
{ {
instruction.Mnemonic = "fsubp"; instruction.Mnemonic = "fsubp";
instruction.Operands = $"st({rm}), st(0)"; instruction.Operands = $"st({(int)rm}), st(0)";
} }
else if (reg == 5) // FSUBRP else if (reg == RegisterIndex.Di) // FSUBRP
{ {
instruction.Mnemonic = "fsubrp"; instruction.Mnemonic = "fsubrp";
instruction.Operands = $"st({rm}), st(0)"; instruction.Operands = $"st({(int)rm}), st(0)";
} }
else if (reg == 6) // FDIVP else if (reg == RegisterIndex.Sp) // FDIVP
{ {
instruction.Mnemonic = "fdivp"; instruction.Mnemonic = "fdivp";
instruction.Operands = $"st({rm}), st(0)"; instruction.Operands = $"st({(int)rm}), st(0)";
} }
else if (reg == 7) // FDIVRP else if (reg == RegisterIndex.Bp) // FDIVRP
{ {
instruction.Mnemonic = "fdivrp"; instruction.Mnemonic = "fdivrp";
instruction.Operands = $"st({rm}), st(0)"; instruction.Operands = $"st({(int)rm}), st(0)";
} }
else else
{ {

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary> /// <summary>
/// Handler for floating-point operations on int32 (DA opcode) /// Handler for floating-point operations on int32 (DA opcode)
/// </summary> /// </summary>
public class Int32OperationHandler : FloatingPointBaseHandler public class Int32OperationHandler : InstructionHandler
{ {
// DA opcode - operations on int32 // DA opcode - operations on int32
private static readonly string[] Mnemonics = private static readonly string[] Mnemonics =
@ -55,47 +55,40 @@ public class Int32OperationHandler : FloatingPointBaseHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte) ((modRM & 0xC0) >> 6);
byte reg = (byte) ((modRM & 0x38) >> 3);
byte rm = (byte) (modRM & 0x07);
// Set the mnemonic based on the opcode and reg field // Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = Mnemonics[reg]; instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); instruction.Operands = destOperand;
instruction.Operands = operand;
} }
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// Special handling for register-register operations // Special handling for register-register operations
if (reg == 0) // FCMOVB if (reg == RegisterIndex.A) // FCMOVB
{ {
instruction.Mnemonic = "fcmovb"; instruction.Mnemonic = "fcmovb";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 1) // FCMOVE else if (reg == RegisterIndex.B) // FCMOVE
{ {
instruction.Mnemonic = "fcmove"; instruction.Mnemonic = "fcmove";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 2) // FCMOVBE else if (reg == RegisterIndex.C) // FCMOVBE
{ {
instruction.Mnemonic = "fcmovbe"; instruction.Mnemonic = "fcmovbe";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 3) // FCMOVU else if (reg == RegisterIndex.D) // FCMOVU
{ {
instruction.Mnemonic = "fcmovu"; instruction.Mnemonic = "fcmovu";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 5 && rm == 1) // FUCOMPP else if (reg == RegisterIndex.Di && rm == RegisterIndex.B) // FUCOMPP
{ {
instruction.Mnemonic = "fucompp"; instruction.Mnemonic = "fucompp";
instruction.Operands = ""; instruction.Operands = "";

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary> /// <summary>
/// Handler for floating-point load, store, and control operations (D9 opcode) /// Handler for floating-point load, store, and control operations (D9 opcode)
/// </summary> /// </summary>
public class LoadStoreControlHandler : FloatingPointBaseHandler public class LoadStoreControlHandler : InstructionHandler
{ {
// D9 opcode - load, store, and control operations // D9 opcode - load, store, and control operations
private static readonly string[] Mnemonics = private static readonly string[] Mnemonics =
@ -55,71 +55,63 @@ public class LoadStoreControlHandler : FloatingPointBaseHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Set the mnemonic based on the opcode and reg field // Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = Mnemonics[reg]; instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Different operand types based on the instruction // Different operand types based on the instruction
if (reg == 0 || reg == 2 || reg == 3) // fld, fst, fstp if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D) // fld, fst, fstp
{ {
// Keep the dword ptr prefix from ModRMDecoder // Keep the dword ptr prefix from ModRMDecoder
instruction.Operands = operand; instruction.Operands = destOperand;
} }
else // fldenv, fldcw, fnstenv, fnstcw else // fldenv, fldcw, fnstenv, fnstcw
{ {
if (reg == 5) // fldcw - should use word ptr if (reg == RegisterIndex.Di) // fldcw - should use word ptr
{ {
instruction.Operands = operand.Replace("dword ptr", "word ptr"); instruction.Operands = destOperand.Replace("dword ptr", "word ptr");
} }
else // fldenv, fnstenv, fnstcw else // fldenv, fnstenv, fnstcw
{ {
// Remove the dword ptr prefix for other control operations // Remove the dword ptr prefix for other control operations
instruction.Operands = operand.Replace("dword ptr ", ""); instruction.Operands = destOperand.Replace("dword ptr ", "");
} }
} }
} }
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// Special handling for D9C0-D9FF (register-register operations) // Special handling for D9C0-D9FF (register-register operations)
if (reg == 0) // FLD ST(i) if (reg == RegisterIndex.A) // FLD ST(i)
{ {
instruction.Operands = $"st({rm})"; instruction.Operands = $"st({(int)rm})";
} }
else if (reg == 1) // FXCH ST(i) else if (reg == RegisterIndex.B) // FXCH ST(i)
{ {
instruction.Mnemonic = "fxch"; instruction.Mnemonic = "fxch";
instruction.Operands = $"st({rm})"; instruction.Operands = $"st({(int)rm})";
} }
else if (reg == 4) else if (reg == RegisterIndex.Si)
{ {
// D9E0-D9EF special instructions // D9E0-D9EF special instructions
switch (rm) switch (rm)
{ {
case 0: case RegisterIndex.A:
instruction.Mnemonic = "fchs"; instruction.Mnemonic = "fchs";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 1: case RegisterIndex.B:
instruction.Mnemonic = "fabs"; instruction.Mnemonic = "fabs";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 4: case RegisterIndex.Si:
instruction.Mnemonic = "ftst"; instruction.Mnemonic = "ftst";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 5: case RegisterIndex.Di:
instruction.Mnemonic = "fxam"; instruction.Mnemonic = "fxam";
instruction.Operands = ""; instruction.Operands = "";
break; break;
@ -129,40 +121,40 @@ public class LoadStoreControlHandler : FloatingPointBaseHandler
break; break;
} }
} }
else if (reg == 5) else if (reg == RegisterIndex.Di)
{ {
// D9F0-D9FF special instructions // D9F0-D9FF special instructions
switch (rm) switch (rm)
{ {
case 0: case RegisterIndex.A:
instruction.Mnemonic = "f2xm1"; instruction.Mnemonic = "f2xm1";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 1: case RegisterIndex.B:
instruction.Mnemonic = "fyl2x"; instruction.Mnemonic = "fyl2x";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 2: case RegisterIndex.C:
instruction.Mnemonic = "fptan"; instruction.Mnemonic = "fptan";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 3: case RegisterIndex.D:
instruction.Mnemonic = "fpatan"; instruction.Mnemonic = "fpatan";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 4: case RegisterIndex.Si:
instruction.Mnemonic = "fxtract"; instruction.Mnemonic = "fxtract";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 5: case RegisterIndex.Di:
instruction.Mnemonic = "fprem1"; instruction.Mnemonic = "fprem1";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 6: case RegisterIndex.Sp:
instruction.Mnemonic = "fdecstp"; instruction.Mnemonic = "fdecstp";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 7: case RegisterIndex.Bp:
instruction.Mnemonic = "fincstp"; instruction.Mnemonic = "fincstp";
instruction.Operands = ""; instruction.Operands = "";
break; break;
@ -172,40 +164,40 @@ public class LoadStoreControlHandler : FloatingPointBaseHandler
break; break;
} }
} }
else if (reg == 6) else if (reg == RegisterIndex.Sp)
{ {
// D9F0-D9FF more special instructions // D9F0-D9FF more special instructions
switch (rm) switch (rm)
{ {
case 0: case RegisterIndex.A:
instruction.Mnemonic = "fprem"; instruction.Mnemonic = "fprem";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 1: case RegisterIndex.B:
instruction.Mnemonic = "fyl2xp1"; instruction.Mnemonic = "fyl2xp1";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 2: case RegisterIndex.C:
instruction.Mnemonic = "fsqrt"; instruction.Mnemonic = "fsqrt";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 3: case RegisterIndex.D:
instruction.Mnemonic = "fsincos"; instruction.Mnemonic = "fsincos";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 4: case RegisterIndex.Si:
instruction.Mnemonic = "frndint"; instruction.Mnemonic = "frndint";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 5: case RegisterIndex.Di:
instruction.Mnemonic = "fscale"; instruction.Mnemonic = "fscale";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 6: case RegisterIndex.Sp:
instruction.Mnemonic = "fsin"; instruction.Mnemonic = "fsin";
instruction.Operands = ""; instruction.Operands = "";
break; break;
case 7: case RegisterIndex.Bp:
instruction.Mnemonic = "fcos"; instruction.Mnemonic = "fcos";
instruction.Operands = ""; instruction.Operands = "";
break; break;

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary> /// <summary>
/// Handler for floating-point load/store float64 operations (DD opcode) /// Handler for floating-point load/store float64 operations (DD opcode)
/// </summary> /// </summary>
public class LoadStoreFloat64Handler : FloatingPointBaseHandler public class LoadStoreFloat64Handler : InstructionHandler
{ {
// DD opcode - load/store float64 // DD opcode - load/store float64
private static readonly string[] Mnemonics = private static readonly string[] Mnemonics =
@ -55,59 +55,51 @@ public class LoadStoreFloat64Handler : FloatingPointBaseHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(true);// true for 64-bit operand
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte) ((modRM & 0xC0) >> 6);
byte reg = (byte) ((modRM & 0x38) >> 3);
byte rm = (byte) (modRM & 0x07);
// Set the mnemonic based on the opcode and reg field // Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = Mnemonics[reg]; instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, true); // true for 64-bit operand if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D) // fld, fst, fstp
if (reg == 0 || reg == 2 || reg == 3) // fld, fst, fstp
{ {
instruction.Operands = operand; instruction.Operands = destOperand;
} }
else // frstor, fnsave, fnstsw else // frstor, fnsave, fnstsw
{ {
// Remove the qword ptr prefix for these operations // Remove the qword ptr prefix for these operations
instruction.Operands = operand.Replace("qword ptr ", ""); instruction.Operands = destOperand.Replace("qword ptr ", "");
} }
} }
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// Special handling for register-register operations // Special handling for register-register operations
if (reg == 0) // FFREE if (reg == RegisterIndex.A) // FFREE
{ {
instruction.Mnemonic = "ffree"; instruction.Mnemonic = "ffree";
instruction.Operands = $"st({rm})"; instruction.Operands = $"st({(int)rm})";
} }
else if (reg == 2) // FST else if (reg == RegisterIndex.C) // FST
{ {
instruction.Mnemonic = "fst"; instruction.Mnemonic = "fst";
instruction.Operands = $"st({rm})"; instruction.Operands = $"st({(int)rm})";
} }
else if (reg == 3) // FSTP else if (reg == RegisterIndex.D) // FSTP
{ {
instruction.Mnemonic = "fstp"; instruction.Mnemonic = "fstp";
instruction.Operands = $"st({rm})"; instruction.Operands = $"st({(int)rm})";
} }
else if (reg == 4) // FUCOM else if (reg == RegisterIndex.Si) // FUCOM
{ {
instruction.Mnemonic = "fucom"; instruction.Mnemonic = "fucom";
instruction.Operands = $"st({rm})"; instruction.Operands = $"st({(int)rm})";
} }
else if (reg == 5) // FUCOMP else if (reg == RegisterIndex.Di) // FUCOMP
{ {
instruction.Mnemonic = "fucomp"; instruction.Mnemonic = "fucomp";
instruction.Operands = $"st({rm})"; instruction.Operands = $"st({(int)rm})";
} }
else else
{ {

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary> /// <summary>
/// Handler for floating-point load/store int16 and miscellaneous operations (DF opcode) /// Handler for floating-point load/store int16 and miscellaneous operations (DF opcode)
/// </summary> /// </summary>
public class LoadStoreInt16Handler : FloatingPointBaseHandler public class LoadStoreInt16Handler : InstructionHandler
{ {
// DF opcode - load/store int16, misc // DF opcode - load/store int16, misc
private static readonly string[] Mnemonics = private static readonly string[] Mnemonics =
@ -55,32 +55,26 @@ public class LoadStoreInt16Handler : FloatingPointBaseHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte) ((modRM & 0xC0) >> 6);
byte reg = (byte) ((modRM & 0x38) >> 3);
byte rm = (byte) (modRM & 0x07);
// Check for FNSTSW AX (DF E0) // Check for FNSTSW AX (DF E0)
if (mod == 3 && reg == 7 && rm == 0) if (mod == 3 && reg == RegisterIndex.Bp && rm == RegisterIndex.A)
{ {
// This is handled by the FnstswHandler, so we should not handle it here // This is handled by the FnstswHandler, so we should not handle it here
return false; return false;
} }
// Set the mnemonic based on the opcode and reg field // Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = Mnemonics[reg]; instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
if (reg == 0 || reg == 2 || reg == 3 || reg == 5 || reg == 7) // fild, fist, fistp, fild, fistp if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D || reg == RegisterIndex.Di || reg == RegisterIndex.Bp) // fild, fist, fistp, fild, fistp
{ {
if (reg == 5 || reg == 7) // 64-bit integer if (reg == RegisterIndex.Di || reg == RegisterIndex.Bp) // 64-bit integer
{ {
// Replace dword ptr with qword ptr for 64-bit integers // Replace dword ptr with qword ptr for 64-bit integers
operand = operand.Replace("dword ptr", "qword ptr"); operand = operand.Replace("dword ptr", "qword ptr");
@ -93,7 +87,7 @@ public class LoadStoreInt16Handler : FloatingPointBaseHandler
instruction.Operands = operand; instruction.Operands = operand;
} }
} }
else if (reg == 4 || reg == 6) // fbld, fbstp else if (reg == RegisterIndex.Si || reg == RegisterIndex.Sp) // fbld, fbstp
{ {
// Replace dword ptr with tbyte ptr for 80-bit packed BCD // Replace dword ptr with tbyte ptr for 80-bit packed BCD
operand = operand.Replace("dword ptr", "tbyte ptr"); operand = operand.Replace("dword ptr", "tbyte ptr");
@ -107,41 +101,41 @@ public class LoadStoreInt16Handler : FloatingPointBaseHandler
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// Special handling for register-register operations // Special handling for register-register operations
if (reg == 0) // FFREEP if (reg == RegisterIndex.A) // FFREEP
{ {
instruction.Mnemonic = "ffreep"; instruction.Mnemonic = "ffreep";
instruction.Operands = $"st({rm})"; instruction.Operands = $"st({(int)rm})";
} }
else if (reg == 1 && rm == 0) // FXCH else if (reg == RegisterIndex.B && rm == RegisterIndex.A) // FXCH
{ {
instruction.Mnemonic = "fxch"; instruction.Mnemonic = "fxch";
instruction.Operands = ""; instruction.Operands = "";
} }
else if (reg == 2 && rm == 0) // FSTP else if (reg == RegisterIndex.C && rm == RegisterIndex.A) // FSTP
{ {
instruction.Mnemonic = "fstp"; instruction.Mnemonic = "fstp";
instruction.Operands = "st(1)"; instruction.Operands = "st(1)";
} }
else if (reg == 3 && rm == 0) // FSTP else if (reg == RegisterIndex.D && rm == RegisterIndex.A) // FSTP
{ {
instruction.Mnemonic = "fstp"; instruction.Mnemonic = "fstp";
instruction.Operands = "st(1)"; instruction.Operands = "st(1)";
} }
else if (reg == 4) // FNSTSW else if (reg == RegisterIndex.Si) // FNSTSW
{ {
// This should not happen as FNSTSW AX is handled by FnstswHandler // This should not happen as FNSTSW AX is handled by FnstswHandler
instruction.Mnemonic = "??"; instruction.Mnemonic = "??";
instruction.Operands = ""; instruction.Operands = "";
} }
else if (reg == 5) // FUCOMIP else if (reg == RegisterIndex.Di) // FUCOMIP
{ {
instruction.Mnemonic = "fucomip"; instruction.Mnemonic = "fucomip";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 6) // FCOMIP else if (reg == RegisterIndex.Sp) // FCOMIP
{ {
instruction.Mnemonic = "fcomip"; instruction.Mnemonic = "fcomip";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else else
{ {

View File

@ -3,7 +3,7 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// <summary> /// <summary>
/// Handler for floating-point load/store int32 and miscellaneous operations (DB opcode) /// Handler for floating-point load/store int32 and miscellaneous operations (DB opcode)
/// </summary> /// </summary>
public class LoadStoreInt32Handler : FloatingPointBaseHandler public class LoadStoreInt32Handler : InstructionHandler
{ {
// DB opcode - load/store int32, misc // DB opcode - load/store int32, misc
private static readonly string[] Mnemonics = private static readonly string[] Mnemonics =
@ -55,69 +55,60 @@ public class LoadStoreInt32Handler : FloatingPointBaseHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte) ((modRM & 0xC0) >> 6);
byte reg = (byte) ((modRM & 0x38) >> 3);
byte rm = (byte) (modRM & 0x07);
// Set the mnemonic based on the opcode and reg field // Set the mnemonic based on the opcode and reg field
instruction.Mnemonic = Mnemonics[reg]; instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D) // fild, fist, fistp
if (reg == 0 || reg == 2 || reg == 3) // fild, fist, fistp
{ {
// Keep the dword ptr prefix for integer operations // Keep the dword ptr prefix for integer operations
instruction.Operands = operand; instruction.Operands = destOperand;
} }
else if (reg == 5 || reg == 7) // fld, fstp (extended precision) else if (reg == RegisterIndex.Di || reg == RegisterIndex.Bp) // fld, fstp (extended precision)
{ {
// Replace dword ptr with tword ptr for extended precision // Replace dword ptr with tword ptr for extended precision
operand = operand.Replace("dword ptr", "tword ptr"); instruction.Operands = destOperand.Replace("dword ptr", "tword ptr");
instruction.Operands = operand;
} }
else else
{ {
instruction.Operands = operand; instruction.Operands = destOperand;
} }
} }
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// Special handling for register-register operations // Special handling for register-register operations
if (reg == 0) // FCMOVNB if (reg == RegisterIndex.A) // FCMOVNB
{ {
instruction.Mnemonic = "fcmovnb"; instruction.Mnemonic = "fcmovnb";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 1) // FCMOVNE else if (reg == RegisterIndex.B) // FCMOVNE
{ {
instruction.Mnemonic = "fcmovne"; instruction.Mnemonic = "fcmovne";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 2) // FCMOVNBE else if (reg == RegisterIndex.C) // FCMOVNBE
{ {
instruction.Mnemonic = "fcmovnbe"; instruction.Mnemonic = "fcmovnbe";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 3) // FCMOVNU else if (reg == RegisterIndex.D) // FCMOVNU
{ {
instruction.Mnemonic = "fcmovnu"; instruction.Mnemonic = "fcmovnu";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 4) else if (reg == RegisterIndex.Si)
{ {
if (rm == 2) // FCLEX if (rm == RegisterIndex.C) // FCLEX
{ {
instruction.Mnemonic = "fclex"; instruction.Mnemonic = "fclex";
instruction.Operands = ""; instruction.Operands = "";
} }
else if (rm == 3) // FINIT else if (rm == RegisterIndex.D) // FINIT
{ {
instruction.Mnemonic = "finit"; instruction.Mnemonic = "finit";
instruction.Operands = ""; instruction.Operands = "";
@ -128,15 +119,15 @@ public class LoadStoreInt32Handler : FloatingPointBaseHandler
instruction.Operands = ""; instruction.Operands = "";
} }
} }
else if (reg == 5) // FUCOMI else if (reg == RegisterIndex.Di) // FUCOMI
{ {
instruction.Mnemonic = "fucomi"; instruction.Mnemonic = "fucomi";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else if (reg == 6) // FCOMI else if (reg == RegisterIndex.Sp) // FCOMI
{ {
instruction.Mnemonic = "fcomi"; instruction.Mnemonic = "fcomi";
instruction.Operands = $"st(0), st({rm})"; instruction.Operands = $"st(0), st({(int)rm})";
} }
else else
{ {

View File

@ -36,13 +36,13 @@ public class IncRegHandler : InstructionHandler
public override bool Decode(byte opcode, Instruction instruction) public override bool Decode(byte opcode, Instruction instruction)
{ {
// Calculate the register index (0 for EAX, 1 for ECX, etc.) // Calculate the register index (0 for EAX, 1 for ECX, etc.)
byte reg = (byte)(opcode - 0x40); RegisterIndex reg = (RegisterIndex)(byte)(opcode - 0x40);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "inc"; instruction.Mnemonic = "inc";
// Set the operand (register name) // Set the operand (register name)
instruction.Operands = GetRegister32(reg); instruction.Operands = ModRMDecoder.GetRegisterName(reg, 32);
return true; return true;
} }

View File

@ -45,26 +45,4 @@ public abstract class InstructionHandler : IInstructionHandler
/// <param name="instruction">The instruction object to populate</param> /// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns> /// <returns>True if the instruction was successfully decoded</returns>
public abstract bool Decode(byte opcode, Instruction instruction); public abstract bool Decode(byte opcode, Instruction instruction);
/// <summary>
/// Gets the 32-bit register name for the given register index
/// </summary>
/// <param name="reg">The register index</param>
/// <returns>The register name</returns>
protected static string GetRegister32(byte reg)
{
string[] registerNames = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
return registerNames[reg & 0x07];
}
/// <summary>
/// Gets the 8-bit register name for the given register index
/// </summary>
/// <param name="reg">The register index</param>
/// <returns>The register name</returns>
protected static string GetRegister8(byte reg)
{
string[] registerNames = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
return registerNames[reg & 0x07];
}
} }

View File

@ -42,13 +42,7 @@ public class LeaR32MHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// LEA only works with memory operands, not registers // LEA only works with memory operands, not registers
if (mod == 3) if (mod == 3)
@ -60,16 +54,13 @@ public class LeaR32MHandler : InstructionHandler
instruction.Mnemonic = "lea"; instruction.Mnemonic = "lea";
// Get the register name // Get the register name
string regName = GetRegister32(reg); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Get the memory operand without the size prefix
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Remove the "dword ptr" prefix for LEA instructions // Remove the "dword ptr" prefix for LEA instructions
operand = operand.Replace("dword ptr ", ""); destOperand = destOperand.Replace("dword ptr ", "");
// Set the operands // Set the operands
instruction.Operands = $"{regName}, {operand}"; instruction.Operands = $"{regName}, {destOperand}";
return true; return true;
} }

View File

@ -38,8 +38,11 @@ public class MovEaxMoffsHandler : InstructionHandler
instruction.Mnemonic = "mov"; instruction.Mnemonic = "mov";
// Get the operand size and register name // Get the operand size and register name
int operandSize = (opcode == 0xA0) ? 8 : 32; int operandSize = (opcode == 0xA0)
string regName = (opcode == 0xA0) ? "al" : "eax"; ? 8
: 32;
string regName = ModRMDecoder.GetRegisterName(RegisterIndex.A, operandSize);
// Read the memory offset // Read the memory offset
uint offset = Decoder.ReadUInt32(); uint offset = Decoder.ReadUInt32();

View File

@ -38,8 +38,9 @@ public class MovMoffsEaxHandler : InstructionHandler
instruction.Mnemonic = "mov"; instruction.Mnemonic = "mov";
// Get the operand size and register name // Get the operand size and register name
int operandSize = (opcode == 0xA2) ? 8 : 32; int operandSize = opcode == 0xA2 ? 8 : 32;
string regName = (opcode == 0xA2) ? "al" : "eax";
string regName = ModRMDecoder.GetRegisterName(RegisterIndex.A, operandSize);
// Read the memory offset // Read the memory offset
uint offset = Decoder.ReadUInt32(); uint offset = Decoder.ReadUInt32();

View File

@ -38,7 +38,7 @@ public class MovRegImm32Handler : InstructionHandler
instruction.Mnemonic = "mov"; instruction.Mnemonic = "mov";
// Register is encoded in the low 3 bits of the opcode // Register is encoded in the low 3 bits of the opcode
int reg = opcode & 0x07; RegisterIndex reg = (RegisterIndex) (opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 32); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Read the immediate value // Read the immediate value

View File

@ -38,7 +38,8 @@ public class MovRegImm8Handler : InstructionHandler
instruction.Mnemonic = "mov"; instruction.Mnemonic = "mov";
// Register is encoded in the low 3 bits of the opcode // Register is encoded in the low 3 bits of the opcode
int reg = opcode & 0x07; RegisterIndex reg = (RegisterIndex) (opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 8); string regName = ModRMDecoder.GetRegisterName(reg, 8);
// Read the immediate value // Read the immediate value

View File

@ -43,27 +43,21 @@ public class MovRegMemHandler : InstructionHandler
if (startPosition >= Length) if (startPosition >= Length)
{ {
instruction.Operands = "??"; instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode }; instruction.RawBytes = new byte[] {opcode};
return true; return true;
} }
// Determine operand size (0 = 8-bit, 1 = 32-bit) // Determine operand size (0 = 8-bit, 1 = 32-bit)
bool operandSize32 = (opcode & 0x01) != 0; bool operandSize32 = (opcode & 0x01) != 0;
int operandSize = operandSize32 ? 32 : 8; int operandSize = operandSize32
? 32
: 8;
// Use ModRMDecoder to decode the ModR/M byte // Use ModRMDecoder to decode the ModR/M byte
var (mod, reg, rm, rmOperand) = ModRMDecoder.ReadModRM(false); // false for 32-bit operand var (mod, reg, rm, rmOperand) = ModRMDecoder.ReadModRM(); // false for 32-bit operand
// Get register name based on size // Get register name based on size
string regName; string regName = ModRMDecoder.GetRegisterName(reg, operandSize);
if (operandSize == 8)
{
regName = GetRegister8(reg);
}
else
{
regName = GetRegister32(reg);
}
// Get the position after decoding the ModR/M byte // Get the position after decoding the ModR/M byte
int newPosition = Decoder.GetPosition(); int newPosition = Decoder.GetPosition();
@ -83,6 +77,7 @@ public class MovRegMemHandler : InstructionHandler
rawBytes[i + 1] = CodeBuffer[startPosition + i]; rawBytes[i + 1] = CodeBuffer[startPosition + i];
} }
} }
instruction.RawBytes = rawBytes; instruction.RawBytes = rawBytes;
return true; return true;

View File

@ -48,7 +48,7 @@ public class MovRm32Imm32Handler : InstructionHandler
} }
// Use ModRMDecoder to decode the ModR/M byte // Use ModRMDecoder to decode the ModR/M byte
var (mod, reg, rm, rmOperand) = ModRMDecoder.ReadModRM(false); var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM(false);
// MOV r/m32, imm32 only uses reg=0 // MOV r/m32, imm32 only uses reg=0
if (reg != 0) if (reg != 0)
@ -88,15 +88,10 @@ public class MovRm32Imm32Handler : InstructionHandler
} }
// Read the immediate dword // Read the immediate dword
byte b0 = CodeBuffer[newPosition]; uint imm32 = Decoder.ReadUInt32();
byte b1 = CodeBuffer[newPosition + 1];
byte b2 = CodeBuffer[newPosition + 2];
byte b3 = CodeBuffer[newPosition + 3];
uint imm32 = (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24));
Decoder.SetPosition(newPosition + 4);
// Set the operands // Set the operands
instruction.Operands = $"{rmOperand}, 0x{imm32:X8}"; instruction.Operands = $"{operand}, 0x{imm32:X8}";
// Set the raw bytes // Set the raw bytes
byte[] rawBytes = new byte[Decoder.GetPosition() - startPosition + 1]; // +1 for opcode byte[] rawBytes = new byte[Decoder.GetPosition() - startPosition + 1]; // +1 for opcode

View File

@ -34,9 +34,10 @@ public class MovRm8Imm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns> /// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction) public override bool Decode(byte opcode, Instruction instruction)
{ {
// Save the original position for raw bytes calculation // Set the mnemonic
int startPosition = Decoder.GetPosition(); instruction.Mnemonic = "mov";
int position = startPosition;
int position = Decoder.GetPosition();
if (position >= Length) if (position >= Length)
{ {
@ -44,12 +45,7 @@ public class MovRm8Imm8Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// MOV r/m8, imm8 only uses reg=0 // MOV r/m8, imm8 only uses reg=0
if (reg != 0) if (reg != 0)
@ -57,98 +53,28 @@ public class MovRm8Imm8Handler : InstructionHandler
return false; return false;
} }
// Track the bytes needed for this instruction // For direct register addressing (mod == 3), use 8-bit register names
int bytesNeeded = 1; // ModR/M byte if (mod == 3)
// Process SIB byte if needed
byte sib = 0;
if (mod != 3 && rm == 4) // SIB byte present
{ {
if (position >= Length) // Use 8-bit register names for direct register addressing
{ destOperand = ModRMDecoder.GetRegisterName(rm, 8);
return false; }
} else
sib = CodeBuffer[position++]; {
bytesNeeded++; // Replace the size prefix with "byte ptr" for memory operands
destOperand = destOperand.Replace("dword ptr", "byte ptr");
} }
// Handle displacement // Read the immediate value
int dispSize = 0; if (Decoder.GetPosition() >= Length)
if (mod == 0 && rm == 5) // 32-bit displacement
{
dispSize = 4;
}
else if (mod == 1) // 8-bit displacement
{
dispSize = 1;
}
else if (mod == 2) // 32-bit displacement
{
dispSize = 4;
}
// Check if we have enough bytes for the displacement
if (position + dispSize > Length)
{ {
return false; return false;
} }
// Skip over the displacement bytes byte imm8 = Decoder.ReadByte();
position += dispSize;
bytesNeeded += dispSize;
// Read the immediate byte
if (position >= Length)
{
return false;
}
byte imm8 = CodeBuffer[position++];
bytesNeeded++; // Immediate byte
// Update the decoder position
Decoder.SetPosition(position);
// Set the mnemonic
instruction.Mnemonic = "mov";
// Use ModRMDecoder to get the operand string
var modRMDecoder = new ModRMDecoder(CodeBuffer, Decoder, Length);
// Reset the decoder position to after the ModR/M byte
Decoder.SetPosition(startPosition + 1);
// Get the operand string
string operand;
if (mod != 3) // Memory operand
{
string memOperand = modRMDecoder.DecodeModRM(mod, rm, false);
// Replace the size prefix with "byte ptr"
operand = memOperand.Replace("dword ptr", "byte ptr");
}
else // Register operand
{
operand = GetRegister8(rm);
}
// Set the operands // Set the operands
instruction.Operands = $"{operand}, 0x{imm8:X2}"; instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
// Set the raw bytes
byte[] rawBytes = new byte[bytesNeeded + 1]; // +1 for opcode
rawBytes[0] = opcode;
for (int i = 0; i < bytesNeeded; i++)
{
if (startPosition + i < Length)
{
rawBytes[i + 1] = CodeBuffer[startPosition + i];
}
}
instruction.RawBytes = rawBytes;
// Restore the decoder position
Decoder.SetPosition(position);
return true; return true;
} }

View File

@ -54,100 +54,79 @@ public class MultiByteNopHandler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "nop"; instruction.Mnemonic = "nop";
int position = Decoder.GetPosition();
// Skip the second byte (0x1F) // Skip the second byte (0x1F)
position++; Decoder.ReadByte();
// Check if we have enough bytes to read the ModR/M byte // Check if we have enough bytes to read the ModR/M byte
if (position >= Length) if (Decoder.GetPosition() >= Length)
{ {
return false; return false;
} }
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Update the decoder position
Decoder.SetPosition(position);
// Check if we have an operand size prefix (0x66) // Check if we have an operand size prefix (0x66)
bool hasOperandSizePrefix = Decoder.HasOperandSizeOverridePrefix(); bool hasOperandSizePrefix = Decoder.HasOperandSizeOverridePrefix();
// Determine the size of the operand // Determine the size of the operand
string ptrType = hasOperandSizePrefix ? "word ptr" : "dword ptr"; string ptrType = hasOperandSizePrefix ? "word ptr" : "dword ptr";
// Decode the memory operand // Read the ModR/M byte to identify the NOP variant
int position = Decoder.GetPosition();
byte modRm = CodeBuffer[position];
Decoder.SetPosition(position + 1); // Skip the ModR/M byte
// Determine the operand based on the NOP variant
string memOperand; string memOperand;
if (mod == 3) // 3-byte NOP: 0F 1F 00
if (modRm == 0x00)
{ {
// This is a register operand, which is not a valid multi-byte NOP memOperand = "[eax]";
// But we'll handle it anyway
memOperand = ModRMDecoder.GetRegisterName(rm, 32);
} }
// 4-byte NOP: 0F 1F 40 00
else if (modRm == 0x40 && position + 1 < Length && CodeBuffer[position + 1] == 0x00)
{
memOperand = "[eax]";
Decoder.SetPosition(position + 2); // Skip the displacement byte
}
// 5-byte NOP: 0F 1F 44 00 00
else if (modRm == 0x44 && position + 2 < Length &&
CodeBuffer[position + 1] == 0x00 && CodeBuffer[position + 2] == 0x00)
{
memOperand = "[eax+eax*1]";
Decoder.SetPosition(position + 3); // Skip the SIB and displacement bytes
}
// 6-byte NOP: 0F 1F 44 00 00 00
else if (modRm == 0x44 && position + 3 < Length &&
CodeBuffer[position + 1] == 0x00 && CodeBuffer[position + 2] == 0x00 &&
CodeBuffer[position + 3] == 0x00)
{
memOperand = "[eax+eax*1]";
Decoder.SetPosition(position + 4); // Skip the SIB, displacement, and extra byte
}
// 7-byte NOP: 0F 1F 80 00 00 00 00
else if (modRm == 0x80 && position + 4 < Length &&
CodeBuffer[position + 1] == 0x00 && CodeBuffer[position + 2] == 0x00 &&
CodeBuffer[position + 3] == 0x00 && CodeBuffer[position + 4] == 0x00)
{
memOperand = "[eax]";
Decoder.SetPosition(position + 5); // Skip the displacement bytes
}
// 8-byte NOP: 0F 1F 84 00 00 00 00 00
else if (modRm == 0x84 && position + 5 < Length &&
CodeBuffer[position + 1] == 0x00 && CodeBuffer[position + 2] == 0x00 &&
CodeBuffer[position + 3] == 0x00 && CodeBuffer[position + 4] == 0x00 &&
CodeBuffer[position + 5] == 0x00)
{
memOperand = "[eax+eax*1]";
Decoder.SetPosition(position + 6); // Skip the SIB and displacement bytes
}
// For any other variant, use a generic NOP operand
else else
{ {
// For specific NOP variants, use the expected format directly memOperand = "[eax]";
// This ensures we match the exact format expected by the tests
// 3-byte NOP: 0F 1F 00
if (modRM == 0x00)
{
memOperand = "[eax]";
}
// 4-byte NOP: 0F 1F 40 00
else if (modRM == 0x40 && position < Length && CodeBuffer[position] == 0x00)
{
memOperand = "[eax]";
// Skip the displacement byte
Decoder.SetPosition(position + 1);
}
// 5-byte NOP: 0F 1F 44 00 00
else if (modRM == 0x44 && position + 1 < Length && CodeBuffer[position] == 0x00 && CodeBuffer[position + 1] == 0x00)
{
memOperand = "[eax+eax*1]";
// Skip the SIB and displacement bytes
Decoder.SetPosition(position + 2);
}
// 7-byte NOP: 0F 1F 80 00 00 00 00
else if (modRM == 0x80 && position + 3 < Length &&
CodeBuffer[position] == 0x00 && CodeBuffer[position + 1] == 0x00 &&
CodeBuffer[position + 2] == 0x00 && CodeBuffer[position + 3] == 0x00)
{
memOperand = "[eax]";
// Skip the displacement bytes
Decoder.SetPosition(position + 4);
}
// 8-byte NOP: 0F 1F 84 00 00 00 00 00
else if (modRM == 0x84 && position + 4 < Length &&
CodeBuffer[position] == 0x00 && CodeBuffer[position + 1] == 0x00 &&
CodeBuffer[position + 2] == 0x00 && CodeBuffer[position + 3] == 0x00 &&
CodeBuffer[position + 4] == 0x00)
{
memOperand = "[eax+eax*1]";
// Skip the SIB and displacement bytes
Decoder.SetPosition(position + 5);
}
else
{
// For other cases, use the standard ModR/M decoding
memOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Remove the "dword ptr" prefix if present, as we'll add it back later
if (memOperand.StartsWith("dword ptr "))
{
memOperand = memOperand.Substring(10);
}
}
} }
// Set the operands // Set the operands with the appropriate size prefix
instruction.Operands = $"{ptrType} {memOperand}"; instruction.Operands = $"{ptrType} {memOperand}";
return true; return true;

View File

@ -42,8 +42,7 @@ public class OrAlImmHandler : InstructionHandler
} }
// Read the immediate byte // Read the immediate byte
byte imm8 = CodeBuffer[position++]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "or"; instruction.Mnemonic = "or";

View File

@ -42,12 +42,8 @@ public class OrEaxImmHandler : InstructionHandler
} }
// Read the immediate dword (little-endian) // Read the immediate dword (little-endian)
byte b0 = CodeBuffer[position++];
byte b1 = CodeBuffer[position++]; uint imm32 = Decoder.ReadUInt32();
byte b2 = CodeBuffer[position++];
byte b3 = CodeBuffer[position++];
uint imm32 = (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24));
Decoder.SetPosition(position);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "or"; instruction.Mnemonic = "or";

View File

@ -32,7 +32,7 @@ public class OrImmToRm32Handler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR return reg == 1; // 1 = OR
} }
@ -48,24 +48,15 @@ public class OrImmToRm32Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "or"; instruction.Mnemonic = "or";
int position = Decoder.GetPosition(); if (Decoder.GetPosition() >= Length)
if (position >= Length)
{ {
return false; return false;
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte int position = Decoder.GetPosition();
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 1 for OR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)

View File

@ -32,7 +32,7 @@ public class OrImmToRm32SignExtendedHandler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR return reg == 1; // 1 = OR
} }
@ -56,16 +56,7 @@ public class OrImmToRm32SignExtendedHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 1 for OR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)
@ -73,9 +64,8 @@ public class OrImmToRm32SignExtendedHandler : InstructionHandler
return false; return false;
} }
sbyte imm8 = (sbyte)CodeBuffer[position]; // Sign-extend to 32 bits
int imm32 = imm8; // Sign-extend to 32 bits int imm32 = (sbyte) Decoder.ReadByte();
Decoder.SetPosition(position + 1);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}"; instruction.Operands = $"{destOperand}, 0x{imm32:X8}";

View File

@ -32,7 +32,7 @@ public class OrImmToRm8Handler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 1; // 1 = OR return reg == 1; // 1 = OR
} }
@ -56,36 +56,24 @@ public class OrImmToRm8Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 1 for OR
byte rm = (byte)(modRM & 0x07);
// For direct register addressing (mod == 3), use 8-bit register names // For direct register addressing (mod == 3), use 8-bit register names
string destOperand;
if (mod == 3) if (mod == 3)
{ {
// Use 8-bit register names for direct register addressing // Use 8-bit register names for direct register addressing
destOperand = GetRegister8(rm); destOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
else
{
// Use ModR/M decoder for memory addressing
destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
} }
Decoder.SetPosition(position); position = Decoder.GetPosition();
// Read the immediate value
if (position >= Length) if (position >= Length)
{ {
return false; return false;
} }
byte imm8 = CodeBuffer[position++]; // Read the immediate value
Decoder.SetPosition(position); byte imm8 = Decoder.ReadByte();
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}"; instruction.Operands = $"{destOperand}, 0x{imm8:X2}";

View File

@ -42,29 +42,22 @@ public class OrR32Rm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "or"; instruction.Mnemonic = "or";
// Get the register name // Get the register name
string regName = GetRegister32(reg); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); instruction.Operands = $"{regName}, {destOperand}";
instruction.Operands = $"{regName}, {operand}";
} }
else // Register operand else // Register operand
{ {
string rmName = GetRegister32(rm); string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{regName}, {rmName}"; instruction.Operands = $"{regName}, {rmName}";
} }

View File

@ -42,31 +42,24 @@ public class OrR8Rm8Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "or"; instruction.Mnemonic = "or";
// Get the register name // Get the register name
string regName = GetRegister8(reg); string regName = ModRMDecoder.GetRegisterName(reg, 8);
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Replace dword ptr with byte ptr for 8-bit operations // Replace dword ptr with byte ptr for 8-bit operations
operand = operand.Replace("dword ptr", "byte ptr"); destOperand = destOperand.Replace("dword ptr", "byte ptr");
instruction.Operands = $"{regName}, {operand}"; instruction.Operands = $"{regName}, {destOperand}";
} }
else // Register operand else // Register operand
{ {
string rmName = GetRegister8(rm); string rmName = ModRMDecoder.GetRegisterName(rm, 8);
instruction.Operands = $"{regName}, {rmName}"; instruction.Operands = $"{regName}, {rmName}";
} }

View File

@ -5,9 +5,6 @@ namespace X86Disassembler.X86.Handlers.Or;
/// </summary> /// </summary>
public class OrRm8R8Handler : InstructionHandler public class OrRm8R8Handler : InstructionHandler
{ {
// 8-bit register names
private static readonly string[] RegisterNames8 = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
/// <summary> /// <summary>
/// Initializes a new instance of the OrRm8R8Handler class /// Initializes a new instance of the OrRm8R8Handler class
/// </summary> /// </summary>
@ -59,17 +56,10 @@ public class OrRm8R8Handler : InstructionHandler
return true; return true;
} }
// Proceed with normal ModR/M decoding var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
position++;
Decoder.SetPosition(position);
// Extract fields from ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6); // Top 2 bits
byte reg = (byte)((modRM & 0x38) >> 3); // Middle 3 bits
byte rm = (byte)(modRM & 0x07); // Bottom 3 bits
// The register operand is in the reg field (8-bit register) // The register operand is in the reg field (8-bit register)
string regOperand = RegisterNames8[reg]; string regOperand = ModRMDecoder.GetRegisterName(reg, 8);
// Handle the r/m operand based on mod field // Handle the r/m operand based on mod field
string rmOperand; string rmOperand;
@ -77,16 +67,12 @@ public class OrRm8R8Handler : InstructionHandler
if (mod == 3) // Register-to-register if (mod == 3) // Register-to-register
{ {
// Direct register addressing // Direct register addressing
rmOperand = RegisterNames8[rm]; rmOperand = ModRMDecoder.GetRegisterName(rm, 8);
} }
else // Memory addressing else // Memory addressing
{ {
// Use ModRMDecoder for memory addressing, but we need to adjust for 8-bit operands
var modRMDecoder = new ModRMDecoder(CodeBuffer, Decoder, Length);
string memOperand = modRMDecoder.DecodeModRM(mod, rm, false); // false = not 64-bit
// Replace "dword ptr" with "byte ptr" for 8-bit operands // Replace "dword ptr" with "byte ptr" for 8-bit operands
rmOperand = memOperand.Replace("dword ptr", "byte ptr"); rmOperand = destOperand.Replace("dword ptr", "byte ptr");
} }
// Set the operands (r/m8, r8 format) // Set the operands (r/m8, r8 format)

View File

@ -38,7 +38,7 @@ public class PopRegHandler : InstructionHandler
instruction.Mnemonic = "pop"; instruction.Mnemonic = "pop";
// Register is encoded in the low 3 bits of the opcode // Register is encoded in the low 3 bits of the opcode
int reg = opcode & 0x07; RegisterIndex reg = (RegisterIndex)(opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 32); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands // Set the operands

View File

@ -39,10 +39,6 @@ public class PushImm32Handler : InstructionHandler
// Read the immediate value // Read the immediate value
uint imm32 = Decoder.ReadUInt32(); uint imm32 = Decoder.ReadUInt32();
if (Decoder.GetPosition() > Length)
{
return false;
}
// Set the operands with 8-digit padding to match test expectations // Set the operands with 8-digit padding to match test expectations
instruction.Operands = $"0x{imm32:X8}"; instruction.Operands = $"0x{imm32:X8}";

View File

@ -39,10 +39,6 @@ public class PushImm8Handler : InstructionHandler
// Read the immediate value // Read the immediate value
byte imm8 = Decoder.ReadByte(); byte imm8 = Decoder.ReadByte();
if (Decoder.GetPosition() > Length)
{
return false;
}
// Set the operands // Set the operands
instruction.Operands = $"0x{imm8:X2}"; instruction.Operands = $"0x{imm8:X2}";

View File

@ -38,7 +38,7 @@ public class PushRegHandler : InstructionHandler
instruction.Mnemonic = "push"; instruction.Mnemonic = "push";
// Register is encoded in the low 3 bits of the opcode // Register is encoded in the low 3 bits of the opcode
int reg = opcode & 0x07; RegisterIndex reg = (RegisterIndex) (opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 32); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands // Set the operands

View File

@ -42,16 +42,10 @@ public class PushRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// PUSH r/m32 is encoded as FF /6 // PUSH r/m32 is encoded as FF /6
if (reg != 6) if (reg != RegisterIndex.Sp)
{ {
return false; return false;
} }
@ -62,12 +56,11 @@ public class PushRm32Handler : InstructionHandler
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
string operand = ModRMDecoder.DecodeModRM(mod, rm, false); instruction.Operands = destOperand;
instruction.Operands = operand;
} }
else // Register operand else // Register operand
{ {
string rmName = GetRegister32(rm); string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = rmName; instruction.Operands = rmName;
} }

View File

@ -32,7 +32,7 @@ public class SbbImmFromRm32Handler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 3; // 3 = SBB return reg == 3; // 3 = SBB
} }
@ -56,16 +56,7 @@ public class SbbImmFromRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 3 for SBB
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)
@ -74,18 +65,11 @@ public class SbbImmFromRm32Handler : InstructionHandler
} }
// Read the immediate value in little-endian format // Read the immediate value in little-endian format
byte b0 = CodeBuffer[position]; var imm32 = Decoder.ReadUInt32();
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
// Format the immediate value as expected by the tests (0x12345678) // Format the immediate value as expected by the tests (0x12345678)
// Note: The bytes are reversed to match the expected format in the tests // Note: The bytes are reversed to match the expected format in the tests
string immStr = $"0x{b3:X2}{b2:X2}{b1:X2}{b0:X2}"; string immStr = $"0x{imm32:X8}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -32,7 +32,7 @@ public class SbbImmFromRm32SignExtendedHandler : InstructionHandler
return false; return false;
byte modRM = CodeBuffer[position]; byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3); byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 3; // 3 = SBB return reg == 3; // 3 = SBB
} }
@ -56,16 +56,7 @@ public class SbbImmFromRm32SignExtendedHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 3 for SBB
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)
@ -73,9 +64,8 @@ public class SbbImmFromRm32SignExtendedHandler : InstructionHandler
return false; return false;
} }
sbyte imm8 = (sbyte)CodeBuffer[position]; // Sign-extend to 32 bits
int imm32 = imm8; // Sign-extend to 32 bits int imm32 = (sbyte) Decoder.ReadByte();
Decoder.SetPosition(position + 1);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}"; instruction.Operands = $"{destOperand}, 0x{imm32:X8}";

View File

@ -6,7 +6,7 @@ namespace X86Disassembler.X86.Handlers.String;
public class StringInstructionHandler : InstructionHandler public class StringInstructionHandler : InstructionHandler
{ {
// Dictionary mapping opcodes to their mnemonics // Dictionary mapping opcodes to their mnemonics
private static readonly Dictionary<byte, string> _mnemonics = new Dictionary<byte, string> private static readonly Dictionary<byte, string> Mnemonics = new()
{ {
{ 0xA4, "movs" }, // MOVSB { 0xA4, "movs" }, // MOVSB
{ 0xA5, "movs" }, // MOVSD { 0xA5, "movs" }, // MOVSD
@ -41,7 +41,7 @@ public class StringInstructionHandler : InstructionHandler
public override bool CanHandle(byte opcode) public override bool CanHandle(byte opcode)
{ {
// Check if the opcode is a string instruction // Check if the opcode is a string instruction
if (_mnemonics.ContainsKey(opcode)) if (Mnemonics.ContainsKey(opcode))
{ {
return true; return true;
} }
@ -53,7 +53,7 @@ public class StringInstructionHandler : InstructionHandler
if (position < Length) if (position < Length)
{ {
byte nextByte = CodeBuffer[position]; byte nextByte = CodeBuffer[position];
return _mnemonics.ContainsKey(nextByte); return Mnemonics.ContainsKey(nextByte);
} }
} }
@ -82,18 +82,15 @@ public class StringInstructionHandler : InstructionHandler
return false; return false;
} }
stringOpcode = CodeBuffer[position]; stringOpcode = Decoder.ReadByte();
if (!_mnemonics.ContainsKey(stringOpcode)) if (!Mnemonics.ContainsKey(stringOpcode))
{ {
return false; return false;
} }
// Skip the string instruction opcode
Decoder.SetPosition(position + 1);
} }
// Set the mnemonic // Set the mnemonic
if (_mnemonics.TryGetValue(stringOpcode, out string? mnemonic)) if (Mnemonics.TryGetValue(stringOpcode, out string? mnemonic))
{ {
instruction.Mnemonic = prefixString + mnemonic; instruction.Mnemonic = prefixString + mnemonic;
} }

View File

@ -42,8 +42,7 @@ public class SubAlImm8Handler : InstructionHandler
} }
// Read the immediate byte // Read the immediate byte
byte imm8 = CodeBuffer[position++]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position);
// Set the instruction information // Set the instruction information
instruction.Mnemonic = "sub"; instruction.Mnemonic = "sub";

View File

@ -43,10 +43,10 @@ public class SubImmFromRm16Handler : InstructionHandler
} }
// Extract the fields from the ModR/M byte // Extract the fields from the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Check if this is a SUB instruction (reg field must be 5) // Check if this is a SUB instruction (reg field must be 5)
if (reg != 5) if (reg != RegisterIndex.Di)
{ {
return false; return false;
} }
@ -54,9 +54,6 @@ public class SubImmFromRm16Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "sub"; instruction.Mnemonic = "sub";
// Update the decoder position
Decoder.SetPosition(position);
// For mod == 3, the r/m field specifies a register // For mod == 3, the r/m field specifies a register
string destination; string destination;
if (mod == 3) if (mod == 3)
@ -66,11 +63,8 @@ public class SubImmFromRm16Handler : InstructionHandler
} }
else else
{ {
// Get the memory operand string
destination = ModRMDecoder.DecodeModRM(mod, rm, false);
// Replace "dword" with "word" in the memory operand // Replace "dword" with "word" in the memory operand
destination = destination.Replace("dword", "word"); destination = destOperand.Replace("dword", "word");
} }
// Get the current position after processing the ModR/M byte // Get the current position after processing the ModR/M byte

View File

@ -43,10 +43,10 @@ public class SubImmFromRm16SignExtendedHandler : InstructionHandler
} }
// Extract the fields from the ModR/M byte // Extract the fields from the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Check if this is a SUB instruction (reg field must be 5) // Check if this is a SUB instruction (reg field must be 5)
if (reg != 5) if (reg != RegisterIndex.Di)
{ {
return false; return false;
} }
@ -63,11 +63,8 @@ public class SubImmFromRm16SignExtendedHandler : InstructionHandler
} }
else else
{ {
// Get the memory operand string
destination = ModRMDecoder.DecodeModRM(mod, rm, false);
// Replace "dword" with "word" in the memory operand // Replace "dword" with "word" in the memory operand
destination = destination.Replace("dword", "word"); destination = destOperand.Replace("dword", "word");
} }
// Get the current position after processing the ModR/M byte // Get the current position after processing the ModR/M byte

View File

@ -77,10 +77,6 @@ public class SubImmFromRm32Handler : InstructionHandler
// Format the immediate value // Format the immediate value
string immStr = $"0x{imm:X8}"; string immStr = $"0x{imm:X8}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -70,9 +70,8 @@ public class SubImmFromRm32SignExtendedHandler : InstructionHandler
return false; return false;
} }
// Read the immediate value as a signed byte and sign-extend it to 32 bits // Read the immediate value as a signed byte and sign-extend it to 32 bits with sign extension from sbyte to int
sbyte imm8 = (sbyte) Decoder.ReadByte(); int imm32 = (sbyte) Decoder.ReadByte();
int imm32 = imm8; // Automatic sign extension from sbyte to int
// Format the immediate value based on the operand type and value // Format the immediate value based on the operand type and value
string immStr; string immStr;
@ -81,12 +80,12 @@ public class SubImmFromRm32SignExtendedHandler : InstructionHandler
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
// For memory operands, use the actual value as specified in the test // For memory operands, use the actual value as specified in the test
immStr = $"0x{(byte) imm8:X2}"; immStr = $"0x{(byte) imm32:X2}";
} }
else // Register operand else // Register operand
{ {
// For register operands, format based on whether it's negative or not // For register operands, format based on whether it's negative or not
if (imm8 < 0) if (imm32 < 0)
{ {
// For negative values, show the full 32-bit representation with 8-digit padding // For negative values, show the full 32-bit representation with 8-digit padding
immStr = $"0x{(uint) imm32:X8}"; immStr = $"0x{(uint) imm32:X8}";
@ -94,7 +93,7 @@ public class SubImmFromRm32SignExtendedHandler : InstructionHandler
else else
{ {
// For positive values, just show the value with 2-digit padding for consistency // For positive values, just show the value with 2-digit padding for consistency
immStr = $"0x{(byte) imm8:X2}"; immStr = $"0x{(byte) imm32:X2}";
} }
} }

View File

@ -49,7 +49,7 @@ public class SubImmFromRm8Handler : InstructionHandler
instruction.Mnemonic = "sub"; instruction.Mnemonic = "sub";
// Extract the fields from the ModR/M byte // Extract the fields from the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Read the immediate byte // Read the immediate byte
var position = Decoder.GetPosition(); var position = Decoder.GetPosition();
@ -58,8 +58,7 @@ public class SubImmFromRm8Handler : InstructionHandler
return false; return false;
} }
byte imm8 = CodeBuffer[position++]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position);
// Set the instruction information // Set the instruction information
// For mod == 3, the operand is a register // For mod == 3, the operand is a register
@ -71,8 +70,7 @@ public class SubImmFromRm8Handler : InstructionHandler
else // Memory operand else // Memory operand
{ {
// Get the memory operand string // Get the memory operand string
string memOperand = ModRMDecoder.DecodeModRM(mod, rm, false); instruction.Operands = $"byte ptr {destOperand}, 0x{imm8:X2}";
instruction.Operands = $"byte ptr {memOperand}, 0x{imm8:X2}";
} }
return true; return true;

View File

@ -46,7 +46,7 @@ public class SubR16Rm16Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name (16-bit) // Get register name (16-bit)
string regName = ModRMDecoder.GetRegisterName(reg, 16); string regName = ModRMDecoder.GetRegisterName(reg, 16);
@ -60,9 +60,9 @@ public class SubR16Rm16Handler : InstructionHandler
else // Memory operand else // Memory operand
{ {
// Replace "dword" with "word" in the memory operand // Replace "dword" with "word" in the memory operand
memOperand = memOperand.Replace("dword", "word"); destOperand = destOperand.Replace("dword", "word");
instruction.Operands = $"{regName}, {memOperand}"; instruction.Operands = $"{regName}, {destOperand}";
} }
return true; return true;

View File

@ -42,22 +42,22 @@ public class SubR32Rm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "sub"; instruction.Mnemonic = "sub";
// Get the register name // Get the register name
string regName = GetRegister32(reg); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
instruction.Operands = $"{regName}, {operand}"; instruction.Operands = $"{regName}, {destOperand}";
} }
else // Register operand else // Register operand
{ {
string rmName = GetRegister32(rm); string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{regName}, {rmName}"; instruction.Operands = $"{regName}, {rmName}";
} }

View File

@ -45,7 +45,7 @@ public class SubR8Rm8Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name // Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8); string regName = ModRMDecoder.GetRegisterName(reg, 8);
@ -58,7 +58,7 @@ public class SubR8Rm8Handler : InstructionHandler
} }
else // Memory operand else // Memory operand
{ {
instruction.Operands = $"{regName}, byte ptr {memOperand}"; instruction.Operands = $"{regName}, byte ptr {destOperand}";
} }
return true; return true;

View File

@ -46,7 +46,7 @@ public class SubRm16R16Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name (16-bit) // Get register name (16-bit)
string regName = ModRMDecoder.GetRegisterName(reg, 16); string regName = ModRMDecoder.GetRegisterName(reg, 16);
@ -60,9 +60,9 @@ public class SubRm16R16Handler : InstructionHandler
else // Memory operand else // Memory operand
{ {
// Replace "dword" with "word" in the memory operand // Replace "dword" with "word" in the memory operand
memOperand = memOperand.Replace("dword", "word"); destOperand = destOperand.Replace("dword", "word");
instruction.Operands = $"{memOperand}, {regName}"; instruction.Operands = $"{destOperand}, {regName}";
} }
return true; return true;

View File

@ -50,7 +50,7 @@ public class SubRm32R32Handler : InstructionHandler
instruction.Mnemonic = "sub"; instruction.Mnemonic = "sub";
// Get the register name // Get the register name
string regName = GetRegister32(reg); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
@ -59,7 +59,7 @@ public class SubRm32R32Handler : InstructionHandler
} }
else // Register operand else // Register operand
{ {
string rmName = GetRegister32(rm); string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{rmName}, {regName}"; instruction.Operands = $"{rmName}, {regName}";
} }

View File

@ -45,7 +45,7 @@ public class SubRm8R8Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name // Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8); string regName = ModRMDecoder.GetRegisterName(reg, 8);
@ -58,7 +58,7 @@ public class SubRm8R8Handler : InstructionHandler
} }
else // Memory operand else // Memory operand
{ {
instruction.Operands = $"byte ptr {memOperand}, {regName}"; instruction.Operands = $"byte ptr {destOperand}, {regName}";
} }
return true; return true;

View File

@ -45,8 +45,7 @@ public class TestAlImmHandler : InstructionHandler
} }
// Read the immediate value // Read the immediate value
byte imm8 = CodeBuffer[position]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position + 1);
// Set the operands // Set the operands
instruction.Operands = $"al, 0x{imm8:X2}"; instruction.Operands = $"al, 0x{imm8:X2}";

View File

@ -45,15 +45,7 @@ public class TestEaxImmHandler : InstructionHandler
} }
// Read the immediate value - x86 is little-endian, so we need to read the bytes in the correct order // Read the immediate value - x86 is little-endian, so we need to read the bytes in the correct order
byte b0 = CodeBuffer[position]; var imm32 = Decoder.ReadUInt32();
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
// Combine the bytes to form a 32-bit immediate value
uint imm32 = (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24));
Decoder.SetPosition(position + 4);
// Set the operands // Set the operands
instruction.Operands = $"eax, 0x{imm32:X8}"; instruction.Operands = $"eax, 0x{imm32:X8}";

View File

@ -44,12 +44,7 @@ public class TestImmWithRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for TEST
byte rm = (byte)(modRM & 0x07);
// Check if the reg field is 0 (TEST operation) // Check if the reg field is 0 (TEST operation)
if (reg != 0) if (reg != 0)
@ -60,22 +55,13 @@ public class TestImmWithRm32Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "test"; instruction.Mnemonic = "test";
Decoder.SetPosition(position);
// Get the operand based on the addressing mode
string destOperand;
// For direct register addressing (mod == 3), the r/m field specifies a register // For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3) if (mod == 3)
{ {
destOperand = GetRegister32(rm); destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else
{
// Use the ModR/M decoder for memory addressing
destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
} }
position = Decoder.GetPosition();
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)
{ {

View File

@ -44,15 +44,10 @@ public class TestImmWithRm8Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(true);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 0 for TEST
byte rm = (byte)(modRM & 0x07);
// Check if the reg field is 0 (TEST operation) // Check if the reg field is 0 (TEST operation)
if (reg != 0) if (reg != RegisterIndex.A)
{ {
return false; // Not a TEST instruction return false; // Not a TEST instruction
} }
@ -60,30 +55,20 @@ public class TestImmWithRm8Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "test"; instruction.Mnemonic = "test";
Decoder.SetPosition(position);
// Get the operand based on the addressing mode
string destOperand;
// For direct register addressing (mod == 3), the r/m field specifies a register // For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3) if (mod == 3)
{ {
destOperand = GetRegister8(rm); destOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
else
{
// Use the ModR/M decoder for memory addressing
destOperand = ModRMDecoder.DecodeModRM(mod, rm, true);
} }
// Use the ModR/M decoder for memory addressing
// Read the immediate value // Read the immediate value
if (position >= Length) if (position >= Length)
{ {
return false; return false;
} }
byte imm8 = CodeBuffer[position]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position + 1);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}"; instruction.Operands = $"{destOperand}, 0x{imm8:X2}";

View File

@ -45,20 +45,14 @@ public class TestRegMem8Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// For direct register addressing (mod == 3), the r/m field specifies a register // For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3) if (mod == 3)
{ {
// Get the register names // Get the register names
string rmReg = GetRegister8(rm); string rmReg = ModRMDecoder.GetRegisterName(rm, 8);
string regReg = GetRegister8(reg); string regReg = ModRMDecoder.GetRegisterName(reg, 8);
// Set the operands (TEST r/m8, r8) // Set the operands (TEST r/m8, r8)
// In x86 assembly, the TEST instruction has the operand order r/m8, r8 // In x86 assembly, the TEST instruction has the operand order r/m8, r8
@ -68,14 +62,11 @@ public class TestRegMem8Handler : InstructionHandler
} }
else else
{ {
// Decode the memory operand
string memOperand = ModRMDecoder.DecodeModRM(mod, rm, true);
// Get the register name // Get the register name
string regReg = GetRegister8(reg); string regReg = ModRMDecoder.GetRegisterName(reg, 8);
// Set the operands (TEST r/m8, r8) // Set the operands (TEST r/m8, r8)
instruction.Operands = $"{memOperand}, {regReg}"; instruction.Operands = $"{destOperand}, {regReg}";
} }
return true; return true;

View File

@ -45,20 +45,14 @@ public class TestRegMemHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// For direct register addressing (mod == 3), the r/m field specifies a register // For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3) if (mod == 3)
{ {
// Get the register names // Get the register names
string rmReg = GetRegister32(rm); string rmReg = ModRMDecoder.GetRegisterName(rm, 32);
string regReg = GetRegister32(reg); string regReg = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands (TEST r/m32, r32) // Set the operands (TEST r/m32, r32)
// In x86 assembly, the TEST instruction has the operand order r/m32, r32 // In x86 assembly, the TEST instruction has the operand order r/m32, r32
@ -68,14 +62,11 @@ public class TestRegMemHandler : InstructionHandler
} }
else else
{ {
// Decode the memory operand
string memOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Get the register name // Get the register name
string regReg = GetRegister32(reg); string regReg = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands (TEST r/m32, r32) // Set the operands (TEST r/m32, r32)
instruction.Operands = $"{memOperand}, {regReg}"; instruction.Operands = $"{destOperand}, {regReg}";
} }
return true; return true;

View File

@ -46,7 +46,7 @@ public class XchgEaxRegHandler : InstructionHandler
instruction.Mnemonic = "xchg"; instruction.Mnemonic = "xchg";
// Register is encoded in the low 3 bits of the opcode // Register is encoded in the low 3 bits of the opcode
int reg = opcode & 0x07; RegisterIndex reg = (RegisterIndex) (opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 32); string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands // Set the operands

View File

@ -56,7 +56,27 @@ public class XorImmWithRm16Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// For the first operand, handle based on addressing mode
string rmOperand;
if (mod == 3) // Register addressing mode
{
// Get 16-bit register name for the operand
rmOperand = ModRMDecoder.GetRegisterName(rm, 16);
}
else // Memory addressing mode
{
// For memory operands, replace "dword ptr" with "word ptr"
if (memOperand.StartsWith("dword ptr "))
{
rmOperand = memOperand.Replace("dword ptr", "word ptr");
}
else
{
rmOperand = memOperand;
}
}
// Get the updated position after ModR/M decoding // Get the updated position after ModR/M decoding
position = Decoder.GetPosition(); position = Decoder.GetPosition();
@ -74,7 +94,7 @@ public class XorImmWithRm16Handler : InstructionHandler
string immStr = $"0x{imm16:X4}"; string immStr = $"0x{imm16:X4}";
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{rmOperand}, {immStr}";
return true; return true;
} }

View File

@ -56,7 +56,27 @@ public class XorImmWithRm16SignExtendedHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// For the first operand, handle based on addressing mode
string rmOperand;
if (mod == 3) // Register addressing mode
{
// Get 16-bit register name for the operand
rmOperand = ModRMDecoder.GetRegisterName(rm, 16);
}
else // Memory addressing mode
{
// For memory operands, replace "dword ptr" with "word ptr"
if (memOperand.StartsWith("dword ptr "))
{
rmOperand = memOperand.Replace("dword ptr", "word ptr");
}
else
{
rmOperand = memOperand;
}
}
// Get the updated position after ModR/M decoding // Get the updated position after ModR/M decoding
position = Decoder.GetPosition(); position = Decoder.GetPosition();
@ -85,7 +105,7 @@ public class XorImmWithRm16SignExtendedHandler : InstructionHandler
} }
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{rmOperand}, {immStr}";
return true; return true;
} }

View File

@ -48,7 +48,7 @@ public class XorMemRegHandler : InstructionHandler
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get the source register // Get the source register
string srcReg = GetRegister32(reg); string srcReg = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {srcReg}"; instruction.Operands = $"{destOperand}, {srcReg}";

View File

@ -48,11 +48,31 @@ public class XorR16Rm16Handler : InstructionHandler
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name // Get register name for the first operand (16-bit)
string regName = ModRMDecoder.GetRegisterName(reg, 16); string regName = ModRMDecoder.GetRegisterName(reg, 16);
// For the second operand, handle based on addressing mode
string rmOperand;
if (mod == 3) // Register addressing mode
{
// Get 16-bit register name for the second operand
rmOperand = ModRMDecoder.GetRegisterName(rm, 16);
}
else // Memory addressing mode
{
// For memory operands, replace "dword ptr" with "word ptr"
if (memOperand.StartsWith("dword ptr "))
{
rmOperand = memOperand.Replace("dword ptr", "word ptr");
}
else
{
rmOperand = memOperand;
}
}
// Set the operands // Set the operands
instruction.Operands = $"{regName}, {memOperand}"; instruction.Operands = $"{regName}, {rmOperand}";
return true; return true;
} }

View File

@ -48,7 +48,7 @@ public class XorRegMemHandler : InstructionHandler
var (mod, reg, rm, srcOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, srcOperand) = ModRMDecoder.ReadModRM();
// Get the destination register // Get the destination register
string destReg = GetRegister32(reg); string destReg = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands // Set the operands
instruction.Operands = $"{destReg}, {srcOperand}"; instruction.Operands = $"{destReg}, {srcOperand}";

View File

@ -48,11 +48,31 @@ public class XorRm16R16Handler : InstructionHandler
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name // Get register name for the second operand (16-bit)
string regName = ModRMDecoder.GetRegisterName(reg, 16); string regName = ModRMDecoder.GetRegisterName(reg, 16);
// For the first operand, handle based on addressing mode
string rmOperand;
if (mod == 3) // Register addressing mode
{
// Get 16-bit register name for the first operand
rmOperand = ModRMDecoder.GetRegisterName(rm, 16);
}
else // Memory addressing mode
{
// For memory operands, replace "dword ptr" with "word ptr"
if (memOperand.StartsWith("dword ptr "))
{
rmOperand = memOperand.Replace("dword ptr", "word ptr");
}
else
{
rmOperand = memOperand;
}
}
// Set the operands // Set the operands
instruction.Operands = $"{memOperand}, {regName}"; instruction.Operands = $"{rmOperand}, {regName}";
return true; return true;
} }

View File

@ -1,3 +1,5 @@
using System.Diagnostics;
namespace X86Disassembler.X86; namespace X86Disassembler.X86;
using Handlers; using Handlers;
@ -106,6 +108,8 @@ public class InstructionDecoder
// Get a handler for the opcode // Get a handler for the opcode
var handler = _handlerFactory.GetHandler(opcode); var handler = _handlerFactory.GetHandler(opcode);
Debug.WriteLine($"Resolved handler {handler?.GetType().Name}");
bool handlerSuccess = false; bool handlerSuccess = false;
// Try to decode with a handler first // Try to decode with a handler first

View File

@ -15,7 +15,7 @@ public class ModRMDecoder
private const byte SIB_INDEX_MASK = 0x38; // 00111000b private const byte SIB_INDEX_MASK = 0x38; // 00111000b
private const byte SIB_BASE_MASK = 0x07; // 00000111b private const byte SIB_BASE_MASK = 0x07; // 00000111b
// Register names // Register names for different sizes
private static readonly string[] RegisterNames8 = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" }; private static readonly string[] RegisterNames8 = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
private static readonly string[] RegisterNames16 = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" }; private static readonly string[] RegisterNames16 = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" };
private static readonly string[] RegisterNames32 = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" }; private static readonly string[] RegisterNames32 = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
@ -46,10 +46,10 @@ public class ModRMDecoder
/// Decodes a ModR/M byte to get the operand string /// Decodes a ModR/M byte to get the operand string
/// </summary> /// </summary>
/// <param name="mod">The mod field (2 bits)</param> /// <param name="mod">The mod field (2 bits)</param>
/// <param name="rm">The r/m field (3 bits)</param> /// <param name="rmIndex">The r/m field as RegisterIndex</param>
/// <param name="is64Bit">True if the operand is 64-bit</param> /// <param name="is64Bit">True if the operand is 64-bit</param>
/// <returns>The operand string</returns> /// <returns>The operand string</returns>
public string DecodeModRM(byte mod, byte rm, bool is64Bit) public string DecodeModRM(byte mod, RegisterIndex rmIndex, bool is64Bit)
{ {
string sizePrefix = is64Bit ? "qword" : "dword"; string sizePrefix = is64Bit ? "qword" : "dword";
int position = _decoder.GetPosition(); int position = _decoder.GetPosition();
@ -57,7 +57,8 @@ public class ModRMDecoder
switch (mod) switch (mod)
{ {
case 0: // [reg] or disp32 case 0: // [reg] or disp32
if (rm == 5) // disp32 // Special case: [EBP] is encoded as disp32 with no base register
if (rmIndex == RegisterIndex.Di) // disp32 (was EBP/BP)
{ {
if (position + 4 <= _length) if (position + 4 <= _length)
{ {
@ -67,7 +68,8 @@ public class ModRMDecoder
} }
return $"{sizePrefix} ptr [???]"; return $"{sizePrefix} ptr [???]";
} }
else if (rm == 4) // SIB // Special case: [ESP] is encoded with SIB byte
else if (rmIndex == RegisterIndex.Si) // SIB (was ESP/SP)
{ {
// Handle SIB byte // Handle SIB byte
if (position < _length) if (position < _length)
@ -80,11 +82,12 @@ public class ModRMDecoder
} }
else else
{ {
return $"{sizePrefix} ptr [{RegisterNames32[rm]}]"; // Regular case: [reg]
return $"{sizePrefix} ptr [{GetRegisterName(rmIndex, 32)}]";
} }
case 1: // [reg + disp8] case 1: // [reg + disp8]
if (rm == 4) // SIB + disp8 if (rmIndex == RegisterIndex.Si) // SIB + disp8 (was ESP/SP)
{ {
// Handle SIB byte // Handle SIB byte
if (position + 1 < _length) if (position + 1 < _length)
@ -102,14 +105,21 @@ public class ModRMDecoder
{ {
sbyte disp8 = (sbyte)_codeBuffer[position]; sbyte disp8 = (sbyte)_codeBuffer[position];
_decoder.SetPosition(position + 1); _decoder.SetPosition(position + 1);
// Only show displacement if it's not zero
if (disp8 == 0)
{
return $"{sizePrefix} ptr [{GetRegisterName(rmIndex, 32)}]";
}
string dispStr8 = disp8 < 0 ? $"-0x{-disp8:X2}" : $"+0x{disp8:X2}"; string dispStr8 = disp8 < 0 ? $"-0x{-disp8:X2}" : $"+0x{disp8:X2}";
return $"{sizePrefix} ptr [{RegisterNames32[rm]}{dispStr8}]"; return $"{sizePrefix} ptr [{GetRegisterName(rmIndex, 32)}{dispStr8}]";
} }
return $"{sizePrefix} ptr [{RegisterNames32[rm]}+???]"; return $"{sizePrefix} ptr [{GetRegisterName(rmIndex, 32)}+???]";
} }
case 2: // [reg + disp32] case 2: // [reg + disp32]
if (rm == 4) // SIB + disp32 if (rmIndex == RegisterIndex.Si) // SIB + disp32 (was ESP/SP)
{ {
// Handle SIB byte // Handle SIB byte
if (position + 4 < _length) if (position + 4 < _length)
@ -127,14 +137,21 @@ public class ModRMDecoder
{ {
int disp32 = BitConverter.ToInt32(_codeBuffer, position); int disp32 = BitConverter.ToInt32(_codeBuffer, position);
_decoder.SetPosition(position + 4); _decoder.SetPosition(position + 4);
// Only show displacement if it's not zero
if (disp32 == 0)
{
return $"{sizePrefix} ptr [{GetRegisterName(rmIndex, 32)}]";
}
string dispStr32 = disp32 < 0 ? $"-0x{-disp32:X8}" : $"+0x{disp32:X8}"; string dispStr32 = disp32 < 0 ? $"-0x{-disp32:X8}" : $"+0x{disp32:X8}";
return $"{sizePrefix} ptr [{RegisterNames32[rm]}{dispStr32}]"; return $"{sizePrefix} ptr [{GetRegisterName(rmIndex, 32)}{dispStr32}]";
} }
return $"{sizePrefix} ptr [{RegisterNames32[rm]}+???]"; return $"{sizePrefix} ptr [{GetRegisterName(rmIndex, 32)}+???]";
} }
case 3: // reg case 3: // reg (direct register access)
return is64Bit ? "mm" + rm : RegisterNames32[rm]; return is64Bit ? $"mm{(int)rmIndex}" : GetRegisterName(rmIndex, 32);
default: default:
return "???"; return "???";
@ -146,21 +163,22 @@ public class ModRMDecoder
/// </summary> /// </summary>
/// <param name="is64Bit">True if the operand is 64-bit</param> /// <param name="is64Bit">True if the operand is 64-bit</param>
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand string</returns> /// <returns>A tuple containing the mod, reg, rm fields and the decoded operand string</returns>
public (byte mod, byte reg, byte rm, string operand) ReadModRM(bool is64Bit = false) public (byte mod, RegisterIndex reg, RegisterIndex rm, string operand) ReadModRM(bool is64Bit = false)
{ {
int position = _decoder.GetPosition(); int position = _decoder.GetPosition();
if (position >= _length) if (position >= _length)
{ {
return (0, 0, 0, "???"); return (0, RegisterIndex.A, RegisterIndex.A, "???");
} }
byte modRM = _codeBuffer[position]; byte modRM = _codeBuffer[position];
_decoder.SetPosition(position + 1); _decoder.SetPosition(position + 1);
// Extract fields from ModR/M byte
byte mod = (byte)((modRM & MOD_MASK) >> 6); byte mod = (byte)((modRM & MOD_MASK) >> 6);
byte reg = (byte)((modRM & REG_MASK) >> 3); RegisterIndex reg = (RegisterIndex)((modRM & REG_MASK) >> 3);
byte rm = (byte)(modRM & RM_MASK); RegisterIndex rm = (RegisterIndex)(modRM & RM_MASK);
string operand = DecodeModRM(mod, rm, is64Bit); string operand = DecodeModRM(mod, rm, is64Bit);
@ -179,14 +197,16 @@ public class ModRMDecoder
string sizePrefix = is64Bit ? "qword" : "dword"; string sizePrefix = is64Bit ? "qword" : "dword";
int position = _decoder.GetPosition(); int position = _decoder.GetPosition();
// Extract fields from SIB byte
byte scale = (byte)((sib & SIB_SCALE_MASK) >> 6); byte scale = (byte)((sib & SIB_SCALE_MASK) >> 6);
byte index = (byte)((sib & SIB_INDEX_MASK) >> 3); RegisterIndex index = (RegisterIndex)((sib & SIB_INDEX_MASK) >> 3);
byte @base = (byte)(sib & SIB_BASE_MASK); RegisterIndex @base = (RegisterIndex)(sib & SIB_BASE_MASK);
// Special case: no index register // Special case: ESP/SP (4) in index field means no index register
if (index == 4) if (index == RegisterIndex.Si)
{ {
if (@base == 5 && displacement == 0) // Special case: disp32 only // Special case: EBP/BP (5) in base field with no displacement means disp32 only
if (@base == RegisterIndex.Di && displacement == 0)
{ {
if (position + 4 <= _length) if (position + 4 <= _length)
{ {
@ -198,45 +218,56 @@ public class ModRMDecoder
} }
else else
{ {
string baseDispStr = ""; // Base register only
if (displacement != 0) // Only show displacement if it's not zero
if (displacement == 0)
{ {
baseDispStr = displacement < 0 ? return $"{sizePrefix} ptr [{GetRegisterName(@base, 32)}]";
$"-0x{-displacement:X}" :
$"+0x{displacement:X}";
} }
return $"{sizePrefix} ptr [{RegisterNames32[@base]}{baseDispStr}]";
string baseDispStr = displacement < 0 ?
$"-0x{-displacement:X}" :
$"+0x{displacement:X}";
return $"{sizePrefix} ptr [{GetRegisterName(@base, 32)}{baseDispStr}]";
} }
} }
// Normal case with index register // Normal case with base and index registers
int scaleFactor = 1 << scale; // 1, 2, 4, or 8 int scaleFactor = 1 << scale; // 1, 2, 4, or 8
// Only include the scale factor if it's not 1
string scaleStr = scaleFactor > 1 ? $"*{scaleFactor}" : ""; string scaleStr = scaleFactor > 1 ? $"*{scaleFactor}" : "";
string indexDispStr = ""; // Only show displacement if it's not zero
if (displacement != 0) if (displacement == 0)
{ {
indexDispStr = displacement < 0 ? return $"{sizePrefix} ptr [{GetRegisterName(@base, 32)}+{GetRegisterName(index, 32)}{scaleStr}]";
$"-0x{-displacement:X}" :
$"+0x{displacement:X}";
} }
return $"{sizePrefix} ptr [{RegisterNames32[@base]}+{RegisterNames32[index]}{scaleStr}{indexDispStr}]"; string indexDispStr = displacement < 0 ?
$"-0x{-displacement:X}" :
$"+0x{displacement:X}";
return $"{sizePrefix} ptr [{GetRegisterName(@base, 32)}+{GetRegisterName(index, 32)}{scaleStr}{indexDispStr}]";
} }
/// <summary> /// <summary>
/// Gets the register name based on the register index and size /// Gets the register name based on the register index and size
/// </summary> /// </summary>
/// <param name="index">The register index</param> /// <param name="regIndex">The register index as RegisterIndex enum</param>
/// <param name="size">The register size (8, 16, or 32 bits)</param> /// <param name="size">The register size (8, 16, or 32 bits)</param>
/// <returns>The register name</returns> /// <returns>The register name</returns>
public static string GetRegisterName(int index, int size) public static string GetRegisterName(RegisterIndex regIndex, int size)
{ {
// Convert RegisterIndex to raw index for array access
int index = (int)regIndex;
return size switch return size switch
{ {
8 => RegisterNames8[index], 8 => RegisterNames8[index],
16 => RegisterNames16[index], 16 => RegisterNames16[index],
_ => RegisterNames32[index] 32 => RegisterNames32[index],
_ => RegisterNames32[index] // Default to 32-bit registers
}; };
} }
} }

View File

@ -0,0 +1,33 @@
namespace X86Disassembler.X86;
/// <summary>
/// Represents the index values for x86 general-purpose registers.
/// These values correspond to the encoding used in ModR/M and SIB bytes
/// for register operand identification in x86 instructions.
/// </summary>
public enum RegisterIndex
{
/// <summary>A register (EAX/AX/AL depending on operand size)</summary>
A = 0,
/// <summary>B register (EBX/BX/BL depending on operand size)</summary>
B = 1,
/// <summary>C register (ECX/CX/CL depending on operand size)</summary>
C = 2,
/// <summary>D register (EDX/DX/DL depending on operand size)</summary>
D = 3,
/// <summary>Source Index register (ESI/SI)</summary>
Si = 4,
/// <summary>Destination Index register (EDI/DI)</summary>
Di = 5,
/// <summary>Stack Pointer register (ESP/SP)</summary>
Sp = 6,
/// <summary>Base Pointer register (EBP/BP)</summary>
Bp = 7,
}

View File

@ -24,7 +24,7 @@ public class CmpInstructionSequenceTests
// Assert // Assert
Assert.Single(instructions); Assert.Single(instructions);
Assert.Equal("cmp", instructions[0].Mnemonic); Assert.Equal("cmp", instructions[0].Mnemonic);
Assert.Equal("byte ptr [ebp+0x00], 0x03", instructions[0].Operands); Assert.Equal("byte ptr [ebp], 0x03", instructions[0].Operands);
} }
/// <summary> /// <summary>
@ -47,7 +47,7 @@ public class CmpInstructionSequenceTests
// First instruction: CMP BYTE PTR [EBP], 0x03 // First instruction: CMP BYTE PTR [EBP], 0x03
Assert.Equal("cmp", instructions[0].Mnemonic); Assert.Equal("cmp", instructions[0].Mnemonic);
Assert.Equal("byte ptr [ebp+0x00], 0x03", instructions[0].Operands); Assert.Equal("byte ptr [ebp], 0x03", instructions[0].Operands);
// Second instruction: JGE +5 // Second instruction: JGE +5
Assert.Equal("jge", instructions[1].Mnemonic); Assert.Equal("jge", instructions[1].Mnemonic);
@ -81,7 +81,7 @@ public class CmpInstructionSequenceTests
// First instruction: CMP BYTE PTR [EBP], 0x03 // First instruction: CMP BYTE PTR [EBP], 0x03
Assert.Equal("cmp", instructions[0].Mnemonic); Assert.Equal("cmp", instructions[0].Mnemonic);
Assert.Equal("byte ptr [ebp+0x00], 0x03", instructions[0].Operands); Assert.Equal("byte ptr [ebp], 0x03", instructions[0].Operands);
// Second instruction: JGE +5 // Second instruction: JGE +5
Assert.Equal("jge", instructions[1].Mnemonic); Assert.Equal("jge", instructions[1].Mnemonic);

View File

@ -105,10 +105,10 @@ RawBytes;Instructions
# XOR with different addressing modes # XOR with different addressing modes
# XOR [ebp+0x8], eax (opcode 0x31) # XOR [ebp+0x8], eax (opcode 0x31)
31458;[{ "Mnemonic": "xor", "Operands": "dword ptr [ebp+0x8], eax" }] 314508;[{ "Mnemonic": "xor", "Operands": "dword ptr [ebp+0x08], eax" }]
# XOR eax, [ebp+0x8] (opcode 0x33) # XOR eax, [ebp+0x8] (opcode 0x33)
33458;[{ "Mnemonic": "xor", "Operands": "eax, dword ptr [ebp+0x8]" }] 334508;[{ "Mnemonic": "xor", "Operands": "eax, dword ptr [ebp+0x08]" }]
# XOR with other segment overrides # XOR with other segment overrides
# XOR ss:[ebx+0x10], ecx (opcode 0x31 with SS override) # XOR ss:[ebx+0x10], ecx (opcode 0x31 with SS override)

Can't render this file because it contains an unexpected character in line 6 and column 9.