System Software ASSEMBLER 1 1 Role of Assembler

System Software ASSEMBLER - 1 1

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

Assembler Directives n Basic assembler directives START n END n BYTE n WORD n RESB n RESW n 3

SIC Instruction Set n n n Load/Store: LDA/STA, LDX/STX…etc. Arithmetic: ADD, SUB, MUL, DIV Compare: COMP Jump: J Conditional Jump: JLT, JEQ, JGT See Appendix A for the complete list. 4

SIC Instruction Format n n Opcode: 8 bits Address: one bit flag (x) and 15 bits of address 8 1 OPCODE X 15 Address 5

Example Program n Purpose n n n Reads records from input device (code F 1) Copies them to output device (code 05) At the end of the file, writes EOF on the output device, then RSUB to the operating system 6

Example Program n Data transfer (RD, WD) n n n A buffer is used to store record Buffering is necessary for different I/O rates The end of each record is marked with a null character (0016) The end of the file is indicated by a zero-length record Subroutines (JSUB, RSUB) n n RDREC, WRREC Save link register first before nested jump 7

Example Program Line 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 Source statement 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 SAVE RETURN ADDRESS READ INPUT RECORD TEST 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 8

110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 . . . RDREC RLOOP EXIT INPUT MAXLEN. . . WRREC WLOOP OUTPUT SUBROUTINE TO READ RECORD INTO BUFFER LDX LDA TD JEQ RD COMP JEQ STCH TIX JLT STX RSUB BYTE WORD ZERO INPUT RLOOP INPUT ZERO EXIT BUFFER, X MAXLEN RLOOP LENGTH X’F 1’ 4096 CLEAR LOOP COUNTER CLEAR A TO ZERO TEST INPUT DEVICE LOOP UNTIL READY READ CHARACTER INTO REGISTER A TEST FOR END OF RECORD (X’ 00’) EXIT LOOP IF EOR STORE CHARACTER IN BUFFER LOOP UNLESS MAX LENGTH HAS BEEN REACHED SAVE RECORD LENGTH RETURN TO CALLER CODE FOR INPUT DEVICE SUBROUTINE TO WRITE RECORD FROM BUFFER LDX TD JEQ LDCH WD TIX JLT RSUB BYTE END ZERO OUTPUT WLOOP BUFFER, X OUTPUT LENGTH WLOOP X’ 05’ FIRST CLEAR LOOP COUNTER TEST OUTPUT DEVICE LOOP UNTIL READY GET CHARACTER FROM BUFFER WRITE CHARACTER LOOP UNTIL ALL CHARACTERS HAVE BEEN WRITTEN RETURN TO CALLER CODE FOR OUTPUT DEVICE 9

Functions of a Basic Assembler n n n 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. 10

Examples n n Mnemonic code (or instruction name) opcode. Examples: STL 1033 opcode 14 10 33 0001 0100 0 001 0000 0011 LDA 1036 opcode 00 10 36 0000 0 001 0000 0011 0110 11

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

Converting Symbols to Numbers n Isn’t it straightforward? n n Isn’t it simply the sequential processing of the source program, one line at a time? Not so, if we have forward references. COPY START … LDA LEN … … LEN RESW 1 1000 13

Example Program with Object Code Line Loc 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 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 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 Object code 1000 RETADR RDREC LENGTH ZERO ENDFIL WRREC CLOOP EOF BUFFER THREE LENGTH WRREC RETADR C’EOF’ 3 0 1 1 4096 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 14

110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 2039 203 C 203 F 2042 2045 2048 204 B 204 E 2051 2054 2057 205 A 205 D 205 E 2061 2064 2067 206 A 206 D 2070 2073 2076 2079 . . . RDREC RLOOP EXIT INPUT MAXLEN. . . WRREC WLOOP OUTPUT SUBROUTINE TO READ RECORD INTO BUFFER LDX LDA TD JEQ RD COMP JEQ STCH TIX JLT STX RSUB BYTE WORD ZERO INPUT RLOOP INPUT ZERO EXIT BUFFER, X MAXLEN RLOOP LENGTH X’F 1’ 4096 041030 001030 E 0205 D 30203 D D 8205 D 281030 302057 549039 2 C 205 E 38203 F 101036 4 C 0000 F 1 001000 SUBROUTINE TO WRITE RECORD FROM BUFFER LDX TD JEQ LDCH WD TIX JLT RSUB BYTE END ZERO OUTPUT WLOOP BUFFER, X OUTPUT LENGTH WLOOP X’ 05’ FIRST 041030 E 02079 302064 509039 DC 2079 2 C 1036 382064 4 C 0000 05 15

