Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2422 Assembly Language & System Programming November 30, 2006.

Similar presentations


Presentation on theme: "CS2422 Assembly Language & System Programming November 30, 2006."— Presentation transcript:

1 CS2422 Assembly Language & System Programming November 30, 2006

2 Today’s Topic Assembler: Basic Functions –Section 2.1 of Beck’s “System Software” book. Reading Assignment: pages 43-52.

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

4 Example Program Fig. 2.1 (1/3) LineSource statement 5COPYSTART1000 10FIRSTSTLRETADRSAVE RETURN ADDRESS 15CLOOPJSUBRDRECREAD INPUT RECORD 20LDALENGTHTEST FOR EOF (LENGTH = 0) 25COMPZERO 30JEQENDFILEXIT IF EOF FOUND 35JSUBWRRECWRITE OUTPUT RECORD 40JCLOOPLOOP 45ENDFILLDAEOFINSERT END OF FILE MARKER 50STABUFFER 55LDATHREESET LENGTH = 3 60STALENGTH 65JSUBWRRECWRITE EOF 70LDLRETADRGET RETURN ADDRESS 75RSUBRETURN TO CALLER 80EOFBYTEC’EOF’ 85THREEWORD3 90ZEROWORD0 95RETADRRESW1 100LENGTHRESW1 105BUFFERRESB40964096-BYTE BUFFER AREA

5 110. 115.SUBROUTINE TO READ RECORD INTO BUFFER 120. 125RDRECLDXZEROCLEAR LOOP COUNTER 130LDAZEROCLEAR A TO ZERO 135RLOOPTDINPUTTEST INPUT DEVICE 140JEQRLOOPLOOP UNTIL READY 145RDINPUT READ CHARACTER INTO REGISTER A 150COMPZEROTEST FOR END OF RECORD (X’00’) 155JEQEXITEXIT LOOP IF EOR 160STCHBUFFER,XSTORE CHARACTER IN BUFFER 165TIXMAXLENLOOP UNLESS MAX LENGTH 170JLTRLOOP HAS BEEN REACHED 175EXITSTXLENGTHSAVE RECORD LENGTH 180RSUBRETURN TO CALLER 185INPUTBYTEX’F1’CODE FOR INPUT DEVICE 190MAXLENWORD4096 195. 200.SUBROUTINE TO WRITE RECORD FROM BUFFER 205. 210WRRECLDXZEROCLEAR LOOP COUNTER 215WLOOPTDOUTPUTTEST OUTPUT DEVICE 220JEQWLOOPLOOP UNTIL READY 225LDCHBUFFER,XGET CHARACTER FROM BUFFER 230WDOUTPUTWRITE CHARACTER 235TIXLENGTHLOOP UNTIL ALL CHARACTERS 240JLTWLOOP HAVE BEEN WRITTEN 245RSUBRETURN TO CALLER 250OUTPUTBYTEX’05’CODE FOR OUTPUT DEVICE 255ENDFIRST

6 Example Program Fig. 2.1 (2/3) Purpose –Reads records from input device (code F1) –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

7 Example Program Fig. 2.1 (3/3) 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 (00 16 ) –The end of the file is indicated by a zero-length record Subroutines (JSUB, RSUB) –RDREC, WRREC –Save link register first before nested jump

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

9 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.

10 Example Program with Object Code LineLoc Source statementObject code 51000COPYSTART1000 101000FIRSTSTLRETADR141033 151003CLOOPJSUBRDREC482039 201006LDALENGTH001036 251009COMPZERO281030 30100CJEQENDFIL301015 35100FJSUBWRREC482061 401012JCLOOP3C1003 451015ENDFILLDAEOF00102A 501018STABUFFER0C1039 55101BLDATHREE00102D 60101ESTALENGTH0C1036 651021JSUBWRREC482061 701024LDLRETADR081033 751027RSUB4C0000 80102AEOFBYTEC’EOF’454F46 85102DTHREEWORD3000003 901030ZEROWORD0000000 951033RETADRRESW1 1001036LENGTHRESW1 1051039BUFFERRESB4096

11 110. 115.SUBROUTINE TO READ RECORD INTO BUFFER 120. 1252039RDRECLDXZERO041030 130203CLDAZERO001030 135203FRLOOPTDINPUTE0205D 1402042JEQRLOOP30203D 1452045RDINPUT D8205D 1502048COMPZERO281030 155204BJEQEXIT302057 160204ESTCHBUFFER,X549039 1652051TIXMAXLEN2C205E 1702054JLTRLOOP38203F 1752057EXITSTXLENGTH101036 180205ARSUB4C0000 185205DINPUTBYTEX’F1’F1 190205EMAXLENWORD4096001000 195. 200.SUBROUTINE TO WRITE RECORD FROM BUFFER 205. 2102061WRRECLDXZERO041030 2152064WLOOPTDOUTPUTE02079 2202067JEQWLOOP302064 225206ALDCHBUFFER,X509039 230206DWDOUTPUTDC2079 2352070TIXLENGTH2C1036 2402073JLTWLOOP382064 2452076RSUB4C0000 2502079OUTPUTBYTEX’05’05 255ENDFIRST

12 SIC Instruction Set (Review) 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.

13 SIC Instruction Format (again) Opcode: 8 bits Address: one bit flag (x) and 15 bits of address OPCODEXAddress 8115

14 Examples Mnemonic code (or instruction name)  opcode. Examples: STL 1033  opcode 14 10 33 LDA 1036  opcode 00 10 36 0001 01000001 0000 0011 0011 0000 0001 0000 0011 0110

15 Symbolic Operands We’re 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

16 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. COPYSTART 1000 … LDALEN … LENRESW1

17 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

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

19 Two Pass Assembler – Pass 1

20 Two Pass Assembler – Pass 2

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

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

23 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 COPY1000 FIRST 1000 CLOOP1003 ENDFIL1015 EOF1024 THREE102D ZERO1030 RETADR1033 LENGTH1036 BUFFER1039 RDREC2039

24 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 in the future (Section 2.4.1)

25 Object Program Header Col. 1 H Col. 2~7 Program name Col. 8~13 Starting address (hex) Col. 14-19 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 (69-10+1)/6=10 instructions End Col.1 E Col.2~7 Address of first executable instruction (hex) (END program_name)

26 Fig. 2.3 H COPY 001000 00107A T 001000 1E 141033 482039 001036 281030 301015 482061... T 00101E 15 0C1036 482061 081044 4C0000 454F46 000003 000000 T 002039 1E 041030 001030 E0205D 30203F D8205D 281030 … T 002057 1C 101036 4C0000 F1 001000 041030 E02079 302064 … T 002073 07 382064 4C0000 05 E 001000  starting address


Download ppt "CS2422 Assembly Language & System Programming November 30, 2006."

Similar presentations


Ads by Google