You are on page 1of 10
225 Source statement COPY START FIRST STL cLooP = JSUB LDA comp JEQ JSUB a ENDFIL LDA STA LDA STA JSUB LDL. RSUB EOF BYTE THREE WORD ZERO WORD RETADR —RESW LENGTH — RESW BUFFER, RESB SUBROUTINE TO RDREC LDX LDA RLOOP TD JEQ RD comp JEQ STCH TIX JLT EXIT sTx RSUB INPUT BYTE MAXLEN WORD 1000 RETADR RDREC LENGTH ZERO ENDFIL WRREC CLOoP EOF BUFFER THREE, ‘LENGTH WRREC RETADR ZERO ZERO INPUT RLOOP INPUT ZERO EXIT BUFFER, X ‘MAXLEN RLOOP LENGTH XFL’ 4096 Asseniblers COPY PILE FROM INPUT TO OUTPUT 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 LENGTH OF RECORD 4096-BYTE BUFFER AREA READ RECORD INTO BUFFER 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 WRREC = LDX WLoop = TD JEQ LDCH wD TIX oLT RSUB ourpur BYTE END ZERO ourpuT WLOOP BUFFER, X ourPuT LENGTH WLOOP x05" PIRST 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 Figure 21. Example of a SIC assembler language program. 47 Line 10 15 20 2 3 3 40 4 5 5 6 6 7 7 8 8 90 9 100 105 110 115 120 125 130 135 140 145 150 155 160 165, 170 175 180 185 190 195, 200 205 210 21s 220 225, 230 235 240 245 250 255, Loc 1000 1000 1003 1006 1009 100c 100F 1012 1015 1018 101B 101z 1021 1024 1027 102A 102D 1030 1033 1036 1039 2039 203C 203F 2042 2045 2048 2048 2045 2051 2054 2057 205A 205D 205E 2061 2064 2067 2060, 206D 2070 2073 2076 2079 Figure 2.2 Program from Fig. 2.1 with object code. Source statement copy FIRST, cLOoP ENDFIL EOF THREE ZERO RETADR LENGTH BUFFER RDREC RLOOP EXIT INPUT MAXLEN WRREC WLOOP ourpuT START STL osuB LDA come JEQ oSUB a LDA STA LDA. STA JSUB LDL RSUB BYTE WORD WORD RESW RESW RESB 1000 RETADR RDREC LENGTH ZERO ENDFIL WRREC CLoop EOF BUFFER THREE, LENGTH WRREC RETADR, c’BOF 0 1 1 4 096 Object code 141033 482039 001036 281030 302015 482061 3C1003 00102a 0c1039 0102p 0c1036 482061 081033 4c0000 45446 000003 000000 SUBROUTINE TO READ RECORD INTO BUFFER LDX LDA 7D JEQ RD COMP JEQ STCH ‘TIX JLT STX RSUB BYTE WORD ZERO ZERO INPUT RLOOP, INPUT ZERO EXIT BUFFER, X MAXLEN RLOOP LENGTH X'FL’ 4096 041030 001030 £020SD 30203F D8205D 281030 302057 549039 2C205E 38203F 101036 4c0000 FL 001000 SUBROUTINE TO WRITE RECORD FROM BUFFER LDX 7™D JEQ LDCH wD TIx SLT RSUB BYTE END ZERO ourPuT WLOOP BUFFER, X ourPuT LENGTH wWLOOP x'05" FIRST 041030 £02079 302064 509039 pc2079 2C1036 382064 4coo00 05 sr aueuuur wy taLoiauly Ue Instructions of the source program, the asoein- must process statements called assembler directives (or pseudo-instructions), ese statements are not translated into machine instructions (although they ay have an effect on the object program). Instead, they provide instructions » the assembler itself. Examples of assembler directives are statements like » BYTE and WORD, which direct the assembler to generate constants as part of the object program, and RESB and RESW, which instruct the assembler to reserve memory locations without generating data values. The other assem- bler directives in our sample program are START, which specifies the starting memory address for the object program, and END, which marks the end of the program. Finally, the assembler must write the generated object code onto some out- put device. This object program will later be loaded into memory for execution, The simple object program format we use contains three types of records: Header, Text, and End. The Header record contains the program name, start- ing address, and length. Text records contain the translated (i-e., machine code) instructions and data of the program, together with an indication of the addresses where these are to be loaded. The End record marks the end of the object program and specifies the address in the program where execution is to begin. (This is taken from the operand of the program’s END statement. If no operand is specified, the address of the first executable instruction is used.) The formats we use for these records are as follows. The details of the for- mats (column numbers, etc.) are arbitrary; however, the information contained in these records must be present (in some form) in the object program. Header record: Col. 1 H Col. 2-7 Program name Col. 8-13 Starting address of object program (hexadecimal) Col.14-19 Length of object program in bytes (hexadecimal) vyte UL ODject code) ~essuutal (Z columns per End record: Col. 1 E Col. 2-7 Address of first execut lable instruction i : (hexadecimal) s, "on In object progra To avoid confusion, we have used the term coltnn rather positions within object program records. This is not meant any particular medium for the object program. Figure 2.3 shows the object program corresponding to Fig. 2.2, using thi format. In this figure, and in the other object programs we display, the symbol & is used to separate fields visually. Of course, such symbols are not present in the actual object program. Note that there is no object code corresponding to addresses 1033-2038. This storage is simply reserved by the loader for use by the program during execution. (Chapter 3 contains a detailed discussion of the operation of the loader.) We can now give a general description of the functions of the two passes of our simple assembler. than byle to refer to to imply the use of Pass 1 (define symbols): 1. Assign addresses to all statements in the program. 2. Save the values (addresses) assigned to alll labels for use in Pass 2. HEOPY 00100000107A 4820613C10030010240C103900102D 7901000151 41033482039001036261030301015, 13 30 40 9. 000454F46000003000000 700101815001036 4820610810334 C0000! 5( 4900000 et 0s430%057 10020391204 1030001030E0205D30203FD8205D281039302057) 9 53 2 7161 5 . W 041030802079302064509039DC20792C1036 3902057161 010364 C000QF 1001000041008 93 As 9p tr 100207 3073820644C000005 5001000 Figure 2.3 Object program corresponding to Fig. 2.2. Assemblers 55 Pass 1: begin 'yead first input line if OPCODE = ‘START’ then || Pbegin 1] save #[OPERAND] as starting address if initialize LoccTR to starting address | write Line to intermediate file read next input line {if START} Ise initialize LOCCTR to 0 while OPCODE # ‘END’ do” begin -if‘this is not a comment line then in if there is a symbol in the LABEL field then in search SYMTAB for LABEL if found then set error flag (duplicate symbol) se insert (LABEL,LOCCTR) into SYMTAB (i£ symbol} search OPTAB for OPCODE i£ 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 begin find length of constant in bytes add length to LOCCTR end (if BYTE) se set error flag (invalid operation code) | sen (4£ not a comment) : write line to intermediate file | read next input line {while not END} write last line to intermediate file Save (LOCCTR - starting address) as program length na (Pass 1) Figure 2.4(a) Algorithm for Pass 1 of assembler. 56 System Software Pass 2: begin xead first input line (from intermediate file) Af OPCODE = ‘START’ then. begin write listing line read next input line end (if START) write Header record to object program initialize first Text record while OPCODE # ‘END’ do begin if this is not a comment line then begin search OPTAB for OPCODE 4£ found then if there is a symbol in OPERAND field then begin search SYMTAB for OPERAND df found then store symbol value as operand address else begin store 0 as operand address set error flag (undefined symbol) end end (if symbol) . else store 0 as operand address assemble the object code instruction end (if opcode found) else if OPCODE = ‘BYTE’, or ‘WORD’ then convert constant to object code if object code will not fit into the current Text record then begin write Text record to object program initialize new Text record end add object code to Text record end {if not comment) 6 write listing line read next input line end {while not END) : write last Text record to object program write End record to object program write last listing line end (Pass 2} 4 Figure 2.4(b) Algorithm for Pass 2 of assembler. : s _«@ Line 10 12 13 15 20 25 30 35 40 45 50 55 60 65 70 80 95 100 105 110 115 120 125 130 132 133 135 140 145 150 155 160 165 170 175 180 185 195 200 205 210 12 215 220 225 230 235 240 245 250 255, Source statement copy START FIRST STL. LDB BASE cLOoP = +aSUB LDA comp JEQ +JSUB a ENDFIL LDA STA LDA STA +ISUB a EOF BYTE RETADR —RESW LENGTH — RESW BUFFER RESB 0 RETADR #LENGTH LENGTH, RDREC LENGTH #0 ENDFIL WRREC CLOOP EOF BUFFER 3 LENGTH WRREC @RETADR CBO’ 1 1 4096 Assemblers 57 COPY FILE FROM INPUT TO OUTPUT SAVE RETURN ADDRESS ESTABLISH BASE REGISTER 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 RETURN TO CALLER ( LENGTH OF RECORD 4096-BYTE BUFFER AREA 7 SUBROUTINE TO READ RECORD INTO BUFFER RDREC CLEAR CLEAR CLEAR +LDT RLOOP = TD JEQ RD COMPR JEQ STCH TIXR oLT EXIT STXx RSUB INPUT BYTE. WRREC CLEAR LDT WLoop = TD JEQ LDCH wD ‘TIXR our RSUB ourpuT BYTE END x A s #4096 INPUT RLOOP INPUT AS EXIT BUFFER, X T RLOOP LENGTH XFL’ x ‘LENGTH ouTPUT WLOOP BUFFER, X ouTPUT T WLOOP x'05' FIRST CLEAR LOOP COUNTER CLEAR A TO ZERO CLEAR $ 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 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 Figure 2.5 Example of a SIC/XE program. System Software Line Loc Source statement Object code 5 0000 copy = starr 0 10 0000 FIRST STL RETADR 17202D 12 0003 * LDB WLENGTH 69202D 13 BASE LENGTH 15 0006. cLOOP = +JSUB RDREC 48101036 20 000A LDA LENGTH 032026 25 oop comp #0 290000 30 oo10 JEQ ENDFIL 332007 35 0013 +ISUB WRREC 4B10105D 40 oo17_ f! a CLOOP 3F2FEC 45 ooia Y gNppr, «LDA EOF 032010 50 001 STA BUFFER 02016 55 0020 LDA #3 010003 60 0023 STA LENGTH 0F200D 65 0026 45SUB | | WRREC 4B10105D 70 002A a @RETADR 3E2003 80 02D -zOF BYTE C’EOF’ 45446 95 0030 RETADR: RESW 1 100 0033 LENGTH «-RESW 1 105 0036 BUFFER RESB 4096 110 7 15 : SUBROUTINE TO READ RECORD INTO BUFFER 120 . 125 1036 RDREC CLEAR -X B410 130 1038 CLEAR A B400 132 103A CLEARS Bao 133 103¢ +L? #4096 75101000 135 1040 RLOOP. «TD INPUT #32019 140 1043 JEQ RLOOP 332FFA 145 1046 RD INPUT DB2013 150 1049 COMPR A,S A004 155 1048 SEQ EXIT 332008 160 104E STCH BUFFER, X 57C003 165 1051 TIXR OT Bg50 1701053 JLT —-RLOOP 3B2FEA 175-1056. EXIT. «= STX LENGTH 134000 / 180 1059 RSUB 4F0000 185 105C 9 INPUT. = BYTE. — XFL" FL 195 : 200 SUBROUTINE TO WRITE RECORD FROM BUFFER 210 05D REC CLEAR x B410 u ce re rt 220 a066 2 ouTPUT £32011 225 a EQ WLOOP 332FFA 20 toes LDCH BUFFER, X 530003 335 70 wp ourPuT DF2008 SE TIXR 240 1070 T B850 245 i073 qut woop 3B2FEF 250 io7 4F0000 Qos 6 OUTPUT BYTE x'05" 05 END FIRS? Figure 2.6 Program from Fig. 2.5 with object code. Assemblers 0090 coos | 48161096 | (+JSUB_ RDREC) 1036 | 841 }e-rorec 1076 : 5000 : soos | se10s036 |(+1suB ROREC) eas | a0 fe ROREC 6076 7420 7 4108466 | (+JSUB ROREC) le—RDREC (a) ©) ) Figure 2.7 Examples of program relocation. The command for the loader, of course, must also be a part of the object pro- gram. We can accomplish this with a Modification record having the following format: Modification record: Col. 1 M Col. 2-7 Starting location of the address field to be modified, rel- ative to the beginning of the program (hexadecimal) Col. 8-9 Length of the address field to be modified, in half- bytes (hexadecimal) The length is stored in half-bytes (rather than bytes) because the address field to be modified may not occupy an integral number of bytes. (For example, Assemblers Hor 000000001077 7000000.04 7202069202048101034032026290000332007.4n1010593F2FEGO32010 79000101:30F20160100030F2000481010599E20034 54846 7901036, DB4 198409844075 101000E32019332FFADB201 300433200857 60032850 7001053, D3B2FEAI 340004F0000F 184 1077400983201 1332FFAS3C003DF2004R850 790107007,3B2FEFAFOOOGOS 400000705 490001405 490002705 5900000 Figure 2.8 Object program corresponding to Fig. 2.6. addressing—if we were to attempt to relocate the program from Fig. 2.1, we would find that almost every instruction required modification. Figure 2.8 shows the complete object program corresponding to the source program of Fig. 2.5. Note that the Text records are exactly the same as those that would be produced by an absolute assembler (with program starting address of 0). However, the load addresses in the Text records are interpreted as relative, rather than absolute, locations. (The same is, of course, true of the addresses in the Modification and End records.) There is one Modification record for each address field that needs to be changed when the program is relocated (in this case, the three +JSUB instructions). You should verify these Modification records yourself and make sure you understand the contents of each. In Chapter 3 we consider in detail how the loader performs the required program modification. It is important that you understand the concepts involved now, however, because we build on these concepts in the next section. 2.3 MACHINE-INDEPENDENT ASSEMBLER FEATURES In this section, we discuss some common assembler features that are not closely related to machine architecture. Of course, more advanced machines tend to have more complex software; therefore the features we consider are more likely to be found on larger and more complex machines. However, the Presence or absence of such capabilities is much more closely related to issues such as programmer convenience and software environment than it is to machine architecture. In Section 2.3.1 we discuss the implementation of literals within an assem- bler, including the required data structures and processing logic. Section 2.3.2 67

You might also like