Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes James Goodman (revised by Robert Sheehan) Credits: “McGraw-Hill” slides prepared.

Similar presentations


Presentation on theme: "Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes James Goodman (revised by Robert Sheehan) Credits: “McGraw-Hill” slides prepared."— Presentation transcript:

1 Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes James Goodman (revised by Robert Sheehan) Credits: “McGraw-Hill” slides prepared by Gregory T. Byrd, North Carolina State University Machine language programs Lecture 10 1

2 2 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? R4  M[R1] R3  R3+R4 R1  R1+1 R2  R2-1 NO YES Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

3 Sample Program 3 AddressInstructionComments x30001 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1 R1  x3100 (PC+0x0FF) x30010 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0 R3  0 x30020 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2  0 x30030 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0 R2  12 x30040 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 If Z, goto x300A (PC+5) x30050 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 Load next value to R4 x30060 0 0 1 0 1 1 0 1 1 0 0 0 1 0 0 Add to R3 x30070 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 Increment R1 (pointer) x30080 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1 Decrement R2 (counter) x30090 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 Goto x3004 (PC-6)

4 4 JMP (Register) Jump is an unconditional branch -- always taken.  Target address is the contents of a register.  Allows any target address. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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.

5 5 TRAP Calls a service routine, identified by 8-bit “trap vector.” When routine is done, PC is set to the instruction following TRAP. (We’ll talk about how this works later.) vectorroutine x23 input a character from the keyboard x21 output a character to the monitor x25 halt the program Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

6 6 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. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

7 7

8 5-8 Program (1 of 2) AddressInstructionComments x30000 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2  0 (counter) x30010 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 R3  M[x3102] (ptr) x30021 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 Input to R0 (TRAP x23) x30030 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1  M[R3] x30040 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 R4  R1 – 4 (EOT) x30050 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 If Z, goto x300E x30061 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1 R1  NOT R1 x30070 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 R1  R1 + 1 X30080 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 R1  R1 + R0 x30090 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 If N or P, goto x300B

9 5-9 Program (2 of 2) AddressInstructionComments x300A0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 R2  R2 + 1 x300B0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1 R3  R3 + 1 x300C0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1  M[R3] x300D0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 0 Goto x3004 x300E0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 R0  M[x3013] x300F0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 R0  R0 + R2 x30101 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 Print R0 (TRAP x21) x30111 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1 HALT (TRAP x25) X3012Starting Address of File x30130 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 ASCII x30 (‘0’)

10 10 Filled arrow = info to be processed. Unfilled arrow = control signal. LC-3 Data Path Revisited

11 11 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) Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

12 5-12 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

13 5-13 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  Two inputs to MAR, controlled by MARMUX 1.Address adder – LD/ST, LDR/STR 2.Zero-extended IR[7:0] -- TRAP (discussed later)

14 5-14 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.

15 Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes James Goodman (revised by Robert Sheehan) Credits: Slides prepared by Gregory T. Byrd, North Carolina State University Chapter 7: Assembly Language 15 Lecture 11

16 16 Human-Readable Machine Language Computers like ones and zeros… Humans like symbols… 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 ADDR6,R2,R6; increment index reg. 0001110010000110 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

17 17 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 optionalmandatory Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

18 18 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: ADDR1,R1,R3 ADDR1,R1,#3 LDR6,NUMBER BRzLOOP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

19 19 Labels and Comments Label  placed at the beginning of the line  assigns a symbolic name to the address corresponding to line ex: LOOPADDR1,R1,#-1 BRpLOOP 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

20 20 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 OpcodeOperandMeaning.ORIG addressstarting address of program.END end of program.BLKW nallocate n words of storage.FILL nallocate one word, initialize with value n.STRINGZ n-character string allocate n+1 locations, initialize w/characters and null terminator Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

21 Hello World in LC-3 Assembler ; Hello world in LC-3 assembler ; I could have just used "PUTS" but that isn't fun..ORIG x3000 LEAR1, hello; R1 points to next character loopLDRR0, R1, #0; R0 holds next character BRzfinish TRAPx21; or just OUT prints R0[7:0] ADDR1, R1, #1 BRnzploop finishTRAPx25; or HALT hello.STRINGZ "Hello world".END 21

22 22 Trap Codes LC-3 assembler provides “pseudo-instructions” for each trap code, so you don’t have to remember them. CodeEquivalentDescription HALTTRAP x25 Halt execution and print message to console. INTRAP x23 Print prompt on console, read (and echo) one character from keybd. Character stored in R0[7:0]. OUTTRAP x21 Write one character (in R0[7:0]) to console. GETCTRAP x20 Read one character from keyboard. Character stored in R0[7:0]. PUTSTRAP x22 Write null-terminated string to console. Address of string is in R0. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

