From efd9141b3912c839d1127f550fbb5296b73cb303 Mon Sep 17 00:00:00 2001 From: bird_egop Date: Sun, 13 Apr 2025 02:20:49 +0300 Subject: [PATCH] Made StringInstructionHandler self-contained by removing dependency on OpcodeMap --- .../String/StringInstructionHandler.cs | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/X86Disassembler/X86/Handlers/String/StringInstructionHandler.cs b/X86Disassembler/X86/Handlers/String/StringInstructionHandler.cs index a943d35..a7b529b 100644 --- a/X86Disassembler/X86/Handlers/String/StringInstructionHandler.cs +++ b/X86Disassembler/X86/Handlers/String/StringInstructionHandler.cs @@ -5,6 +5,19 @@ namespace X86Disassembler.X86.Handlers.String; /// public class StringInstructionHandler : InstructionHandler { + // Dictionary mapping opcodes to their mnemonics + private static readonly Dictionary _mnemonics = new Dictionary + { + { 0xA4, "movs" }, // MOVSB + { 0xA5, "movs" }, // MOVSD + { 0xAA, "stos" }, // STOSB + { 0xAB, "stos" }, // STOSD + { 0xAC, "lods" }, // LODSB + { 0xAD, "lods" }, // LODSD + { 0xAE, "scas" }, // SCASB + { 0xAF, "scas" } // SCASD + }; + /// /// Initializes a new instance of the StringInstructionHandler class /// @@ -24,10 +37,7 @@ public class StringInstructionHandler : InstructionHandler public override bool CanHandle(byte opcode) { // Check if the opcode is a string instruction - return opcode == 0xA4 || opcode == 0xA5 || // MOVS - opcode == 0xAA || opcode == 0xAB || // STOS - opcode == 0xAC || opcode == 0xAD || // LODS - opcode == 0xAE || opcode == 0xAF; // SCAS + return _mnemonics.ContainsKey(opcode); } /// @@ -39,7 +49,15 @@ public class StringInstructionHandler : InstructionHandler public override bool Decode(byte opcode, Instruction instruction) { // Set the mnemonic - instruction.Mnemonic = OpcodeMap.GetMnemonic(opcode); + if (_mnemonics.TryGetValue(opcode, out string? mnemonic)) + { + instruction.Mnemonic = mnemonic; + } + else + { + // This shouldn't happen if CanHandle is called first + return false; + } // Set the operands based on the string operation switch (opcode)