CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University
1 Today ’ s Topic Beck ’ s Section 2.4: Assembler Design Options One-pass assemblers Multi-pass assemblers
2 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
3 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
4 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
5 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
6 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
7 Program for One-pass Assembler LineLoc Source statementObject code 01000COPYSTART EOFBYTEC’EOF’454F THREEWORD ZEROWORD RETADRRESW1 5100CLENGTHRESW1 6100FBUFFERRESB FFIRSTSTLRETADR CLOOPJSUBRDREC48203D LDALENGTH00100C COMPZERO BJEQENDFIL EJSUBWRREC JCLOOP3C ENDFILLDAEOF STABUFFER0C100F 55202ALDATHREE DSTALENGTH0C100C JSUBWRREC LDLRETADR RSUB4C0000 Figure 2.18
8 Figure 2.19(a): Before Line 40 Memory AddressContents F xxxxxx xxxxxxxx 1010xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx. 2000xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx C C2012. Address unknown yet
9 Figure 2.19(a): Before Line 40 SYMTAB: LENGTH100C RDREC* THREE1003 ZERO1006 WRREC* EOF1000 ENDFIL* RETADR1009 BUFFER100F CLOOP2012 FIRST200F... SymbolValue 2013 201F 201C
10 After Line 45 Memory AddressContents F xxxxxx xxxxxxxx 1010xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx. 2000xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx C C
11 After Line 45 SYMTAB: LENGTH100C RDREC* THREE1003 ZERO1006 WRREC* EOF1000 ENDFIL2024 RETADR1009 BUFFER100F CLOOP2012 FIRST200F... SymbolValue 2013 201F
SUB TO READ RECORD INTO BUFFER INPUTBYTEX’F1’F AMAXLENWORD DRDRECLDXZERO LDAZERO RLOOPTDINPUTE JEQRLOOP RDINPUT D CCOMPZERO FJEQEXIT30205B STCHBUFFER,X54900F TIXMAXLEN2C203A JLTRLOOP BEXITSTXLENGTH10100C ERSUB4C0000
13 Figure 2.19(b): After Line 160 Memory AddressContents F xxxxxx xxxxxxxx 1010xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx. 2000xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx D00100C C C 100F C100C C00 00F E DB F.
14 Figure 2.19(b): After Line 160 LENGTH100C RDREC203D THREE1003 ZERO1006 WRREC* EOF1000 ENDFIL2024 RETADR1009 BUFFER100F CLOOP2012 FIRST200F MAXLEN203A INPUT2039 EXIT* RLOOP F 2031 2050
SUB TO WRITE RECORD FROM BUFFER OUTPUTBYTEX’05’ WRRECLDXZERO WLOOPTDOUTPUTE JEQWLOOP BLDCHBUFFER,X50900F EWDOUTPUTDC TIXLENGTH2C100C JLTWLOOP RSUB4C ENDFIRST
16 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
17 Fig (Partial) H COPY A T F T 00200F C C2012 T 00201C T C100F C100C C0000 T D … E 00200F
18 Figure 2.20
19 Today ’ s Topic Beck ’ s Section 2.4: Assembler Design Options One-pass assemblers Multi-pass assemblers
20 Multi-Pass Assemblers Restriction on EQU and ORG No forward reference, since symbols’ value can’t be defined during the first pass Example: ALPHAEQU BETA BETAEQU DELTA DELTARESW 1 Assemblers with 2 passes cannot resolve
21 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
22 Figure 2.21(a): After Pass 1 1 HALFSZ EQU MAXLEN/2 2 MAXLEN EQU BUFEND-BUFFER 3 PREVBT EQU BUFFER BUFFER RESB BUFEND EQU * HALFSZ&1MAXLEN/2 MAXLEN* HALFSZ 1 symbol undefined
23 Figure 2.21(c): MAXLEN Defined 1 HALFSZ EQU MAXLEN/2 2 MAXLEN EQU BUFEND-BUFFER 3 PREVBT EQU BUFFER BUFFER RESB BUFEND EQU * BUFEND* HALFSZ&1MAXLEN/2 MAXLEN&2 BUFEND- BUFFER BUFFER* MAXLEN HALFSZ
24 Figure 2.21(d): PREVBT Defined 1 HALFSZ EQU MAXLEN/2 2 MAXLEN EQU BUFEND-BUFFER 3 PREVBT EQU BUFFER BUFFER RESB BUFEND EQU * BUFEND* HLFSZ&1MAXLEN/2 PREVBT&1BUFFER-1 MAXLEN&2 BUFEND- BUFFER BUFFER* MAXLEN PREVBT HALFSZ MAXLEN
25 Figure 2.21(e): After Line 4 1 HALFSZ EQU MAXLEN/2 2 MAXLEN EQU BUFEND-BUFFER 3 PREVBT EQU BUFFER BUFFER RESB BUFEND EQU * BUFEND* HLFSZ&1MAXLEN/2 PREVBT1033 MAXLEN&1 BUFEND- BUFFER BUFFER1034 MAXLEN Assume loc=1034 HALFSZ
26 Figure 2.21(f): After Line 5 1 HALFSZ EQU MAXLEN/2 2 MAXLEN EQU BUFEND-BUFFER 3 PREVBT EQU BUFFER BUFFER RESB BUFEND EQU * BUFEND2034 HLFSZ800 PREVBT1033 MAXLEN1000 BUFFER1034