예제 #1
0
        public Instruction[] Assemble(string assemblyCode)
        {
            _symbolTable.Clear();
            _bytes.Clear();
            _currentLineNumber = 0;

            foreach (string line in GetLines(assemblyCode))
            {
                _currentLineNumber += 1;

                InstructionSyntax instructionSyntax = InstructionSyntax.Parse(line, _currentLineNumber);

                if (!string.IsNullOrWhiteSpace(instructionSyntax.Label))
                {
                    AddLabelToSymbolTable(instructionSyntax.Label);
                }

                if (!string.IsNullOrWhiteSpace(instructionSyntax.Mnemonic))
                {
                    AssembleLine(instructionSyntax);
                }
            }

            Instruction[] instructions = _bytes.ToInstructions(_symbolTable);

            return(instructions);
        }
예제 #2
0
        private void AssembleLine(InstructionSyntax instructionSyntax)
        {
            switch (instructionSyntax.Mnemonic.ToLowerInvariant())
            {
            case "load":
                Load(instructionSyntax.Operands);
                break;

            case "store":
                Store(instructionSyntax.Operands);
                break;

            case "move":
                Move(instructionSyntax.Operands);
                break;

            case "addi":
                Addi(instructionSyntax.Operands);
                break;

            case "addf":
                Addf(instructionSyntax.Operands);
                break;

            case "jmpeq":
                JmpEQ(instructionSyntax.Operands);
                break;

            case "jmple":
                JmpLE(instructionSyntax.Operands);
                break;

            case "jmp":
                Jmp(instructionSyntax.Operands);
                break;

            case "and":
                And(instructionSyntax.Operands);
                break;

            case "or":
                Or(instructionSyntax.Operands);
                break;

            case "xor":
                Xor(instructionSyntax.Operands);
                break;

            case "ror":
                Ror(instructionSyntax.Operands);
                break;

            case "db":
                DataByte(instructionSyntax.Operands);
                break;

            case "org":
                Org(instructionSyntax.Operands);
                break;

            case "halt":
                Halt(instructionSyntax.Operands);
                break;

            default:
                throw new UnrecognizedMnemonicException(_currentLineNumber);
            }
        }