23 23 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. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

24 24 Sample Program Count the occurrences of a character in a file. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. TEST: BRz OUTPUT OUTPUT:

25 25 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END

26 Do this Download the LC ‐ 3 simulator package from the resources page. 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

27 Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes James Goodman (revised by Robert Sheehan) Credits: Slides prepared by Gregory T. Byrd, North Carolina State University 27 Lecture 12 The Assembly Process

28 28 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. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

29 29 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

30 30 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 PTR

31 31 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

32 32 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 TEST

33 33 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. TEST 0x3004

34 34 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005 OUTPUT

35 35 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. OUTPUT ?

36 36 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005 0x3006 0x3007 0x3008 0x3009 GETCHAR

37 37 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 OUTPUT? Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. GETCHAR ?

38 38 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005 0x3006 0x3007 0x3008 0x3009 0x300A 0x300B GETCHAR

39 39 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 OUTPUT? GETCHAR? Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 0x300B

40 40 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005 0x3006 0x3007 0x3008 0x3009 0x300A 0x300B 0x300C 0x300D TEST

41 41 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 OUTPUT? GETCHAR0x300B Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

42 42 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 OUTPUT? GETCHAR0x300B Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

43 43 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005 0x3006 0x3007 0x3008 0x3009 0x300A 0x300B 0x300C 0x300D 0x300E OUTPUT

44 44 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 OUTPUT? GETCHAR0x300B Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 0x300E

45 45 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005 0x3006 0x3007 0x3008 0x3009 0x300A 0x300B 0x300C 0x300D 0x300E ASCII

46 46 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 OUTPUT0x300E GETCHAR0x300B Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ASCII ?

47 47 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005 0x3006 0x3007 0x3008 0x3009 0x300A 0x300B 0x300C 0x300D 0x300E 0x300F 0x3010 0x3011 0x3012 ASCII

48 48 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 OUTPUT0x300E GETCHAR0x300B ASCII? Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 0x3012

49 49 ; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Convert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005 0x3006 0x3007 0x3008 0x3009 0x300A 0x300B 0x300C 0x300D 0x300E 0x300F 0x3010 0x3011 0x3012 0x3013 PTR

50 50 Symbol Table Construction Construct the symbol table for the program in Figure 7.1. SymbolAddress PTR? TEST0x3004 OUTPUT0x300E GETCHAR0x300B ASCII0x3012 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 0x3013

51 51 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: NOTR1,#7 ADDR1,R2 ADDR3,R3,NUMBER  Immediate argument too large ex: ADDR1,R2,#1023  Address (associated with label) more than 256 from instruction can’t use PC-relative addressing mode Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

52 52 Practice Using the symbol table constructed earlier, translate these statements into LC-3 machine language. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. StatementMachine Language LD R3,PTR ADD R4,R1,#-4 LDR R1,R3,#0 BRnp GETCHAR

53 53 Practice Using the symbol table constructed earlier, translate these statements into LC-3 machine language. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

54 54 LC-3 Assembler Using “assemble” (Unix) or LC3Edit (Windows), generates several different output files. This one gets loaded into the simulator. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

55 55 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: 0011000000000000 0101010010100000 0010011000010001 1111000000100011....ORIG x3000 AND R2, R2, #0 LD R3, PTR TRAP x23 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

56 56 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

57 57 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

58 58 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??

59 59 “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.”

60 60 Time Line 10 -10 10 -7 10 0 10 -9 10 -8 10 -6 10 -5 10 -4 10 -3 10 -2 10 -1 Millisecond Microsecond Nanosecond Second Time (Logarithmic Scale) 1 month1 day1 hour1 minute1 second1 year MicroCentury NanoCentury Century MilliCentury Scale by 31,557,600

61 61 Speed Line Time for light to travel 30 cm One clock period 2 GHz Total Disk access time Cache miss time (Memory access time) Cache hit time Execute one instruction (best case) Time for sound to travel 30 cm One disk revolution (6-8 ms) Transfer 1 char at 56K baud Read 1 byte from disk 10 -10 10 -7 10 0 10 -9 10 -8 10 -6 10 -5 10 -4 10 -3 10 -2 10 -1 Millisecond Microsecond Nanosecond Second Time (Logarithmic Scale)


Download ppt "Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes James Goodman (revised by Robert Sheehan) Credits: “McGraw-Hill” slides prepared."

Similar presentations


Ads by Google