Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes Lecture 10 Machine language programs James Goodman (revised by Robert Sheehan) Credits: “McGraw-Hill” slides prepared by Gregory T. Byrd, North Carolina State University1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Using Branch Instructions Compute sum of 12 integers. Numbers start at location x3100. Program starts at location x3000. R1 x3100 R3 0 R2 12 R2=0? NO R4 M[R1] R3 R3+R4 R1 R1+1 R2 R2-1 YES 2 Sample Program Address Instruction Comments x3000 1 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1 R1 x3100 (PC+0x0FF) x3001 0 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0 R3 0 x3002 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 0 x3003 0 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0 R2 12 x3004 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 If Z, goto x300A (PC+5) x3005 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 Load next value to R4 x3006 0 0 0 1 0 1 1 0 1 1 0 0 0 1 0 0 Add to R3 x3007 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 Increment R1 (pointer) x3008 0 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1 Decrement R2 (counter) x3009 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 Goto x3004 (PC-6) 3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. JMP (Register) Jump is an unconditional branch -- always taken. Target address is the contents of a register. Allows any target address. We could have used this as the last instruction in the previous sample program, but we would have had to put the destination address into a register first. 4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. TRAP Calls a service routine, identified by 8-bit “trap vector.” vector routine x23 input a character from the keyboard x21 output a character to the monitor x25 halt the program When routine is done, PC is set to the instruction following TRAP. (We’ll talk about how this works later.) 5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Another Example Count the occurrences of a character in a file Program begins at location x3000 Read character from keyboard Load each character from a “file” • File is a sequence of memory locations • Starting address of file is stored in the memory location immediately after the program If file character equals input character, increment counter End of file is indicated by a special ASCII value: EOT (x04) At the end, print the number of characters and halt (assume there will be less than 10 occurrences of the character) A special character used to indicate the end of a sequence is often called a sentinel. Useful when you don’t know ahead of time how many times to execute a loop. 6 7 Program (1 of 2) 5-8 Address Instruction Comments x3000 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 0 (counter) x3001 0 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 R3 M[x3102] (ptr) x3002 1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 Input to R0 (TRAP x23) x3003 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1 M[R3] x3004 0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 R4 R1 – 4 (EOT) x3005 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 If Z, goto x300E x3006 1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1 R1 NOT R1 x3007 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 R1 R1 + 1 X3008 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 R1 R1 + R0 x3009 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 If N or P, goto x300B Program (2 of 2) 5-9 Address Instruction Comments x300A 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 R2 R2 + 1 x300B 0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1 R3 R3 + 1 x300C 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1 M[R3] x300D 0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 0 Goto x3004 x300E 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 R0 M[x3013] x300F 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 R0 R0 + R2 x3010 1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 Print R0 (TRAP x21) x3011 1 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1 HALT (TRAP x25) X3012 Starting Address of File x3013 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 ASCII x30 (‘0’) LC-3 Data Path Revisited Filled arrow = info to be processed. Unfilled arrow = control signal. 10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Data Path Components Global bus special set of wires that carry a 16-bit signal to many components inputs to the bus are “tri-state devices,” that only place a signal on the bus when they are enabled only one (16-bit) signal should be enabled at any time • control unit decides which signal “drives” the bus any number of components can read the bus • register only captures bus data if it is write-enabled by the control unit Memory Control and data registers for memory and I/O devices memory: MAR, MDR (also control signal for read/write) 11 Data Path Components ALU Accepts inputs from register file and from sign-extended bits from IR (immediate field). Output goes to bus. • used by condition code logic, register file, memory Register File Two read addresses (SR1, SR2), one write address (DR) Input from bus • result of ALU operation or memory read Two 16-bit outputs • used by ALU, PC, memory address • data for store instructions passes through ALU 5-12 Data Path Components PC and PCMUX Three inputs to PC, controlled by PCMUX 1. PC+1 – FETCH stage 2. Address adder – BR, JMP 3. bus – TRAP (discussed later) MAR and MARMUX 5-13 Two inputs to MAR, controlled by MARMUX 1. Address adder – LD/ST, LDR/STR 2. Zero-extended IR[7:0] -- TRAP (discussed later) Data Path Components Condition Code Logic Looks at value on bus and generates N, Z, P signals Registers set only when control unit enables them (LD.CC) • only certain instructions set the codes (ADD, AND, NOT, LD, LDI, LDR, LEA) Control Unit – Finite State Machine On each machine cycle, changes control signals for next phase of instruction processing • who drives the bus? (GatePC, GateALU, …) • which registers are write enabled? (LD.IR, LD.REG, …) • which operation should ALU perform? (ALUK) •… Logic includes decoder for opcode, etc. 5-14 Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes Lecture 11 Chapter 7: Assembly Language James Goodman (revised by Robert Sheehan) Credits: Slides prepared by Gregory T. Byrd, North Carolina State University 15 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Human-Readable Machine Language Computers like ones and zeros… 0001110010000110 Humans like symbols… ADD R6,R2,R6 ; increment index reg. Assembler is a program that turns symbols into machine instructions. ISA-specific: close correspondence between symbols and instruction set • mnemonics for opcodes • labels for memory locations additional operations for allocating storage and initializing data 16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. LC-3 Assembly Language Syntax Each line of a program is one of the following: an instruction an assembler directive (or pseudo-op) a comment Whitespace (between symbols) and case are ignored. Comments (beginning with “;”) are also ignored. An instruction has the following format: LABEL OPCODE OPERANDS ; COMMENTS optional mandatory 17 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Opcodes and Operands Opcodes reserved symbols that correspond to LC-3 instructions listed in Appendix A • ex: ADD, AND, LD, LDR, … Operands registers -- specified by Rn, where n is the register number numbers -- indicated by # (decimal) or x (hex) or b (binary) label -- symbolic name of memory location separated by comma number, order, and type correspond to instruction format • ex: ADD R1,R1,R3 ADD R1,R1,#3 LD R6,NUMBER BRz LOOP 18 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Labels and Comments Label placed at the beginning of the line assigns a symbolic name to the address corresponding to line • ex: LOOP ADD R1,R1,#-1 BRp LOOP Comment anything after a semicolon is a comment ignored by assembler used by humans to document/understand programs tips for useful comments: • avoid restating the obvious, as “decrement R1” • provide additional insight, as in “accumulate product in R6” • use comments to separate pieces of program 19 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Assembler Directives Pseudo-operations do not refer to operations executed by program used by assembler look like instruction, but “opcode” starts with a full stop Opcode Operand Meaning .ORIG address starting address of program end of program .END .BLKW n allocate n words of storage .FILL n allocate one word, initialize with value n .STRINGZ n-character string allocate n+1 locations, initialize w/characters and null terminator 20 Hello World in LC-3 Assembler ; Hello world in LC-3 assembler ; I could have just used "PUTS" but that isn't fun. .ORIG LEA loop LDR BRz TRAP ADD BRnzp finish TRAP hello x3000 R1, hello R0, R1, #0 finish x21 R1, R1, #1 loop x25 ; R1 points to next character ; R0 holds next character ; or just OUT prints R0[7:0] ; or HALT .STRINGZ "Hello world" .END 21 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Trap Codes LC-3 assembler provides “pseudo-instructions” for each trap code, so you don’t have to remember them. Code Equivalent Description HALT TRAP x25 Halt execution and print message to console. IN TRAP x23 Print prompt on console, read (and echo) one character from keybd. Character stored in R0[7:0]. OUT TRAP x21 Write one character (in R0[7:0]) to console. GETC TRAP x20 Read one character from keyboard. Character stored in R0[7:0]. PUTS TRAP x22 Write null-terminated string to console. Address of string is in R0. 22 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Style Guidelines Use the following style guidelines to improve the readability and understandability of your programs: 1. Provide a program header, with author’s name, date, etc., and purpose of program. 2. Start labels, opcode, operands, and comments in same column for each line. (Unless entire line is a comment.) 3. Use comments to explain what each register does. 4. Give explanatory comment for most instructions. 5. Use meaningful symbolic names. • Mixed upper and lower case for readability. • ASCIItoBinary, InputRoutine, SaveR1 6. Provide comments between program sections. 7. Each line must fit on the page -- no wraparound or truncations. • Long statements split in aesthetically pleasing manner. 23 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sample Program Count the occurrences of a character in a file. OUTPUT: TEST: BRz OUTPUT 24 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04) BRz OUTPUT ; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; If no match, do not increment ADD R2, R2, #1 ; ; Get next character from file. ; GETCHAR ADD R3, R3, #1 ; Point to next character. LDR R1, R3, #0 ; R1 gets next char to test BRnzp TEST ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 25 Do this Download the LC‐3 simulator package from the resources page <http://www.cs.auckland.ac.nz/compsci210s1c/resources/>. For running on Windows, read the document LC3WinGuide.pdf. (You may run the simulator under Linux: read the document LC3_unix.pdf). Follow the instructions for running a program, creating the files described in the example and execute the program. Create a source file from the text of program discussed in the lecture (figures 5.16 & 7.2 in the book). Create a “file” starting in the memory at location x4000. Assemble the programme. Execute the programme, typing different characters and make sure the programme prints the correct result. What goes wrong if the character you enter occurs more than 10 times in the file? 26 Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes Lecture 12 The Assembly Process James Goodman (revised by Robert Sheehan) Credits: Slides prepared by Gregory T. Byrd, North Carolina State University 27 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. First Pass: Constructing the Symbol Table 1. Find the .ORIG statement, which tells us the address of the first instruction. • Initialize location counter (LC), which keeps track of the current instruction. 2. For each non-empty line in the program: a) If line contains a label, add label and LC to symbol table. b) Increment LC. – NOTE: If statement is .BLKW or .STRINGZ, increment LC by the number of words allocated. 3. Stop when .END statement is reached. NOTE: A line that contains only a comment is considered an empty line. 28 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address 29 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04) BRz OUTPUT ; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; If no match, do not increment ADD R2, R2, #1 ; ; Get next character from file. ; GETCHAR ADD R3, R3, #1 ; Point to next character. LDR R1, R3, #0 ; R1 gets next char to test BRnzp TEST ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 30 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol PTR Address ? 31 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD BRz OUTPUT ; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; If no match, do not increment ADD R2, R2, #1 ; ; Get next character from file. ; GETCHAR ADD R3, R3, #1 ; Point to next character. LDR R1, R3, #0 ; R1 gets next char to test BRnzp TEST ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 32 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 33 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD OUTPUT ; If done, prepare the output 0x3005 BRz ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; If no match, do not increment ADD R2, R2, #1 ; ; Get next character from file. ; GETCHAR ADD R3, R3, #1 ; Point to next character. LDR R1, R3, #0 ; R1 gets next char to test BRnzp TEST ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 34 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 OUTPUT ? 35 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD OUTPUT ; If done, prepare the output 0x3005 BRz ; ; Test character for match. If a match, increment count. ; R1, R1 0x3006 NOT R1, R1, R0 ; If match, R1 = xFFFF 0x3007 ADD R1, R1 ; If match, R1 = x0000 0x3008 NOT GETCHAR ; If no match, do not increment 0x3009 BRnp ADD R2, R2, #1 ; ; Get next character from file. ; GETCHAR ADD R3, R3, #1 ; Point to next character. LDR R1, R3, #0 ; R1 gets next char to test BRnzp TEST ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 36 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 OUTPUT ? GETCHAR ? 37 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD OUTPUT ; If done, prepare the output 0x3005 BRz ; ; Test character for match. If a match, increment count. ; R1, R1 0x3006 NOT R1, R1, R0 ; If match, R1 = xFFFF 0x3007 ADD R1, R1 ; If match, R1 = x0000 0x3008 NOT GETCHAR ; If no match, do not increment 0x3009 BRnp R2, R2, #1 0x300A ADD ; ; Get next character from file. ; GETCHAR 0x300B ADD R3, R3, #1 ; Point to next character. LDR R1, R3, #0 ; R1 gets next char to test BRnzp TEST ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 38 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 OUTPUT ? GETCHAR ?0x300B 39 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD OUTPUT ; If done, prepare the output 0x3005 BRz ; ; Test character for match. If a match, increment count. ; R1, R1 0x3006 NOT R1, R1, R0 ; If match, R1 = xFFFF 0x3007 ADD R1, R1 ; If match, R1 = x0000 0x3008 NOT GETCHAR ; If no match, do not increment 0x3009 BRnp R2, R2, #1 0x300A ADD ; ; Get next character from file. ; GETCHAR 0x300B ADD R3, R3, #1 ; Point to next character. R1, R3, #0 ; R1 gets next char to test 0x300C LDR TEST 0x300D BRnzp ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 40 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 OUTPUT ? GETCHAR 0x300B 41 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 OUTPUT ? GETCHAR 0x300B 42 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD OUTPUT ; If done, prepare the output 0x3005 BRz ; ; Test character for match. If a match, increment count. ; R1, R1 0x3006 NOT R1, R1, R0 ; If match, R1 = xFFFF 0x3007 ADD R1, R1 ; If match, R1 = x0000 0x3008 NOT GETCHAR ; If no match, do not increment 0x3009 BRnp R2, R2, #1 0x300A ADD ; ; Get next character from file. ; GETCHAR 0x300B ADD R3, R3, #1 ; Point to next character. R1, R3, #0 ; R1 gets next char to test 0x300C LDR TEST 0x300D BRnzp ; ; Output the count. ; OUTPUT 0x300E LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 43 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 OUTPUT ? 0x300E GETCHAR 0x300B 44 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD OUTPUT ; If done, prepare the output 0x3005 BRz ; ; Test character for match. If a match, increment count. ; R1, R1 0x3006 NOT R1, R1, R0 ; If match, R1 = xFFFF 0x3007 ADD R1, R1 ; If match, R1 = x0000 0x3008 NOT GETCHAR ; If no match, do not increment 0x3009 BRnp R2, R2, #1 0x300A ADD ; ; Get next character from file. ; GETCHAR 0x300B ADD R3, R3, #1 ; Point to next character. R1, R3, #0 ; R1 gets next char to test 0x300C LDR TEST 0x300D BRnzp ; ; Output the count. ; OUTPUT 0x300E LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END 45 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 OUTPUT 0x300E GETCHAR 0x300B ASCII ? 46 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD OUTPUT ; If done, prepare the output 0x3005 BRz ; ; Test character for match. If a match, increment count. ; R1, R1 0x3006 NOT R1, R1, R0 ; If match, R1 = xFFFF 0x3007 ADD R1, R1 ; If match, R1 = x0000 0x3008 NOT GETCHAR ; If no match, do not increment 0x3009 BRnp R2, R2, #1 0x300A ADD ; ; Get next character from file. ; GETCHAR 0x300B ADD R3, R3, #1 ; Point to next character. R1, R3, #0 ; R1 gets next char to test 0x300C LDR TEST 0x300D BRnzp ; ; Output the count. ; OUTPUT 0x300E LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII 0x300F ; ASCII code in R0 is displayed. 0x3010 OUT ; Halt machine 0x3011 HALT ; ; Storage for pointer and ASCII template ; ASCII 0x3012 .FILL x0030 PTR .FILL x4000 .END 47 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? TEST 0x3004 OUTPUT 0x300E GETCHAR 0x300B ASCII ?0x3012 48 ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization 0x3000 0x3001 0x3002 0x3003 .ORIG AND LD GETC LDR x3000 R2, R2, #0 R3, PTR ; ; ; ; R2 R3 R0 R1 is counter, initially 0 is pointer to characters gets character input gets first character R1, R3, #0 ; ; Test character for end of file ; TEST R4, R1, #-4 ; Test for EOT (ASCII x04) 0x3004 ADD OUTPUT ; If done, prepare the output 0x3005 BRz ; ; Test character for match. If a match, increment count. ; R1, R1 0x3006 NOT R1, R1, R0 ; If match, R1 = xFFFF 0x3007 ADD R1, R1 ; If match, R1 = x0000 0x3008 NOT GETCHAR ; If no match, do not increment 0x3009 BRnp R2, R2, #1 0x300A ADD ; ; Get next character from file. ; GETCHAR 0x300B ADD R3, R3, #1 ; Point to next character. R1, R3, #0 ; R1 gets next char to test 0x300C LDR TEST 0x300D BRnzp ; ; Output the count. ; OUTPUT 0x300E LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary count to ASCII 0x300F ; ASCII code in R0 is displayed. 0x3010 OUT ; Halt machine 0x3011 HALT ; ; Storage for pointer and ASCII template ; ASCII 0x3012 .FILL x0030 PTR x4000 0x3013 .FILL .END 49 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Construction Construct the symbol table for the program in Figure 7.1 . Symbol Address PTR ? 0x3013 TEST 0x3004 OUTPUT 0x300E GETCHAR 0x300B ASCII 0x3012 50 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Second Pass: Generating Machine Language For each executable assembly language statement, generate the corresponding machine language instruction. If operand is a label, look up the address from the symbol table. Potential problems: Improper number or type of arguments • ex: NOT R1,#7 ADD R1,R2 ADD R3,R3,NUMBER Immediate argument too large • ex: ADD R1,R2,#1023 Address (associated with label) more than 256 from instruction • can’t use PC-relative addressing mode 51 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Practice Using the symbol table constructed earlier, translate these statements into LC-3 machine language. Statement LD R3,PTR ADD R4,R1,#-4 LDR R1,R3,#0 Machine Language BRnp GETCHAR 52 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Practice Practice Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Using the symbol table constructed earlier, translate these table statements into LC-3 machine language. Using the symbol constructed earlier, translate these statements into LC-3 machine language. Statement LD R3,PTR ADD R4,R1,#-4 LDR R1,R3,#0 BRnp GETCHAR Machine Language 0010 0110 0001 0001 0001 1000 0111 1100 0110 0010 1100 0000 0000 1010 0000 0001 53 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. LC-3 Assembler Using “assemble” (Unix) or LC3Edit (Windows), generates several different output files. This one gets loaded into the simulator. 54 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object File Format LC-3 object file contains Starting address (location where program must be loaded), followed by… Machine instructions Example Beginning of “count character” object file looks like this: .ORIG x3000 0011000000000000 AND R2, R2, #0 0101010010100000 LD R3, PTR 0010011000010001 1111000000100011 TRAP x23 . . . 55 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Multiple Object Files An object file is not necessarily a complete program. system-provided library routines code blocks written by multiple developers For LC-3 simulator, can load multiple object files into memory, then start executing at a desired address. system routines, such as keyboard input, are loaded automatically • loaded into “system memory,” below x3000 • user code should be loaded between x3000 and xFDFF each object file includes a starting address be careful not to load overlapping object files 56 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Linking and Loading Loading is the process of copying an executable image into memory. more sophisticated loaders are able to relocate images to fit into available memory must readjust branch targets, load/store addresses Linking is the process of resolving symbols between independent object files. suppose we define a symbol in one module, and want to use it in another some notation, such as .EXTERNAL, is used to tell assembler that a symbol is defined in another module linker will search symbol tables of other modules to resolve symbols and complete code generation before loading 57 News from the NYTimes (18 June 1996) “When a computer runs out of [main memory], modern operating systems automatically use the memory on the hard drive. But today’s hard drives retrieve data at speeds of about 10 milliseconds (millionths of a second). That seems fast until you consider that modern RAM can do this at 60 nanoseconds (billionths of a second), more than 150 times as fast.” What’s wrong with this statement?? 58 “Correction: June 19, 1996, Wednesday The Personal Computers column in Science Times yesterday, about options for increasing computer memory, incorrectly described milliseconds (they are thousandths of a second, not millionths) and therefore misstated the difference in speed between random access memory (RAM) and hard drives. RAM can be more than 100,000 times, not 150 times, as fast.” 59 Time Line 1 second 10-10 10-9 1 minute 10-8 10-7 1 hour 10-6 10-5 1 day 10-4 1 month 10-3 10-2 1 year 10-1 100 Time (Logarithmic Scale) Scale by 31,557,600 60 Speed Line Execute one instruction (best case) One clock period 2 GHz Cache miss time (Memory access time) Transfer 1 char at 56K baud Cache hit time Read 1 byte from disk 10-10 10-9 10-8 10-7 10-6 10-5 10-4 One disk revolution (6-8 ms) Total Disk access time 10-3 10-2 10-1 100 Time (Logarithmic Scale) Time for light to travel 30 cm Time for sound to travel 30 cm 61