Object Program n Header Col. 1 Col. 2~7 Col. 8~13 Col. 14 -19 n Text Col. 1 Col. 2~7 Col. 8~9 Col. 10~69 n 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 Object code End Col. 1 Col. 2~7 E Address of first executable instruction (hex) (END program_name) 16

Object Code n n n n n H^COPY ^001000^00107 A T^001000^1 E^141033^482039^001036^281030^301015^ 482061^3 C 1003^00102 A^0 C 1039^00102 D T^00101 E^15^0 C 1036^482061^081033^4 C 0000^454 F 46^ 000003^000000…. . T^002039^1 E^041030^001030^E 0205 D^30203 F^D 8205 D^ 281030^302057^549039^2 C 205 E^38203 F T^002057^1 C^101036^4 C 0000^F 1^0010000^041030^E 02079 ^302064^509039^DC 2079^2 C 1036 T^002073^07^382064^4 C 0000^05 E 001000 starting address 17

Two Pass Assembler n Pass 1 n n 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 n n Assemble instructions 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 18

Two Pass Assembler n Read from input line n LABEL, OPCODE, OPERAND Source program Pass 1 OPTAB Intermediate file Pass 2 Object codes SYMTAB 19

Data Structures n n n Operation Code Table (OPTAB) Symbol Table (SYMTAB) Location Counter(LOCCTR) 20

OPTAB (operation code table) n Content n n Characteristic n n Mnemonic, machine code (instruction format, length) etc. Static table Implementation n Array or hash table, easy for search 21

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

Two Pass Assembler – Pass 1 Assembler 23

Two Pass Assembler – Pass 2 Assembler 24

SIC/XE Assembler n What’s new for SIC/XE? n n More addressing modes. Program Relocation. 25

An SIC/XE Example Line Loc 5 10 12 13 15 20 25 30 35 40 45 50 55 60 65 70 80 95 100 105 Source statement 0000 0003 COPY FIRST 0006 000 A 000 D 0010 0013 0017 001 A 001 D 0020 0023 0026 002 A 002 D 0030 0033 0036 CLOOP ENDFIL EOF RETADR LENGTH BUFFER START STL LDB BASE +JSUB LDA COMP JEQ +JSUB J LDA STA +JSUB J BYTE RESW RESB Object code 0 RETADR #LENGTH RDREC LENGTH #0 ENDFIL WRREC CLOOP EOF BUFFER #3 LENGTH WRREC @RETADR C’EOF’ 1 1 4096 17202 D 69202 D 4 B 101036 032026 290000 332007 4 B 10105 D 3 F 2 FEC 032010 0 F 2016 010003 0 F 200 D 4 B 10105 D 3 E 2003 454 F 46 26

115 120 125 130 132 133 135 140 145 150 155 160 165 170 175 180 185 195 200 205 210 212 215 220 225 230 235 1036 1038 103 A 103 C 1040 1043 1046 1049 104 B 104 E 1051 1053 1056 1059 105 C 105 D 105 F 1062 1065 1068 106 B 106 E . . RDREC RLOOP EXIT INPUT. . . WRREC READ RECORD INTO BUFFER CLEAR X CLEAR A CLEAR S +LDT #4096 TD INPUT JEQ RLOOP RD INPUT COMPR A, S JEQ EXIT STCH BUFFER, X TIXR T JLT RLOOP STX LENGTH RSUB BYTE X’F 1’ B 410 B 400 B 440 75101000 E 32019 332 FFA DB 2013 A 004 332008 57 C 003 B 850 3 B 2 FEA 134000 4 F 0000 F 1 WRITE RECORD FROM BUFFER CLEAR LDT WLOOP TD JEQ LDCH WD TIXR. . . (omitted) X LENGTH OUTPUT WLOOP BUFFER, X OUTPUT T B 410 774000 E 32011 332 FFA 53 C 003 DF 2008 B 850 27

