6.4.符号的处理
\(6.4.\)Handling the Symbol
1.Type of symbol
Generally there are three types of symbol:
- Variable symbols: Which represents memory locations where the programmer wants to maintain values.
- Label symbols: Which represents destinations of goto instructions.
- Pre-defined symbols: Which represents special memory lorations.
2.Pre-define symbols translation
- These symbols exist only in the context of A
instructions. So they will only be seen in expression like
@preDefinedSymbol
- Translating
@preDefinedSymbol
: ReplacepreDefineSymbol
with its corresponding value:
3.Label symbols translation
To begin with, there are two sorts of label:
- Used to label destination of
goto
commands. For example,@STOP, 0;JMP
- Declared by the pseudo-command. For example,
(STOP)
This is called pseudo-commands because it doesn't generate any code. When we translate the program into binary. we don't translate the label declaration instructions.
To allocate a specific location to each label, we number the instruction one by one, like the left hand side:
- We ignore white space and pseudo-commands when numbering.
Once we complete the numbering, we can refer the label to
its corresponding line number. For example, LOOP
relates to 4 and STOP
relates to 18. And when we see the
instruction @LOOP
, we actually mean @4
.
After this pre-work, the translating is plain: * Translating
@labelSymbol
: Replace labelSymbol
with its
value.
4.Varables translation
- Any symbol xx appearing in an assembly program which is not pre-defined and is not defined elsewhere using the (xxx) directive is treated as a variable
- if a variable appears in the program for the first time, we allocate it to a memory address starting from address 16 . If we see this varable popping up later in the program, we simply look up the value that you assigned to it before.
5.Symbol table construction
\(a.\)First pass
In the first pass, we extract from the program all the label symbols.
Before starting any translation, create an empty table and add all the predefined symbos one by one.
March through the entire text file that constitutes the source assembly program:
Only look for label declarations
- Line of code that begins with left parenthesis.
Meanwhile keep track of how many lines you've read so far (only count real instructions).
\(b.\)Second pass
- We start once again to scan the entire program.
- Whenever we see a symbol which doesn't appear in the symbol
table, we add it to the table, assign to it
16
or larger number (for it must be a varable). - If we find them in the symbol table, then they've been declared and we can use them.
\(c.\)More about the table
- When resolving a symbol, we look it up in the table, retrieve its value, and put it into the instruction. What you get is the meaning of this symbol according to the symbol table.
- We maintain the table as long as the assembler is processing the program, and then we can toss it away.
6.Summary: The assembly process
Initialization:
- Construct an empty symbol table,
- Add the pre-defined symbols to the symbol table,
First pass:
Scan the entire program;
For each "instruction" of the form
(xxx)
:- Add the pair (xxx, address) to the symbol table, where
address is the number of the instruction following
(xxx)
.
- Add the pair (xxx, address) to the symbol table, where
address is the number of the instruction following
Second pass:
Set n to 16
Scan the entire program again, for each instruction:
If the instruction is
@symbol
, look up symbol in the symbol table:If (symbol, value) is found, use value to complete the instruction's translation;
If not found:
- Add (symbol, n) to the symbol table,
- Use n to complete the instruction's translation,
- n++
If the instruction is a C-instruction, complete the instruction's translation.
Write the translated instruction to the output file.