public string[] GetAssemblyCodeFromMemoryAccessVMCommand(string vmCommand)
        {
            string[] assemblyCodeEquivalentToVMCommand;

            string[] splitVMCommand = vmCommand.Split(' ');

            string stackCommand;

            string segment;

            string i;

            int iAsInt;

            if (splitVMCommand.Length == 3)
            {
                stackCommand = splitVMCommand[0];

                segment = splitVMCommand[1];

                i = splitVMCommand[2];

                if (SyntaxValidator.IsConvertableToInteger(i))
                {
                    iAsInt = Convert.ToInt32(i);

                    if (iAsInt > 65535)
                    {
                        throw new Exception("The value parameter cannot be larger than 65535");
                    }
                }
                else
                {
                    throw new Exception("An integer is expected as the last parameter");
                }
            }
            else
            {
                throw new Exception("Memory access command does not have the correct number of arguments");
            }

            assemblyCodeEquivalentToVMCommand = GetAssemblyCode(stackCommand, segment, i);

            return(assemblyCodeEquivalentToVMCommand);
        }
Beispiel #2
0
        public IEnumerable <string> GetAssemblyCommands(IEnumerable <string> vmCommands)
        {
            IEnumerable <string> nextAssemblyInstructions;

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

            int vmCommandLineNumber = 1;

            try
            {
                foreach (string vmCommand in vmCommands)
                {
                    if (SyntaxValidator.IsArithmeticVMCommand(vmCommand))
                    {
                        nextAssemblyInstructions = translator.GetAssemblyCodeFromArithmeticVMCommand(vmCommand);
                    }
                    else if (SyntaxValidator.IsMemoryAccessVMCommand(vmCommand))
                    {
                        nextAssemblyInstructions = translator.GetAssemblyCodeFromMemoryAccessVMCommand(vmCommand);
                    }
                    else
                    {
                        throw new Exception("Unrecognized virtual machine command");
                    }

                    assemblyCommands.AddRange(nextAssemblyInstructions);

                    vmCommandLineNumber++;
                }

                Console.WriteLine("VM code converted to assembly code...");

                return(assemblyCommands);
            }
            catch (Exception e)
            {
                string newErrorMessage =
                    "Error on line " + vmCommandLineNumber.ToString() + ": " + e.Message;

                throw new Exception(newErrorMessage);
            }
        }