Post on 23-Dec-2015
Computer Science 210 s1c
Computer Systems 12014 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
Using Branch Instructions
Compute sum of 12 integers.Numbers start at location x3100. Program starts at location x3000.
R1 x3100R3 0
R2 12
R2=0?
R4 M[R1]R3 R3+R4R1 R1+1R2 R2-1
NO
YES
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Sample Program
3
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)
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
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.)
vector routine
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
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 sequenceis often called a sentinel.
Useful when you don’t know ahead of time how many timesto execute a loop.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7
5-8
Program (1 of 2)
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
5-9
Program (2 of 2)
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’)
10
Filled arrow = info to be processed.
Unfilled arrow= control signal.
LC-3 Data PathRevisited
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.
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
5-13
Data Path Components
PC and PCMUX Three inputs to PC, controlled by PCMUX
1. PC+1 – FETCH stage2. Address adder – BR, JMP3. bus – TRAP (discussed later)
MAR and MARMUX Two inputs to MAR, controlled by MARMUX
1. Address adder – LD/ST, LDR/STR2. Zero-extended IR[7:0] -- TRAP (discussed later)
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
phaseof 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.
Computer Science 210 s1c
Computer Systems 12014 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
Human-Readable Machine Language
Computers like ones and zeros…
Humans like symbols…
Assembler is a program that turns symbols intomachine 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
ADD R6,R2,R6 ; increment index reg.
0001110010000110
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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
optional mandatory
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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:ADD R1,R1,R3ADD R1,R1,#3LD R6,NUMBERBRz LOOP
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
19
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,#-1BRp 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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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
Opcode Operand Meaning
.ORIG address starting address of program
.END end of program
.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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Hello World in LC-3 Assembler
; Hello world in LC-3 assembler; I could have just used "PUTS" but that isn't fun.
.ORIG x3000LEA R1, hello ; R1 points to next character
loop LDR R0, R1, #0 ; R0 holds next characterBRz finishTRAP x21 ; or just OUT prints R0[7:0]ADD R1, R1, #1BRnzp loop
finish TRAP x25 ; or HALT
hello .STRINGZ "Hello world".END
21
22
Trap Codes
LC-3 assembler provides “pseudo-instructions” foreach 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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
23
Style Guidelines
Use the following style guidelines to improvethe 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
columnfor 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
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
; 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 x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
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 12014 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
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
thecurrent 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
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol Address
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x3001 PTR
31
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x3004TEST
33
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
TEST 0x3004
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x30040x3005 OUTPUT
35
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
OUTPUT ?
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x30040x3005
0x30060x30070x30080x3009 GETCHAR
37
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
OUTPUT ?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
GETCHAR ?
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x30040x3005
0x30060x30070x30080x30090x300A
0x300BGETCHAR
39
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
OUTPUT ?
GETCHAR ?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
0x300B
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x30040x3005
0x30060x30070x30080x30090x300A
0x300B0x300C0x300D TEST
41
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
OUTPUT ?
GETCHAR 0x300B
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
42
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
OUTPUT ?
GETCHAR 0x300B
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x30040x3005
0x30060x30070x30080x30090x300A
0x300B0x300C0x300D
0x300EOUTPUT
44
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
OUTPUT ?
GETCHAR 0x300B
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
0x300E
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x30040x3005
0x30060x30070x30080x30090x300A
0x300B0x300C0x300D
0x300E ASCII
46
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
OUTPUT 0x300E
GETCHAR 0x300B
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ASCII ?
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x30040x3005
0x30060x30070x30080x30090x300A
0x300B0x300C0x300D
0x300E0x300F0x30100x3011
0x3012ASCII
48
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
OUTPUT 0x300E
GETCHAR 0x300B
ASCII ?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
0x3012
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;
.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character
;; 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, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD 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 testBRnzp TEST
;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template
ADD R0, R0, R2 ; Convert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine
;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000
.END
0x30000x30010x30020x3003
0x30040x3005
0x30060x30070x30080x30090x300A
0x300B0x300C0x300D
0x300E0x300F0x30100x3011
0x30120x3013PTR
50
Symbol Table Construction
Construct the symbol table for the program in Figure 7.1 .
Symbol AddressPTR ?
TEST 0x3004
OUTPUT 0x300E
GETCHAR 0x300B
ASCII 0x3012
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
0x3013
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: NOT R1,#7ADD R1,R2ADD 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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
Statement Machine LanguageLD R3,PTR
ADD R4,R1,#-4
LDR R1,R3,#0
BRnp GETCHAR
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
LC-3 Assembler
Using “assemble” (Unix) or LC3Edit (Windows),generates several different output files.
This one getsloaded into thesimulator.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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:
0011000000000000010101001010000000100110000100011111000000100011
.
.
.
.ORIG x3000
AND R2, R2, #0
LD R3, PTR
TRAP x23
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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
Linking and Loading
Loading is the process of copying an executable imageinto memory.
more sophisticated loaders are able to relocate imagesto fit into available memory
must readjust branch targets, load/store addresses
Linking is the process of resolving symbols betweenindependent 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
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
“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
Time Line
10-10 10-7 10010-9 10-8 10-6 10-5 10-4 10-3 10-2 10-1
Mill
isec
ond
Mic
rose
cond
Nan
osec
ond
Seco
nd
Time (Logarithmic Scale)
1 month1 day1 hour1 minute1 second 1 year
Mic
roCen
tury
Nan
oCen
tury
Cen
tury
Mill
iCen
tury
Scale by 31,557,600
61
Speed Line
Time for light to travel 30 cm
One clock period2 GHz
Total Diskaccess time
Cache miss time(Memory access time)
Cache hit time
Executeone
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 10010-9 10-8 10-6 10-5 10-4 10-3 10-2 10-1
Mill
isec
ond
Mic
rose
cond
Nan
osec
ond
Seco
nd
Time (Logarithmic Scale)