CS 2422 Assembly Language and System Programming Assembler
CS 2422 Assembly Language and System Programming Assembler Basics Department of Computer Science National Tsing Hua University
Today’s Topic u Assembler: Basic Functions l u Section 2. 1 of Beck’s “System Software” book. Reading Assignment: pages 43 -52. 1
Role of Assembler Source Program Assembler Object Code Linker Executable Code Loader 2
Example Program Fig. 2. 1 (1/4) u Purpose l l l u Data transfer (RD, WD) l l u Reads records from input device (code F 1) 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 End of each record is marked with a null character End of the file is indicated by a zero-length record Subroutines (JSUB, RSUB) l l RDREC, WRREC Save link register first before nested jump 3
Example Program Fig. 2. 1 (2/4) 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 COPY FIRST CLOOP ENDFIL EOF THREE ZERO RETADR LENGTH BUFFER START STL JSUB LDA COMP JEQ JSUB J LDA STA JSUB LDL RSUB BYTE WORD RESW RESB 1000 RETADR RDREC LENGTH ZERO ENDFIL WRREC CLOOP EOF BUFFER THREE LENGTH WRREC RETADR C’EOF’ 3 0 1 1 4096 LOAD SAVE READ TEST PROG AT LOC 1000 RETURN ADDRESS INPUT RECORD FOR EOF (LENGTH = 0) EXIT IF EOF FOUND WRITE OUTPUT RECORD LOOP INSERT END OF FILE MARKER SET LENGTH = 3 WRITE EOF GET RETURN ADDRESS RETURN TO CALLER 4096 -BYTE BUFFER AREA 4
Example Program Fig. 2. 1 (3/4) 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 . . SUBROUTINE TO. RDREC LDX ZERO LDA ZERO RLOOP TD INPUT JEQ RLOOP RD INPUT COMP ZERO JEQ EXIT STCH BUFFER, X TIX MAXLEN JLT RLOOP EXIT STX LENGTH RSUB INPUT BYTE X’F 1’ MAXLEN WORD 4096 READ RECORD INTO BUFFER CLEAR LOOP COUNTER CLEAR A TO ZERO TEST INPUT DEVICE LOOP UNTIL READY READ CHARACTER INTO A TEST FOR END OF RECORD EXIT LOOP IF EOR STORE CHAR IN BUFFER LOOP UNLESS MAX LENGTH HAS BEEN REACHED SAVE RECORD LENGTH RETURN TO CALLER CODE FOR INPUT DEVICE 5
Example Program Fig. 2. 1 (4/4) 195 200 205 210 215 220 225 230 235 240 245 250 255 . . SUBROUTINE TO WRITE RECORD FROM BUFFER. WRREC LDX ZERO CLEAR LOOP COUNTER WLOOP TD OUTPUT TEST OUTPUT DEVICE JEQ WLOOP UNTIL READY LDCH BUFFER, X GET CHAR FROM BUFFER WD OUTPUT WRITE CHARACTER TIX LENGTH LOOP UNTIL ALL CHAR JLT WLOOP HAVE BEEN WRITTEN RSUB RETURN TO CALLER OUTPUT BYTE X’ 05’ CODE FOR OUTPUT DEVICE END FIRST 6
Assembler Directives u Pseudo-Instructions l l u Not translated into machine instructions Providing information to the assembler Basic assembler directives l l l START END BYTE WORD RESB RESW 7
Functions of a Basic Assembler u u u 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 8
Example Program & Object Code (1/3) Line 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 Loc 1000 1003 1006 1009 100 C 100 F 1012 1015 1018 101 B 101 E 1021 1024 1027 102 A 102 D 1030 1033 1036 1039 Source statement COPY START FIRST STL CLOOP JSUB LDA COMP JEQ JSUB J ENDFIL LDA STA JSUB LDL RSUB EOF BYTE THREE WORD ZERO WORD RETADR RESW LENGTH RESW BUFFER RESB 1000 RETADR RDREC LENGTH ZERO ENDFIL WRREC CLOOP EOF BUFFER THREE LENGTH WRREC RETADR C’EOF’ 3 0 1 1 4096 Object code 141033 482039 001036 281030 301015 482061 3 C 1003 00102 A 0 C 1039 00102 D 0 C 1036 482061 081033 4 C 0000 454 F 46 000003 000000 9
Example Program & Object Code (2/3) 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 . . SUB TO READ RECORD INTO BUFFER. 2039 RDREC LDX ZERO 041030 203 C LDA ZERO 001030 203 F RLOOP TD INPUT E 0205 D 2042 JEQ RLOOP 30203 F 2045 RD INPUT D 8205 D 2048 COMP ZERO 281030 204 B JEQ EXIT 302057 204 E STCH BUFFER, X 549039 2051 TIX MAXLEN 2 C 205 E 2054 JLT RLOOP 38203 F 2057 EXIT STX LENGTH 101036 205 A RSUB 4 C 0000 205 D INPUT BYTE X’F 1’ F 1 205 E MAXLEN WORD 4096 001000 10
Example Program & Object Code (3/3) 195 200 205 210 215 220 225 230 235 240 245 250 255 . . SUB TO WRITE RECORD FROM BUFFER. 2061 WRREC LDX ZERO 041030 2064 WLOOP TD OUTPUT E 02079 2067 JEQ WLOOP 302064 206 A LDCH BUFFER, X 509039 206 D WD OUTPUT DC 2079 2070 TIX LENGTH 2 C 1036 2073 JLT WLOOP 382064 2076 RSUB 4 C 0000 2079 OUTPUT BYTE X’ 05 END FIRST 11
Examples u u Mnemonic code (or instruction name) opcode Examples: STL 1033 14 10 33 0001 0100 0 001 0000 0011 LDA 1036 00 10 36 0000 0 001 0000 0011 0110 12
Symbolic Operands u We are not likely to write memory addresses directly in our code l u Instead, we will define variable names Other examples of symbolic operands: l l l Labels (for jump instructions) Subroutines Constants 13
Converting Symbols to Numbers u Isn’t it straightforward? l l 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 … LDA LEN … … LEN RESW 1 1000 14
Two-Pass Assembler u Pass 1 l l l u 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 l l 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 15
Two-Pass Assembler u u From input line: LABEL, OPCODE, OPERAND Operation Code Table (OPTAB) Symbol Table (SYMTAB) Location Counter (LOCCTR) Source program Pass 1 OPTAB SYMTAB Intermediate file Pass 2 Object code SYMTAB 16
OPTAB (Operation Code Table) u Content l u Characteristic l u Mnemonic, machine code (instruction format, length) etc. Static table Implementation l Array or hash table, easy for search 17
SYMTAB (Symbol Table) u Content l u Characteristic l u Label name, value, flag, (type, length) etc. Dynamic table (insert, delete, search) Implementation l Hash table, non-random keys, hashing function COPY FIRST CLOOP ENDFIL EOF THREE ZERO RETADR LENGTH BUFFER RDREC 1000 1003 1015 1024 102 D 1030 1033 1036 1039 2039 18
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 19
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 20
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 21 (undefined symbol)
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 22
Object Program u Header Col. 1 Col. 2~7 Col. 8~13 Col. 14 -19 u Text Col. 1 Col. 2~7 Col. 8~9 Col. 10~69 u H Program name Starting address (hex) Length of object program in bytes (hex) T Starting address in this record (hex) Length of object code in this record in bytes (hex) Object code (69 -10+1)/6=10 instructions End Col. 1 Col. 2~7 E Address of first executable instruction (hex) (END program_name) 23
Fig. 2. 3 H COPY 001000 00107 A T 001000 1 E 141033 482039 001036 281030 301015 482061. . . T 00101 E 15 0 C 1036 482061 081044 4 C 0000 454 F 46 000003 000000 T 002039 1 E 041030 001030 E 0205 D 30203 F D 8205 D 281030 … T 002057 1 C 101036 4 C 0000 F 1 001000 041030 E 02079 302064 … T 002073 07 382064 4 C 0000 05 E 001000 starting address 24
1036 1033 1030 102 D 102 A 1027 1024 x x x 0 0 0 4 5 4 4 C 0 0 81 x x 0 0 F 0 0 x x 0 0 4 0 3 x x 0 0 3 3 6 ‘E’ ‘O’ ‘F’ 0 RSUB 3 LDL RETADR 0 0 0 1 3 3 5 0 6 9 3 … 100 C 1009 1006 1003 1000 0 3 2 0 4 1 01 82 41 … JEQ COMP LDA JSUB STL ENDFIL ZERO LENGTH RDREC RETADR RESW 1 RESW WORD BYTE 1 0 3 C’EOF’
One-Pass Assemblers u u u 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) 26
- Slides: 27