Beispiel #1
0
        /// <summary>
        /// Reads a line in a .text segment of a program, and adds any found symbols to the
        /// symbol table.
        /// </summary>
        /// <param name="asmLine">The line of assembly code to parse.</param>
        /// <param name="symbolList">The list of symbols that will be added to.</param>
        /// <param name="alignment">Unused. Alignment is always on word boundaries in the text segment.</param>
        public void ParseSymbolsInLine(LineData asmLine, SymbolTable symbolList, int alignment)
        {
            string[] tokens = asmLine.Text.Split(' ');
            // a label should end with a ':' character.
            // this is OK if there's trash and no real assembly at this point,
            // as the second pass code generator will flag it.
            // we're just here to get symbols and addresses.
            if (ParserCommon.ContainsLabel(tokens[0]))
            {
                string labelName = ParserCommon.ExtractLabel(tokens[0]);
                var    label     = new Symbol(labelName, SegmentType.Text, m_CurrTextAddress);
                label.Size = sizeof(int);
                symbolList.AddSymbol(label);

                // determine if there are any instructions on this line.
                string[] subTokens = tokens[0].Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                // if we have more than one subtoken, then there is more than just a label on this line.
                // increment the number of words in the segment (since we're assuming whatever is on the right-hand side
                // is an instruction) by however many bytes the instruction is
                if (subTokens.Length > 1)
                {
                    ParseUnlabeledLine(asmLine);
                }
            }

            // if this doesn't have a label, and is not empty or a comment,
            // then this is an instruction. increment the counter.
            else
            {
                ParseUnlabeledLine(asmLine);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Parses a labeled line for symbols, and calculates the appropriate address of the element (if any).
        /// </summary>
        /// <param name="symTable">The symbol table to add the label to.</param>
        /// <param name="originalLine">The line data being parsed.</param>
        /// <param name="tokens">The string array of space-separated tokens.</param>
        /// <param name="alignment">The current alignment</param>
        private void ParseLabeledLine(SymbolTable symTable, LineData originalLine, string[] tokens, int alignment)
        {
            // if we're trying to figure out the backing data size of a label still (e.g.
            // label was declared, then the data declaration followed a few newlines later),
            // we shouldn't be seeing another label.
            if (m_UnresolvedSym != null)
            {
                throw new AssemblyException(originalLine.LineNum, "Expected data declaration after label.");
            }
            string labelName = ParserCommon.ExtractLabel(tokens[0]);
            var    label     = new Symbol(labelName, SegmentType.Data, m_CurrDataAddress);

            symTable.AddSymbol(label);
            m_UnresolvedSym = label;

            // if we couldn't find any following data elements, list this symbol as unresolved.
            ParseUnlabeledLine(originalLine, tokens, alignment);
        }