[go: up one dir, main page]

100% found this document useful (1 vote)
18K views27 pages

Assembler Design Options: Department of Computer Science National Tsing Hua University

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 27

CS2422 Assembly Language and System Programming

Assembler Design Options

Department of Computer Science


National Tsing Hua University
Today’s Topic
 Beck’s Section 2.4: Assembler Design Options
 One-pass assemblers
 Multi-pass assemblers

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

1000 454F4600 00030000 00xxxxxx xxxxxxxx


1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
.
.
2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx14
2010 100948-- --00100C 28100630 ----48--
2020 --3C2012
.
.
Address unknown yet
8
Figure 2.19(a): Before Line 40
 SYMTAB: Symbol Value
LENGTH 100C
RDREC * 2013 
THREE 1003
ZERO 1006
WRREC * 201F 
EOF 1000
ENDFIL * 201C 
RETADR 1009
BUFFER 100F
CLOOP 2012
FIRST 200F
... ... 9
After Line 45
Memory
Address Contents

1000 454F4600 00030000 00xxxxxx xxxxxxxx


1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
.
.
2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx14
2010 100948-- --00100C 28100630 202448--
2020 --3C2012 001000
.
.

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

1000 454F4600 00030000 00xxxxxx xxxxxxxx


1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
.
2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx14
2010 10094820 3D00100C 28100630 202448--
2020 --3C2012 0010000C 100F0010 030C100C
2030 48----08 10094C00 00F10010 00041006
2040 001006E0 20393020 43DB2039 28100630
2050 ----5490 0F
.

13
LENGTH 100C

Figure 2.19(b): RDREC 203D

After Line 160 THREE 1003


ZERO 1006
WRREC * 201F
EOF 1000
ENDFIL 2024
2031 
RETADR 1009
BUFFER 100F
CLOOP 2012
FIRST 200F
MAXLEN 203A
INPUT 2039
EXIT * 2050 
RLOOP 2043
14
... ...
195 .
200 . SUB TO WRITE RECORD FROM BUFFER
205 .
206 2061 OUTPUT BYTE X’05’ 05
207 .
210 2062 WRREC LDX ZERO 041006
215 2065 WLOOP TD OUTPUT E02061
220 2068 JEQ WLOOP 302065
225 206B LDCH BUFFER,X 50900F
230 206E WD OUTPUT DC2061
235 2071 TIX LENGTH 2C100C
240 2074 JLT WLOOP 382065
245 2077 RSUB 4C0000
255 END FIRST

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)

H COPY 001000 00107A


T 001000 09 454F46 000003 000000
T 00200F 15 141009 480000 00100C 281006 300000 480000 3C2012
T 00201C 02 2024
T 002024 19 001000 0C100F 001003 0C100C 480000 081009 4C0000
T 002013 02 203D

E 00200F

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 HALFSZ EQU MAXLEN/2


2 MAXLEN EQU BUFEND-BUFFER
3 PREVBT EQU BUFFER-1 HALFSZ &1 MAXLEN/2 
...
4 BUFFER RESB 4096
5 BUFEND EQU * MAXLEN * HALFSZ 

1 symbol undefined

22
Figure 2.21(c): MAXLEN Defined

1 HALFSZ EQU MAXLEN/2


BUFEND * MAXLEN 
2 MAXLEN EQU BUFEND-BUFFER
3 PREVBT EQU BUFFER-1 HALFSZ &1 MAXLEN/2 
...
4 BUFFER RESB 4096
5 BUFEND EQU * MAXLEN &2 BUFEND-
BUFFER HALFSZ 

BUFFER * MAXLEN 

23
Figure 2.21(d): PREVBT Defined

1 HALFSZ EQU MAXLEN/2


BUFEND * MAXLEN 
2 MAXLEN EQU BUFEND-BUFFER
3 PREVBT EQU BUFFER-1 HLFSZ &1 MAXLEN/2 
...
PREVBT &1 BUFFER-1 
4 BUFFER RESB 4096
5 BUFEND EQU * MAXLEN &2 BUFEND-
BUFFER HALFSZ 

BUFFER * MAXLEN

PREVBT 
24
Figure 2.21(e): After Line 4

1 HALFSZ EQU MAXLEN/2


BUFEND * MAXLEN 
2 MAXLEN EQU BUFEND-BUFFER
3 PREVBT EQU BUFFER-1 HLFSZ &1 MAXLEN/2 
...
PREVBT 1033 
4 BUFFER RESB 4096
5 BUFEND EQU * MAXLEN &1 BUFEND-
BUFFER HALFSZ 

BUFFER 1034 
Assume
loc=1034

25
Figure 2.21(f): After Line 5

1 HALFSZ EQU MAXLEN/2


BUFEND 2034 
2 MAXLEN EQU BUFEND-BUFFER
3 PREVBT EQU BUFFER-1 HLFSZ 800 

...
PREVBT 1033 
4 BUFFER RESB 4096
5 BUFEND EQU * MAXLEN 1000 

BUFFER 1034 

26

You might also like