Exemplo n.º 1
0
        public string[] SubstituteSymbolsWithValues(string[] assemblyInstructions)
        {
            List <string> assemblyInstructionsWithoutSymbols = new List <string>();

            foreach (string instruction in assemblyInstructions)
            {
                if (!SyntaxValidator.IsLabel(instruction))
                {
                    if (SyntaxValidator.IsAddressInstructionWithSymbol(instruction))
                    {
                        string addressSymbol = instruction.Remove(0, 1);

                        if (!symbolTable.Contains(addressSymbol))
                        {
                            symbolTable.AddVariable(addressSymbol);
                        }

                        assemblyInstructionsWithoutSymbols.Add("@" + symbolTable.GetValue(addressSymbol));
                    }
                    else
                    {
                        assemblyInstructionsWithoutSymbols.Add(instruction);
                    }
                }
            }

            return(assemblyInstructionsWithoutSymbols.ToArray());
        }
Exemplo n.º 2
0
        static public string[] GetMachineCodeArray(string[] assemblyInstructions)
        {
            int lineNumber = 0;

            try
            {
                SymbolHandler symbolHandler = new SymbolHandler(assemblyInstructions);

                Console.WriteLine("Labels saved to symbol table...");

                assemblyInstructions = symbolHandler.SubstituteSymbolsWithValues(assemblyInstructions);

                Console.WriteLine("Symbol to value substitution complete...");

                List <string> machineCodeInstructionArray = new List <string>();

                Instruction newInstruction;

                string newInstructionAsMachineCode;

                foreach (string assemblyInstruction in assemblyInstructions)
                {
                    if (!SyntaxValidator.IsLabel(assemblyInstruction))
                    {
                        newInstruction = GetInstructionFromAssemblyCommand(assemblyInstruction);

                        newInstructionAsMachineCode = newInstruction.GetInstructionAsMachineCode();

                        machineCodeInstructionArray.Add(newInstructionAsMachineCode);

                        lineNumber++;
                    }
                }

                Console.WriteLine("Assembly language converted to machine code...");

                return(machineCodeInstructionArray.ToArray());
            }
            catch (Exception e)
            {
                throw new Exception("Error on line " + lineNumber.ToString() + ": " + e.Message);
            }
        }
Exemplo n.º 3
0
        static private Instruction TryGetInstructionFromAssemblyCommand(string assemblyCommand)
        {
            Instruction instruction;

            if (SyntaxValidator.IsAddressInstruction(assemblyCommand))
            {
                instruction = GetAddressInstruction(assemblyCommand);
            }
            else if (SyntaxValidator.IsComputationInstruction(assemblyCommand))
            {
                instruction = GetComputationInstruction(assemblyCommand);
            }
            else
            {
                throw new Exception("Parser.GetInstructionFromAssemblyCommand - Instruction is not valid");
            }

            return(instruction);
        }
Exemplo n.º 4
0
        public SymbolHandler(string[] assemblyInstructions)
        {
            Dictionary <string, int> labelDictionary = new Dictionary <string, int>();

            string label;

            int instructionLineNumber = 0;

            foreach (string instruction in assemblyInstructions)
            {
                if (SyntaxValidator.IsLabel(instruction))
                {
                    label = instruction.Trim('(', ')');

                    labelDictionary.Add(label, instructionLineNumber);
                }
                else
                {
                    instructionLineNumber++;
                }
            }

            symbolTable = new SymbolTable(labelDictionary);
        }