コード例 #1
0
        public void Parse(OutputFormat format)
        {
            var converter = new CodeConverter();

            while (this.HasMoreCommands() && this.Advance())
            {
                if (format == OutputFormat.hex)
                {
                    this.output.Add(converter.InstructionAsHexString(this.CommandType()));
                }
                else
                {
                    this.output.Add(converter.InstructionAsBinaryString(this.CommandType()));
                }
                // TODO do any conversion we need to do on the operand -
                // the operand might be hex, decimal, or binary... convert it.
                this.output = this.output.Concat(this.Operands()).ToList();
            }
            logger.log("*********parsed output*********");
            logger.log(string.Join(Environment.NewLine, this.output));
        }
コード例 #2
0
        private IEnumerable <string> ConvertOpCodes(IEnumerable <string> code)
        {
            logger.log("PHASE: CONVERT OPCODES TO OUTPUT FORMAT");

            var parser    = new AssemblyParser(null, code, this.logger);
            var converter = new CodeConverter();

            while (parser.HasMoreCommands() && parser.Advance())
            {
                //dont do anything for labels or defines - we already made symbols for them
                //in the first pass.
                if (parser.CommandType() != CommandType.ASSEM_LABEL && parser.CommandType() != CommandType.ASSEM_DEFINE)
                {
                    //first convert the opcode.
                    parser.output.Add(converter.InstructionAsHexString(parser.CommandType()));

                    //then check if this opcode has symbols
                    if (parser.HasSymbols())
                    {
                        //get the symbol and lookup the memory address it points to -
                        //store this as the next line in the output string array.

                        // always assume a default offset of addding 0.
                        int symbolOffset = 0;
                        var symbol       = parser.Operands()[0];
                        if (parser.SymbolHasOffset())
                        {
                            symbolOffset = parser.symbolOffsetExpressionInfo().Item2;
                            symbol       = parser.symbolOffsetExpressionInfo().Item1;
                        }

                        if (this.symbolTable.ContainsKey(symbol))
                        {
                            parser.output.Add(converter.NumberAsHexString(this.symbolTable[symbol] + symbolOffset));
                        }

                        //new symbol store it.
                        //increment the symbolTable offset so variables are stored at next free space at offset 255+500 ( so first one is at 755)
                        //this means programs have a max length currently of 500 lines - and can store 1000 - 755 symbols -
                        //to increase this we just need to modify the bootloader - we have 64k address space to play with in the cpu.
                        //transfer time will just increase.
                        else
                        {
                            var symbolTableCurrentLocation = MemoryMap[MemoryMapKeys.symbols].AbsoluteStart + this.currentSymbolTableOffset;
                            if (symbolTableCurrentLocation > MemoryMap[MemoryMapKeys.symbols].AbsoluteEnd)
                            {
                                throw new Exception(" symbol table has more variables than allocated memory space for symbols");
                            }
                            logger.log($"adding symbol, {symbol} at line: {symbolTableCurrentLocation}");
                            this.symbolTable[symbol] = symbolTableCurrentLocation;
                            //increment the offset.
                            this.currentSymbolTableOffset = this.currentSymbolTableOffset + 1;
                            parser.output.Add(converter.NumberAsHexString(this.symbolTable[symbol]));
                        }
                    }
                    //if no symbols check if it has any operands
                    else if (parser.HasOperands())
                    {
                        var convertedOperandsToHex = parser.Operands().Select(x => converter.NumberAsHexString(int.Parse(x))).ToArray();
                        parser.output = parser.output.Concat(convertedOperandsToHex).ToList();
                    }
                }
            }
            return(parser.output);
        }