Assembler Design Options: Department of Computer Science National Tsing Hua University
Assembler Design Options: Department of Computer Science National Tsing Hua University
Assembler Design Options: Department of Computer Science National Tsing Hua University
1
One-Pass Assemblers (1/2)
Main problem
Forward references
‒ Data items
‒ Labels on instructions
Solution
Data items: require all such areas be defined
before they are referenced
Labels on instructions: no good solution
2
One-Pass Assemblers (2/2)
Two types of one-pass assembler
Type 1: Load-and-go
‒ Produces object code directly in memory for
immediate execution
Type 2:
‒ Produces usual kind of object code for later
execution
3
Load-and-go Assembler (1/3)
Characteristics
Useful for program development and testing
Avoids the overhead of writing the object program
out and reading it back
Both one-pass and two-pass assemblers can be
designed as load-and-go
However one-pass also avoids the overhead of an
additional pass over the source program
For a load-and-go assembler, the actual address
must be known at assembly time, we can use an
absolute program
4
Load-and-go Assembler (2/3)
Assembler operations:
1. Omit address translation for any undefined
symbol
2. Insert the symbol into SYMTAB, mark it undefined
3. The address that refers to the undefined symbol is
added to a list of forward references associated
with the symbol table entry
4. When the definition for a symbol is encountered,
the proper address for the symbol is then inserted
into any instructions previously generated
according to the forward reference list
5
Load-and-go Assembler (3/3)
At the end of the program
Any SYMTAB entries that are still marked with *
indicate undefined symbols
Search SYMTAB for the symbol named in the
END statement and jump to this location to begin
execution
The actual starting address must be specified at
assembly time
6
Program for One-pass Assembler
Line Loc Source statement Object code
0 1000 COPY START 1000
1 1000 EOF BYTE C’EOF’ 454F46
2 1003 THREE WORD 3 000003
3 1006 ZERO WORD 0 000000
4 1009 RETADR RESW 1
5 100C LENGTH RESW 1 Figure 2.18
6 100F BUFFER RESB 4096
10 200F FIRST STL RETADR 141009
15 2012 CLOOP JSUB RDREC 48203D
20 2015 LDA LENGTH 00100C
25 2018 COMP ZERO 281006
30 201B JEQ ENDFIL 302024
35 201E JSUB WRREC 482062
40 2021 J CLOOP 3C2012
45 2024 ENDFIL LDA EOF 001000
50 2027 STA BUFFER 0C100F
55 202A LDA THREE 001003
60 202D STA LENGTH 0C100C
65 2030 JSUB WRREC 482062
70 2033 LDL RETADR 081009
7
75 2036 RSUB 4C0000
Figure 2.19(a): Before Line 40
Memory
Address Contents
10
After Line 45
SYMTAB: Symbol Value
LENGTH 100C
RDREC * 2013
THREE 1003
ZERO 1006
WRREC * 201F
EOF 1000
ENDFIL 2024
RETADR 1009
BUFFER 100F
CLOOP 2012
FIRST 200F
... ... 11
110 .
115 . SUB TO READ RECORD INTO BUFFER
120 .
121 2039 INPUT BYTE X’F1’ F1
122 203A MAXLEN WORD 4096 001000
124 .
125 203D RDREC LDX ZERO 041006
130 2040 LDA ZERO 001009
135 2043 RLOOP TD INPUT E02039
140 2046 JEQ RLOOP 302043
145 2049 RD INPUT D82039
150 204C COMP ZERO 281006
155 204F JEQ EXIT 30205B
160 2052 STCH BUFFER,X 54900F
165 2055 TIX MAXLEN 2C203A
170 2058 JLT RLOOP 382043
175 205B EXIT STX LENGTH 10100C
180 205E RSUB 4C0000
12
Figure 2.19(b): After Line 160
Memory
Address Contents
13
LENGTH 100C
15
Type 2 Assembler
Will produce object code
Assembler operations:
Forward references are entered into list as before
Instruction referencing are written into object file
as a Text record, even with incorrect addresses
When definition of a symbol is encountered,
assembler must generate another Text record with
the correct operand address
Loader is used to insert correct addresses into
instructions with forward references that could not
be handled by the assembler
Object program records must be kept in original
order when they are presented to the loader
16
Fig. 2.20 (Partial)
17
Figure 2.20
18
Today’s Topic
Beck’s Section 2.4: Assembler Design Options
One-pass assemblers
Multi-pass assemblers
19
Multi-Pass Assemblers
Restriction on EQU and ORG
No forward reference, since symbols’ value can’t
be defined during the first pass
Example:
ALPHA EQU BETA
BETA EQU DELTA
DELTA RESW 1
Assemblers with 2 passes cannot resolve
20
Multi-Pass Assemblers
Resolve forward references with as many passes
as needed
Portions that involve forward references in symbol
definition are saved during Pass 1
Additional passes through stored definitions
Finally a normal Pass 2
Example implementation:
Use link lists to keep track of whose value depend
on an undefined symbol
21
Figure 2.21(a): After Pass 1
1 symbol undefined
22
Figure 2.21(c): MAXLEN Defined
BUFFER * MAXLEN
23
Figure 2.21(d): PREVBT Defined
BUFFER * MAXLEN
PREVBT
24
Figure 2.21(e): After Line 4
BUFFER 1034
Assume
loc=1034
25
Figure 2.21(f): After Line 5
...
PREVBT 1033
4 BUFFER RESB 4096
5 BUFEND EQU * MAXLEN 1000
BUFFER 1034
26