Subject Name SYSTEM SOFTWARE Subject Code 10 CS

Subject Name: SYSTEM SOFTWARE Subject Code: 10 CS 52 Prepared By: Suganthi Sivakumar Department: CSE Date SYSTEM SOFTWARE - UNIT II 1

SYSTEM SOFTWARE UNIT-II Engineered for Tomorrow SYSTEM SOFTWARE - UNIT II 2

ASSEMBLERS - I Engineered for Tomorrow SYSTEM SOFTWARE - UNIT II 3

Topic details üAssembler üIntroduction üAssembler Directives üAssembler functions üData Structures üMachine Dependant Assembler Features üInstruction format and Assembler Features üProgram Relocation SYSTEM SOFTWARE - UNIT II 4

Role of Assembler Source Program Object Assembler Code Linker Executable Code Loader SYSTEM SOFTWARE - UNIT II 5

Introduction Fundamental functions üTranslating mnemonic operation codes to their machine language equivalents üAssigning machine addresses to symbolic labels Machine dependency üDifferent machine instruction formats and codes SYSTEM SOFTWARE - UNIT II 6

2. 1 Assembler Directives Pseudo-Instructions Not translated into machine instructions Providing information to the assembler Basic assembler directives START END BYTE WORD RESB RESW SYSTEM SOFTWARE - UNIT II 7

Example of a SIC Assembler Language Program (3/1) SYSTEM SOFTWARE - UNIT II 8

Example of a SIC Assembler Language Program (3/2) SYSTEM SOFTWARE - UNIT II 9

Example of a SIC Assembler Language Program (3/3) SYSTEM SOFTWARE - UNIT II 10

Example Program (Fig. 2. 1) Purpose ü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 SYSTEM SOFTWARE - UNIT II 11

Example Program (Fig. 2. 1) Data transfer (RD, WD) 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) RDREC, WRREC save link register first before nested jump SYSTEM SOFTWARE - UNIT II 12

2. 1. 1 Assembler’s functions üConvert mnemonic operation codes to their machine language equivalents üConvert symbolic operands to their equivalent machine addresses üBuild the machine instructions in the proper format üConvert the data constants to internal machine representations üWrite the object program and the assembly listing SYSTEM SOFTWARE - UNIT II 13

Assembly Program with Object Code Forward reference SYSTEM SOFTWARE - UNIT II 14

Assembly Program with Object Code SYSTEM SOFTWARE - UNIT II 15

Assembly Program with Object Code SYSTEM SOFTWARE - UNIT II 16

Forward Reference Definition üA reference to a label that is defined later in the program Solution üTwo passes üFirst pass: scan the source program for label definition, address accumulation, and address assignment üSecond pass: perform most of the actual instruction translation SYSTEM SOFTWARE - UNIT II 17

Example FIRST STL RETADR ---- X--- ---- ---0001 0100 0001 0000 0011 141033 STCH BUFFER, X ---- X--- ---- ---0101 0100 1001 0000 0011 1001 549039 SYSTEM SOFTWARE - UNIT II 18

Object Program Format Header Col. 1 Col. 2~7 Col. 8~13 Col. 14 -19 H Program name Starting address of object program (hex) Length of object program in bytes (hex) Text Col. 1 Col. 2~7 Col. 8~9 Col. 10~69 T Starting address for object code in this record (hex) Length of object code in this record in bytes (hex) Object code, represented in hex (2 col. per byte) End Col. 1 Col. 2~7 E Address of first executable instruction in object program (hex) SYSTEM SOFTWARE - UNIT II 19

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 SYSTEM SOFTWARE - UNIT II 20

Example of Instruction Assemble STCH BUFFER, X (54)16 549039 1 (001)2 (039)16 Forward reference SYSTEM SOFTWARE - UNIT II 21

Difficulties: Forward Reference Forward reference: reference to a label that is defined later in the program. Loc Label Operator Operand 1000 1003 … 1012 … 1033 FIRST CLOOP … STL JSUB … J … RETADR RDREC … CLOOP … … RETADR RESW SYSTEM SOFTWARE - UNIT II … … 1 22

Two Pass Assembler Pass 1 ü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 ü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 SYSTEM SOFTWARE - UNIT II 23

Two Pass Assembler Read from input line üLABEL, OPCODE, OPERAND Source program Intermediate file Pass 1 OPTAB SYMTAB Pass 2 Object codes SYMTAB SYSTEM SOFTWARE - UNIT II 24

2. 1. 2 Assembler Algorithm and Data Structures üOperation Code Table (OPTAB) üSymbol Table (SYMTAB) üLocation Counter(LOCCTR) SYSTEM SOFTWARE - UNIT II 25

OPTAB (operation code table) Content ümenmonic, machine code (instruction format, length) etc. Characteristic üstatic table Implementation üarray or hash table, easy for search SYSTEM SOFTWARE - UNIT II 26

SYMTAB (symbol table) Content ülabel name, value, flag, (type, length) etc. Characteristic üdynamic table (insert, delete, search) Implementation ühash table, non-random keys, hashing function SYSTEM SOFTWARE - UNIT II COPY 1000 FIRST 1000 CLOOP 1003 ENDFIL 1015 EOF 1024 THREE 102 D ZERO 1030 RETADR 1033 LENGTH 1036 BUFFER 1039 RDREC 2039 27

