cs2422 assembly language and system programming assembler design options department of computer...

27
CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

Upload: amice-henderson

Post on 04-Jan-2016

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

CS2422 Assembly Language and System Programming

Assembler Design Options

Department of Computer ScienceNational Tsing Hua University

Page 2: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

2

Today’s Topic

Beck’s Section 2.4: Assembler Design Options One-pass assemblers Multi-pass assemblers

Page 3: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

3

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

Page 4: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

4

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

Page 5: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

5

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

Page 6: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

6

Load-and-go Assembler (2/3)

Assembler operations:1. Omit address translation for any undefined

symbol2. Insert the symbol into SYMTAB, mark it undefined3. 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

Page 7: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

7

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

Page 8: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

8

Program for One-pass AssemblerLine Loc Source statement Object code

0 1000 COPY START 10001 1000 EOF BYTE C’EOF’ 454F462 1003 THREE WORD 3 0000033 1006 ZERO WORD 0 0000004 1009 RETADR RESW 15 100C LENGTH RESW 16 100F BUFFER RESB 409610 200F FIRST STL RETADR 14100915 2012 CLOOP JSUB RDREC 48203D20 2015 LDA LENGTH 00100C25 2018 COMP ZERO 28100630 201B JEQ ENDFIL 30202435 201E JSUB WRREC 48206240 2021 J CLOOP 3C201245 2024 ENDFIL LDA EOF 00100050 2027 STA BUFFER 0C100F55 202A LDA THREE 00100360 202D STA LENGTH 0C100C65 2030 JSUB WRREC 48206270 2033 LDL RETADR 08100975 2036 RSUB 4C0000

Figure 2.18

Page 9: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

9

Figure 2.19(a): Before Line 40

MemoryAddress Contents

1000 454F4600 00030000 00xxxxxx xxxxxxxx1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx . .2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx142010 100948-- --00100C 28100630 ----48--2020 --3C2012 . .

Address unknown yet

Page 10: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

10

Figure 2.19(a): Before Line 40

SYMTAB:LENGTH 100CRDREC *

THREE 1003ZERO 1006WRREC *

EOF 1000ENDFIL *

RETADR 1009BUFFER 100FCLOOP 2012FIRST 200F... ...

Symbol Value

2013

201F

201C

Page 11: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

11

After Line 45

MemoryAddress Contents

1000 454F4600 00030000 00xxxxxx xxxxxxxx1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx . .2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx142010 100948-- --00100C 28100630 202448--2020 --3C2012 001000 . .

Page 12: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

12

After Line 45

SYMTAB:LENGTH 100CRDREC *

THREE 1003ZERO 1006WRREC *

EOF 1000ENDFIL 2024RETADR 1009BUFFER 100FCLOOP 2012FIRST 200F... ...

Symbol Value

2013

201F

Page 13: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

13

110 . 115 . SUB TO READ RECORD INTO BUFFER120 . 121 2039 INPUT BYTE X’F1’ F1122 203A MAXLEN WORD 4096 001000124 .125 203D RDREC LDX ZERO 041006130 2040 LDA ZERO 001009135 2043 RLOOP TD INPUT E02039140 2046 JEQ RLOOP 302043145 2049 RD INPUT D82039150 204C COMP ZERO 281006155 204F JEQ EXIT 30205B160 2052 STCH BUFFER,X 54900F165 2055 TIX MAXLEN 2C203A170 2058 JLT RLOOP 382043175 205B EXIT STX LENGTH 10100C180 205E RSUB 4C0000

Page 14: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

14

Figure 2.19(b): After Line 160

MemoryAddress Contents

1000 454F4600 00030000 00xxxxxx xxxxxxxx1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx .2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx142010 10094820 3D00100C 28100630 202448--2020 --3C2012 0010000C 100F0010 030C100C2030 48----08 10094C00 00F10010 000410062040 001006E0 20393020 43DB2039 281006302050 ----5490 0F .

Page 15: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

15

Figure 2.19(b): After

Line 160

LENGTH 100C

RDREC 203D

THREE 1003

ZERO 1006

WRREC *

EOF 1000

ENDFIL 2024

RETADR 1009

BUFFER 100F

CLOOP 2012

FIRST 200F

MAXLEN 203A

INPUT 2039

EXIT *

RLOOP 2043

... ...

201F

2031

2050

Page 16: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

16

195 .200 . SUB TO WRITE RECORD FROM BUFFER 205 .206 2061 OUTPUT BYTE X’05’ 05207 . 210 2062 WRREC LDX ZERO 041006215 2065 WLOOP TD OUTPUT E02061220 2068 JEQ WLOOP 302065 225 206B LDCH BUFFER,X 50900F230 206E WD OUTPUT DC2061235 2071 TIX LENGTH 2C100C240 2074 JLT WLOOP 382065 245 2077 RSUB 4C0000 255 END FIRST

Page 17: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

17

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

Page 18: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

18

Fig. 2.20 (Partial)

H COPY 001000 00107AT 001000 09 454F46 000003 000000T 00200F 15 141009 480000 00100C 281006 300000 480000 3C2012T 00201C 02 2024T 002024 19 001000 0C100F 001003 0C100C 480000 081009 4C0000T 002013 02 203D…E 00200F

Page 19: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

19

Figure 2.20

Page 20: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

20

Today’s Topic

Beck’s Section 2.4: Assembler Design Options One-pass assemblers Multi-pass assemblers

Page 21: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

21

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 BETABETA EQU DELTADELTA RESW 1 Assemblers with 2 passes cannot resolve

Page 22: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

22

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

Page 23: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

23

Figure 2.21(a): After Pass 1

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

HALFSZ &1 MAXLEN/2

MAXLEN *HALFSZ

1 symbol undefined

Page 24: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

24

Figure 2.21(c): MAXLEN Defined

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND *

HALFSZ &1 MAXLEN/2

MAXLEN &2 BUFEND-BUFFER

BUFFER *MAXLEN

MAXLEN

HALFSZ

Page 25: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

25

Figure 2.21(d): PREVBT Defined

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND *

HLFSZ &1 MAXLEN/2

PREVBT &1 BUFFER-1

MAXLEN &2 BUFEND-BUFFER

BUFFER *

MAXLEN

PREVBT

HALFSZ

MAXLEN

Page 26: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

26

Figure 2.21(e): After Line 4

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND *

HLFSZ &1 MAXLEN/2

PREVBT 1033

MAXLEN &1 BUFEND-BUFFER

BUFFER 1034

MAXLEN

Assume loc=1034

HALFSZ

Page 27: CS2422 Assembly Language and System Programming Assembler Design Options Department of Computer Science National Tsing Hua University

27

Figure 2.21(f): After Line 5

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND 2034

HLFSZ 800

PREVBT 1033

MAXLEN 1000

BUFFER 1034