Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s Topic Assembler: Basic Functions

Similar presentations


Presentation on theme: "Today’s Topic Assembler: Basic Functions"— Presentation transcript:

0 Department of Computer Science National Tsing Hua University
Assembler Basics Department of Computer Science National Tsing Hua University

1 Today’s Topic Assembler: Basic Functions
Section 2.1 of Beck’s “System Software” book. Reading Assignment: pages

2 Role of Assembler Object Source Assembler Code Linker Program
Executable Code Loader

3 Example Program Fig. 2.1 (1/4)
Purpose Reads records from input device (code F1) and store in BUFFER Copies them to output device (code 05) At the end of the file, writes an extra EOF on the output device, then RSUB to the operating system Data transfer (RD, WD) End of each record is marked with a null character End of the file is indicated by a zero-length record Subroutines (JSUB, RSUB) RDREC, WRREC Save link register first before nested jump

4 Example Program Fig. 2.1 (2/4)
5 COPY START 1000 LOAD PROG AT LOC 1000 10 FIRST STL RETADR SAVE RETURN ADDRESS 15 CLOOP JSUB RDREC READ INPUT RECORD 20 LDA LENGTH TEST FOR EOF (LENGTH = 0) 25 COMP ZERO 30 JEQ ENDFIL EXIT IF EOF FOUND 35 JSUB WRREC WRITE OUTPUT RECORD 40 J CLOOP LOOP 45 ENDFIL LDA EOF INSERT END OF FILE MARKER 50 STA BUFFER 55 LDA THREE SET LENGTH = 3 60 STA LENGTH 65 JSUB WRREC WRITE EOF 70 LDL RETADR GET RETURN ADDRESS 75 RSUB RETURN TO CALLER 80 EOF BYTE C’EOF’ 85 THREE WORD 3 90 ZERO WORD 0 95 RETADR RESW 1 100 LENGTH RESW 1 105 BUFFER RESB BYTE BUFFER AREA

5 Example Program Fig. 2.1 (3/4)
110 . 115 . SUBROUTINE TO READ RECORD INTO BUFFER 120 . 125 RDREC LDX ZERO CLEAR LOOP COUNTER 130 LDA ZERO CLEAR A TO ZERO 135 RLOOP TD INPUT TEST INPUT DEVICE 140 JEQ RLOOP LOOP UNTIL READY 145 RD INPUT READ CHARACTER INTO A 150 COMP ZERO TEST FOR END OF RECORD 155 JEQ EXIT EXIT LOOP IF EOR 160 STCH BUFFER,X STORE CHAR IN BUFFER 165 TIX MAXLEN LOOP UNLESS MAX LENGTH 170 JLT RLOOP HAS BEEN REACHED 175 EXIT STX LENGTH SAVE RECORD LENGTH 180 RSUB RETURN TO CALLER 185 INPUT BYTE X’F1’ CODE FOR INPUT DEVICE 190 MAXLEN WORD 4096

6 Example Program Fig. 2.1 (4/4)
195 . 200 . SUBROUTINE TO WRITE RECORD FROM BUFFER 205 . 210 WRREC LDX ZERO CLEAR LOOP COUNTER 215 WLOOP TD OUTPUT TEST OUTPUT DEVICE 220 JEQ WLOOP LOOP UNTIL READY 225 LDCH BUFFER,X GET CHAR FROM BUFFER 230 WD OUTPUT WRITE CHARACTER 235 TIX LENGTH LOOP UNTIL ALL CHAR 240 JLT WLOOP HAVE BEEN WRITTEN 245 RSUB RETURN TO CALLER 250 OUTPUT BYTE X’05’ CODE FOR OUTPUT DEVICE 255 END FIRST

7 Assembler Directives Pseudo-Instructions Basic assembler directives
Not translated into machine instructions Providing information to the assembler Basic assembler directives START END BYTE WORD RESB RESW

8 Functions of a Basic Assembler
Mnemonic code (or instruction name)  opcode Symbolic operands (e.g., variable names)  addresses Choose the proper instruction format and addressing mode Constants  Numbers Output to object files and listing files

9 Example Program & Object Code (1/3)
Line Loc Source statement Object code COPY START 1000 FIRST STL RETADR CLOOP JSUB RDREC LDA LENGTH COMP ZERO 30 100C JEQ ENDFIL 35 100F JSUB WRREC J CLOOP 3C1003 ENDFIL LDA EOF 00102A STA BUFFER 0C1039 55 101B LDA THREE 00102D 60 101E STA LENGTH 0C1036 JSUB WRREC LDL RETADR RSUB 4C0000 80 102A EOF BYTE C’EOF’ 454F46 85 102D THREE WORD ZERO WORD RETADR RESW 1 LENGTH RESW 1 BUFFER RESB 4096

10 Example Program & Object Code (2/3)
110 . SUB TO READ RECORD INTO BUFFER 120 . RDREC LDX ZERO C LDA ZERO F RLOOP TD INPUT E0205D JEQ RLOOP 30203F RD INPUT D8205D COMP ZERO B JEQ EXIT E STCH BUFFER,X TIX MAXLEN 2C205E JLT RLOOP 38203F EXIT STX LENGTH A RSUB 4C0000 D INPUT BYTE X’F1’ F1 E MAXLEN WORD