LOCCTR üUsed to help in the assignment of addresses üInitialized to the beginning address specified in the START statement üAfter each source statement is processed, the length of the assembled instruction or data area to be generated is added üGives the address of a label SYSTEM SOFTWARE - UNIT II 28

Pseudo Code for Pass 1 SYSTEM SOFTWARE - UNIT II 29

Pseudo Code for Pass 1 SYSTEM SOFTWARE - UNIT II 30

Pseudo Code for Pass 1 SYSTEM SOFTWARE - UNIT II 31

Pseudo Code for Pass 2 SYSTEM SOFTWARE - UNIT II 32

Pseudo Code for Pass 2 SYSTEM SOFTWARE - UNIT II 33

Pseudo Code for Pass 2 SYSTEM SOFTWARE - UNIT II 34

Machine-dependent Assembler Features üInstruction formats and addressing modes üProgram relocation SYSTEM SOFTWARE - UNIT II 35

2. 2. 1 Instruction Format and Addressing Mode SIC/XE ü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) SYSTEM SOFTWARE - UNIT II 36

Translation Register translation ü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 üMost register-memory instructions use program counter relative or base relative addressing üFormat 3: 12 -bit address field übase-relative: 0~4095 üpc-relative: -2048~2047 üFormat 4: 20 -bit address field SYSTEM SOFTWARE - UNIT II 37

PC-Relative Addressing Modes PC-relative ü 10 0000 FIRST STL RETADR 17202 D (14)16 1 1 0 0 1 0 (02 D) 16 üdisplacement= RETADR - PC = 30 -3 = 2 D ü 40 0017 J CLOOP 3 F 2 FEC (3 C)16 11 001 0 (FEC) 16 üdisplacement= CLOOP-PC= 6 - 1 A= -14= FEC SYSTEM SOFTWARE - UNIT II 38

BASE RELATIVE ADDRESSING MODE Base-relative übase register is under the control of the programmer ü 12 LDB #LENGTH ü 13 BASE LENGTH ü 160 104 E STCH BUFFER, X 57 C 003 ( 54 )16 111100 ( 003 ) 16 (54) 1 1 1 0 0036 -1051= -101 B 16 ü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 SYSTEM SOFTWARE - UNIT II 39

Immediate Address Translation Immediate addressing ◦ 55 0020 ◦ 133 103 C LDA #3 ( 00 )16 010000 +LDT #4096 ( 74 )16 010001 SYSTEM SOFTWARE - UNIT II 010003 ( 003 ) 16 75101000 ( 01000 ) 16 40

Immediate Address Translation(Cont. ) Immediate addressing ü 12 0003 LDB #LENGTH 69202 D ( 68)16 010010 ( 02 D ) 16 ( 68)16 010000 ( 033)16 690033 ü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 SYSTEM SOFTWARE - UNIT II 41

Indirect Address Translation Indirect addressing ütarget addressing is computed as usual (PC-relative or BASErelative) üonly the n bit is set to 1 ü 70 002 A J @RETADR 3 E 2003 ( 3 C )16 100010 ( 003 ) 16 üTA=RETADR=0030 üTA=(PC)+disp=002 D+0003 SYSTEM SOFTWARE - UNIT II 42

Example of SIC/XE Program(3/1) SYSTEM SOFTWARE - UNIT II 43

Example of SIC/XE Program(3/2) SYSTEM SOFTWARE - UNIT II 44

Example of SIC/XE Program(3/3) SYSTEM SOFTWARE - UNIT II 45

Program with Object Code (3/1) SYSTEM SOFTWARE - UNIT II 46

Object Code Translation Format 3 op(6) n i x b p e Format 4 op(6) n i xbpe disp(12) address(20) Line 10: STL=14, n=1, i=1 ni=3, op+ni=14+3=17, RETADR=0030, x=0, b=0, p=1, e=0 xbpe=2, PC=0003, disp=RETADR-PC=030 -003=02 D, xbpe+disp=202 D, obj=17202 D Line 12: LDB=68, n=0, i=1 ni=1, op+ni=68+1=69, LENGTH=0033, x=0, b=0, p=1, e=0 xbpe=2, PC=0006, disp=LENGTH-PC=033 -006=02 D, xbpe+disp=202 D, obj=69202 D SYSTEM SOFTWARE - UNIT II 47

Object Code Translation Format 3 op(6) n i x b p e Format 4 op(6) n i xbpe disp(12) address(20) Line 15: JSUB=48, n=1, i=1 ni=3, op+ni=48+3=4 B, RDREC=01036, x=0, b=0, p=0, e=1, xbpe+RDREC=101036, obj=4 B 101036 Line 40: J=3 C, n=1, i=1 ni=3, op+ni=3 C+3=3 F, CLOOP=0006, x=0, b=0, p=1, e=0 xbpe=2, PC=001 A, disp=CLOOP-PC=0006 -001 A=14=FEC(2’s complement), xbpe+disp=2 FEC, obj=3 F 2 FEC Line 55: LDA=00, n=0, i=1 ni=1, op+ni=00+1=01, disp=#3 003, x=0, b=0, p=0, e=0 xbpe=0, xbpe+disp=0003, obj=010003 SYSTEM SOFTWARE - UNIT II 48