A Case of Object Code Generation n n Figure 2. 6, Line 10 STL RETADR 17 20 2 D The mode bit p=1, meaning PC relative addressing mode. 6 bits OPCODE n i x b p e 0001 01 17 1 1 0 0 1 0 20 12 bits Address 0000 0010 1101 2 D 28

Instruction Format and Addressing Mode n SIC/XE n n n n PC-relative or Base-relative addressing: op m Indirect addressing: op @m Immediate addressing: op #c Extended format: +op m Index addressing: op m, x register-to-register instructions larger memory -> multi-programming (program allocation) 29

Translation n Register translation n Register name (A, X, L, B, S, T, F, PC, SW) and their values (0, 1, 2, 3, 4, 5, 6, 8, 9) Preloaded in SYMTAB Address translation n n Most register-memory instructions use program counter relative or base relative addressing Format 3: 12 -bit address field n n n Base-relative: 0~4095 PC-relative: -2048~2047 Format 4: 20 -bit address field 30

PC-Relative Addressing Mode n PC-relative n 10 0000 FIRST STL RETADR OPCODE n i x b p e 0001 01 1 1 0 0 17202 D Address (02 D)16 Displacement= RETADR - PC = 30 -3 = 2 D 40 0017 J CLOOP 3 F 2 FEC n n OPCODE n i x b p e 0011 11 n 1 1 0 0 1 0 Address (FEC)16 Displacement= CLOOP-PC= 6 - 1 A= -14= FEC 31

Base-Relative Addressing Modes n Base-relative n n Base register is under the control of the programmer 12 LDB #LENGTH 13 BASE LENGTH 160 104 E STCH BUFFER, X 57 C 003 OPCODE n i x b p e Address 0101 01 1 1 0 0 (003)16 n Displacement= BUFFER - B = 0036 - 0033 = 3 § NOBASE is used to inform the assembler that the contents of the base register no longer be relied upon for addressing 32

Immediate Address Translation n Immediate addressing n 55 0020 LDA OPCODE n i x b p e 0000 00 n 133 010003 Address 0 1 0 0 103 C (003)16 +LDT #4096 OPCODE n i x b p e 0111 01 #3 0 1 0 0 0 1 75101000 Address (01000)16 33

Immediate Address Translation n Immediate addressing n 12 0003 LDB #LENGTH 69202 D OPCODE n i x b p e Address 0110 10 n 12 n n n (02 D)16 0003 LDB #LENGTH 690033 OPCODE n i x b p e Address 0110 10 n 0 1 0 0 (033)16 The immediate operand is the symbol LENGTH The address of this symbol LENGTH is loaded into register B LENGTH=0033=PC+displacement=0006+02 D If immediate mode is specified, the target address becomes the operand 34

Indirect Address Translation n Indirect addressing n Target addressing is computed as usual (PC-relative or BASE-relative) n Only the n bit is set to 1 n 70 002 A J @RETADR OPCODE n i x b p e 0011 11 n n 1 0 0 0 1 0 3 E 2003 Address (003)16 TA=RETADR=0030 TA=(PC)+disp=002 D+0003 35

Program Relocation 36

Examples of Program Relocation n Example Fig. 2. 2 n 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 Absolute program, starting address 1000 2000 2003 2006 2009 200 C 200 F 2012 2015 2018 201 B 201 E 2021 2024 2027 202 A 202 D 2030 2033 2036 2039 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 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 WREC CLOOP EOF BUFFER THREE LENGTH WREC RETADR C'EOF' 3 0 1 1 4096 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 E 46 000003 000000 37 142033 483039 002036 282030 302015 483061 3 C 2003 00202 A 0 C 2039 00202 D 0 C 2036 483061 082033 4 C 0000 454 E 46 000003 000000