11 Example Program & Object Code (3/3)
195 . SUB TO WRITE RECORD FROM BUFFER 205 . WRREC LDX ZERO WLOOP TD OUTPUT E02079 JEQ WLOOP A LDCH BUFFER,X D WD OUTPUT DC2079 TIX LENGTH 2C1036 JLT WLOOP RSUB 4C0000 OUTPUT BYTE X’05’ 05 255 END FIRST

12 Examples Mnemonic code (or instruction name)  opcode Examples:
STL 1033  LDA 1036 

13 Symbolic Operands We are not likely to write memory addresses directly in our code Instead, we will define variable names Other examples of symbolic operands: Labels (for jump instructions) Subroutines Constants

14 Converting Symbols to Numbers
Isn’t it straightforward? Isn’t it simply the sequential processing of the source program, one line at a time? Not so, if we have forward references  we don’t know the value of the symbol, because it is defined later in the code COPY START 1000 LDA LEN LEN RESW 1

15 Two-Pass Assembler Pass 1 Pass 2
Assign addresses to all statements in the program Save the values assigned to all labels for use in Pass 2 Perform some processing of assembler directives Pass 2 Assemble instructions by translating opcode and symbolic operands Generate data values defined by BYTE, WORD Perform processing of assembler directives not done in Pass 1 Write the object program and the assembly listing

16 Two-Pass Assembler From input line: LABEL, OPCODE, OPERAND
Operation Code Table (OPTAB) Symbol Table (SYMTAB) Location Counter (LOCCTR) Source program Intermediate file Object code Pass 1 Pass 2 OPTAB SYMTAB SYMTAB

17 OPTAB (Operation Code Table)
Content Mnemonic, machine code (instruction format, length) etc. Characteristic Static table Implementation Array or hash table, easy for search

18 SYMTAB (Symbol Table) Content Characteristic Implementation
Label name, value, flag, (type, length) etc. Characteristic Dynamic table (insert, delete, search) Implementation Hash table, non-random keys, hashing function COPY 1000 FIRST CLOOP 1003 ENDFIL 1015 EOF 1024 THREE 102D ZERO 1030 RETADR 1033 LENGTH 1036 BUFFER 1039 RDREC 2039

19 Two Pass Assembler – Pass 1
read first input line if OPCODE = ‘START’ then save #[OPERAND] as starting address initialize LOCCTR to starting address write line to intermediate file read next input line else initialize LOCCTR to 0 while OPCODE  ‘END’ do if this is not a comment line then if there is a symbol in the LABEL field then search SYMTAB for LABEL if found then set error flag (duplicate symbol) else insert (LABEL, LOCCTR) into SYMTAB

20 Two Pass Assembler – Pass 1
search OPTAB for OPCODE if found then add 3 (instruction length) to LOCCTR else if OPCODE = ‘WORD’ then add 3 to LOCCTR else if OPCODE = ‘RESW’ then add 3 * #[OPERAND] to LOCCTR else if OPCODE = ‘RESB’ then add #[OPERAND] to LOCCTR else if OPCODE = ‘BYTE’ then find length of constant in bytes add length to LOCCTR else set error flag (invalid op code) write line to intermediate file read next input line // end while write last line to intermediate file save (LOCCTR – starting address) as program length

21 Two Pass Assembler – Pass 2
read first input line from intermediate file if OPCODE = ‘START’ then write listing line read next input line write Header record to object program initialize first Text record while OPCODE  ‘END’ do if this is not a comment line then search OPTAB for OPCODE if found then if there is a symbol in OPERAND field then search SYMTAB for OPERAND if found then store symbol value as operand address else store 0 as operand address and set error flag (undefined symbol)

22 Two Pass Assembler – Pass 2
else store 0 as operand address assemble the object code instructions else if OPCODE = ‘BYTE’ or ‘WORD’ then convert constant to object code if object code will not fit into the current Text record then write Text record to object file initialize new Text record add object code to Text record write listing line read next input line write last Text record to object file write End record to object program write last listing line

23 Object Program Header Text End Col. 1 H Col. 2~7 Program name
Col. 8~ Starting address (hex) Col Length of object program in bytes (hex) Text Col.1 T Col.2~7 Starting address in this record (hex) Col. 8~9 Length of object code in this record in bytes (hex) Col. 10~69 Object code ( )/6=10 instructions End Col.1 E Col.2~7 Address of first executable instruction (hex) (END program_name)

24 Fig. 2.3 H COPY A T E T 00101E 15 0C C F T E E0205D 30203F D8205D … T C C0000 F E … T C E starting address

25 1036 xxxxxx BYTE C’EOF’ WORD 3 WORD 0 RESW 1 1033 xxxxxx 1030 000000 STL RETADR JSUB RDREC LDA LENGTH COMP ZERO JEQ ENDFIL LDL RETADR RSUB ‘E’ ‘O’ ‘F’ 3 102D 000003 102A 454F46 1027 4C0000 1024 081033 100C 301015 1009 281030 1006 001036 1003 482039 1000 141033

26 One-Pass Assemblers Forward references can be resolved in One-Pass Assemblers too! Add a linked list to the Symbol Table to keep track of unresolved references. (See p.95) We will discuss 1-pass assembler again (Section 2.4.1)


Download ppt "Today’s Topic Assembler: Basic Functions"

Similar presentations


Ads by Google