Esempio n. 1
0
        public string Disassemble(string operationString)
        {
            var operation = SplitOperationString(operationString);

            var instruction = LanguageDefinition.Instructions.Find(i => {
                return(i.TextDisplay.ToLower() == operation[0].ToLower());
            });

            if (instruction == null)
            {
                throw new Exception("The operation supplied does not match any known instruction definition.");
            }

            var binary = new BinaryString(instruction.Binary);

            var syntaxDefinition = new string[0];
            var isMatch          = false;

            foreach (var syntax in instruction.Syntax)
            {
                syntaxDefinition = SplitOperationString(syntax);
                if (operation.Length == syntaxDefinition.Length)
                {
                    isMatch = true;
                    break;
                }
            }

            if (!isMatch)
            {
                throw new Exception($"The operation does not match any known syntax definition for instruction {instruction.TextDisplay}");
            }

            var bitIndex = 0;

            foreach (var arg in instruction.Args)
            {
                var definitionIndex = Array.FindIndex(syntaxDefinition, def => def == arg.Value);

                if (definitionIndex != -1)
                {
                    var argBinary = GetArgByValue(arg.Value, operation[definitionIndex]);

                    if (argBinary != null)
                    {
                        binary.Insert(bitIndex, argBinary.Value);
                    }
                }
                bitIndex += arg.Size;
            }

            return(binary.ToHexString().Value);
        }