Examples of Program Relocation n Example Fig. 2. 2 n Absolute program, starting address 1000 ==== 2000 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 2000 2003 2006 2009 200 C 200 F 2012 2015 2018 201 B 201 E 2021 2024 2027 202 A 202 D 2030 2033 2036 2039 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 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 2000 ==== RETADR RDREC LENGTH ZERO ENDFIL WREC CLOOP EOF BUFFER THREE LENGTH WREC RETADR C'EOF' 3 0 1 1 4096 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 E 46 000003 000000 38 142033 483039 002036 282030 302015 483061 3 C 2003 00202 A 0 C 2039 00202 D 0 C 2036 483061 082033 4 C 0000 454 E 46 000003 000000

Examples of Program Relocation n Example Fig. 2. 6: n Except for absolute address, the rest of the instructions need not be modified n n n 5 10 12 13 15 20 25 30 35 40 45 50 55 60 65 70 80 95 100 not a memory address (immediate addressing) PC-relative, Base-relative The only parts of the program that require modification at load time are those that specify direct addresses 1000 1003 0000 0003 COPY FIRST 1006 100 A 100 D 1010 1013 1017 101 A 101 D 1020 1023 1026 102 A 102 D 1030 1036 000 A 000 D 0010 0013 0017 001 A 001 D 0020 0023 0026 002 A 002 D 0030 0036 CLOOP ENDFIL EOF RETADR BUFFER START STL LDB BASE +JSUB LDA COMP JEQ +JSUB J LDA STA +JSUB J BYTE RESW RESB 0 RETADR #LENGTH RDREC LENGTH #0 ENDFIL WRREC CLOOP EOF BUFFER #3 LENGTH WRREC @RETADR C'EOF' 1 4096 17202 D 69202 D 4 B 101036 032026 290000 332007 4 B 10105 D 3 F 2 FEC 032010 0 F 2016 010003 0 F 200 D 4 B 10105 D 3 E 2003 454 F 46 4 B 102036 032026 290000 332007 4 B 10205 D 3 F 2 FEC 032010 0 F 2016 010003 0 F 200 D 4 B 10205 D 3 E 2003 454 F 46 39

Examples of Program Relocation n Example Fig. 2. 6: n Except for absolute address, the rest of the instructions need not be modified n n n 5 10 12 13 15 20 25 30 35 40 45 50 55 60 65 70 80 95 100 not a memory address (immediate addressing) PC-relative, Base-relative The only parts of the program that require modification at load time are those that specify direct addresses 1000 1003 0000 0003 COPY FIRST 1006 100 A 100 D 1010 1013 1017 101 A 101 D 1020 1023 1026 102 A 102 D 1030 1036 000 A 000 D 0010 0013 0017 001 A 001 D 0020 0023 0026 002 A 002 D 0030 0036 CLOOP ENDFIL EOF RETADR BUFFER START STL LDB BASE +JSUB LDA COMP JEQ +JSUB J LDA STA +JSUB J BYTE RESW RESB 0 1000 == RETADR #LENGTH RDREC LENGTH #0 ENDFIL WRREC CLOOP EOF BUFFER #3 LENGTH WRREC @RETADR C'EOF' 1 4096 17202 D 69202 D 4 B 101036 032026 290000 332007 4 B 10105 D 3 F 2 FEC 032010 0 F 2016 010003 0 F 200 D 4 B 10105 D 3 E 2003 454 F 46 4 B 102036 032026 290000 332007 4 B 10205 D 3 F 2 FEC 032010 0 F 2016 010003 0 F 200 D 4 B 10205 D 3 E 2003 454 F 46 40

How to Make Program Relocation Easier n Use program-counter (PC) relative addresses n n Did you notice that we didn’t modify the addresses for JEQ, JLT and J instructions? We didn’t modify the addresses for RETADR, LENGTH, and BUFFER in Figure 2. 6 either. 41

Relocatable Program n Modification record n n Col 1 M Col 2 -7 Starting location of the address field to be modified, relative to the beginning of the program n Col 8 -9 Length of the address field to be modified, in half - bytes 42

Object File with M-Records n n Modification records are added to the object files. Example: HCOPY 001000 001077 T 000000 1 D 17202 D… 4 B 101036… T 00001 D …… … M 000007 05 Modification Record …… E 000000 43

Object Code 44
- Slides: 44