Hardware Reference
In-Depth Information
During pass one of the assembly, opcodes are looked up and macros expanded.
Whenever a macro definition is encountered, it is stored in the macro table. When
a macro is called, the assembler temporarily stops reading input from the input de-
vice and starts reading from the stored macro body instead. Formal parameters
extracted from the stored macro body are replaced by the actual parameters pro-
vided in the call. The presence of an ampersand in front of the formal parameters
makes it easy for the assembler to recognize them.
7.3 THE ASSEMBLY PROCESS
In the following sections we will briefly describe how an assembler works.
Although each machine has a different assembly language, the assembly process is
sufficiently similar on different machines that it is possible to describe it in general.
7.3.1 Two-Pass Assemblers
Because an assembly language program consists of a series of one-line state-
ments, it might at first seem natural to have an assembler that read one statement,
then translated it to machine language, and finally output the generated machine
language onto a file, along with the corresponding piece of the listing, if any, onto
another file. This process would then be repeated until the whole program had
been translated. Unfortunately, this strategy does not work.
Consider the situation where the first statement is a branch to L . The assem-
bler cannot assemble this statement until it knows the address of statement L .
Statement L may be near the end of the program, making it impossible for the
assembler to find the address without first reading almost the entire program. This
difficulty is called the forward reference problem , because a symbol, L , has been
used before it has been defined; that is, a reference has been made to a symbol
whose definition will only occur later.
Forward references can be handled in two ways. First, the assembler may in
fact read the source program twice. Each reading of the source program is called a
pass ; any translator that reads the input program twice is called a two-pass trans-
lator . On pass one, the definitions of symbols, including statement labels, are col-
lected and stored in a table. By the time the second pass begins, the values of all
symbols are known; thus no forward reference remains and each statement can be
read, assembled, and output. Although this approach requires an extra pass over
the input, it is conceptually simple.
The second approach consists of reading the assembly program once, convert-
ing it to an intermediate form, and storing this intermediate form in a table in
memory. Then a second pass is made over the table instead of over the source pro-
gram. If there is enough memory (or virtual memory), this approach saves I/O
time. If a listing is to be produced, then the entire source statement, including all
 
 
 
Search WWH ::




Custom Search