Program with Object Code (3/2) SYSTEM SOFTWARE - UNIT II 49

Object Code Translation Format 2 op(8) r 1(4) r 2(4) Line 125: CLEAR=B 4, r 1=X=1, r 2=0, obj=B 410 Line 133: LDT=74, n=0, i=1 ni=1, op+ni=74+1=75, x=0, b=0, p=0, e=1 xbpe=1, #4096=01000, xbpe+address=101000, obj=75101000 Line 160: STCH=54, n=1, i=1 ni=3, op+ni=54+3=57, BUFFER=0036, B=0033, disp=BUFFER-B=003, x=1, b=1, p=0, e=0 xbpe=C, xbpe+disp=C 003, obj=57 C 003 SYSTEM SOFTWARE - UNIT II 50

Program with Object Code (3/3) SYSTEM SOFTWARE - UNIT II 51

SYMTAB SYMBOL VALUE FIRST FLAGS SYMBOL VALUE 0000 RDREC 1036 CLOOP 0006 RLOOP 1040 ENDFIL 001 A EXIT 1056 EOF 002 D INPUT 105 C RETADR 0030 WRREC 105 D LENGTH 0033 WLOOP 1062 BUFFER 0036 OUTPUT 1076 SYSTEM SOFTWARE - UNIT II FLAGS 52

2. 2. 2 Program Relocation Example Fig. 2. 1 ◦ Absolute program, starting address 1000 e. g. 55 101 B LDA THREE ◦ Relocate the program to 2000 e. g. 55 101 B LDA THREE ◦ Each Absolute address should be modified SYSTEM SOFTWARE - UNIT II 00102 D 00202 D 53

2. 2. 2 Program Relocation Example Fig. 2. 5: üExcept for absolute address, the rest of the instructions need not be modified ü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 SYSTEM SOFTWARE - UNIT II 54

Example SYSTEM SOFTWARE - UNIT II 55

Relocatable Program Modification record ◦ Col 1 ◦ Col 2 -7 ◦ Col 8 -9 M Starting location of the address field to be modified, relative to the beginning of the program length of the address field to be modified, in halfbytes SYSTEM SOFTWARE - UNIT II 56

Object Code SYSTEM SOFTWARE - UNIT II 57

Question Bank 1) Mention the different data structures used in two pass assembler along with brief explanation of their function in both the pass. 2) Generate the object code & object program for the following SIC/XE program. SUM START 0 FIRST LDX LDA +LDB BASE ADD TIX JLT +STA #0 #0 #TABLE 2 TABLE, X TABLE 2, X COUNT LOOP TOTAL LOOP Given details of opcode for instruction LDX=04 LDA=00 LDB=68 ADD=18 TIX=2 C JLT=38 STA=0 C RSUB=4 C RSUB COUNT RESW 1 TABLE RESW 2000 TOTAL RESW 1 END FIRST SYSTEM SOFTWARE - UNIT II 58

Question Bank 3) Write the fundamental functions of assembler. 4) Generate the object code for each statement & write the object program for the following SIC/XE program. WRREC WLOOP OUTPUT BUFFER LENGTH START 105 D Given details of opcode for instruction CLEAR LDT TD JEQ LDCH WD TI XR JLT RSUB BYTE RESB END X LENGTH OUTPUT WLOOP BUFFER, X OUTPUT T WLOOP CLEAR=B 4 T=5 TIX=2 C JLT=38 STA=0 C RSUB=4 C LDT=74 TD=E 0 JEQ=30 TI XR=B 8 LDCH=50 WD=DC X=1 X ‘ 05’ 400 2 WRREC SYSTEM SOFTWARE - UNIT II 59

Question Bank 5) What is the need of pass 2 assembler? Reason out with simple example. 6) Define assembler directive. Explain the different types of directives used in SIC machine. 7) With brief explanation write the algorithm of PASS-1 of two pass assembler. 8)Briefly explain each & every record format of an object program for SIC/XE machine. 9) With an example explain OPTAB & SYMTAB. 10) What is program relocation? Explain the problems associated with it & their solutions. 11)With brief explanation write the algorithm of PASS-2 of two pass assembler. SYSTEM SOFTWARE - UNIT II 60

Question Bank 12)Generate the object code for the following SIC program. SUM START 4000 Given details of opcode for instruction FIRST ZERO TABLE, X COUNT LOOP TOTAL LDX=04 LDA=00 TIX=2 C STA=0 C RSUB=4 C ADD=18 JLT=38 COUNT LDX LDA ADD TIX JLT STA RSUB RESW TABLE RESW 2000 ZERO WORD 0 TOTAL RESW 1 END FIRST LOOP 1 SYSTEM SOFTWARE - UNIT II 61

THANK YOU SYSTEM SOFTWARE - UNIT II 62
- Slides: 62