Processor
Processor
SYLLABUS
Module 0: (Review) Computing Systems (6) From Bits and Gates to C and Beyond Module 1: Architecture of Microprocessors (6) General definitions of mini computers, microprocessors, micro controllers and digital signal processors. Overview of 8085 microprocessor. Overview of 8086 microprocessor. Signals and pins of 8086 microprocessor Module 2: Assembly language of 8086 (6) Description of Instructions. Assembly directives. Assembly software programs with algorithms
Module 3: Interfacing with 8086 (8) Interfacing with RAMs, ROMs along with the explanation of timing diagrams. Interfacing with peripheral ICs like 8255, 8254, 8279, 8259, 8259 etc. Interfacing with key boards, LEDs, LCDs, ADCs, and DACs etc. Module 4: Coprocessor 8087 (4) Architecture of 8087, interfacing with 8086. Data types, instructions and programming Module 5: Architecture of Micro controllers (4) Overview of the architecture of 8051 microcontroller. Overview of the architecture of 8096 16 bit microcontroller Module 6: Assembly language of 8051 (4) Description of Instructions. Assembly directives. Assembly software programs with algorithms Module 7: Interfacing with 8051 (5) Interfacing with keyboards, LEDs, 7 segment LEDs, LCDs, Interfacing with ADCs. Interfacing with DACs, etc. Module 8: High end processors (2) Introduction to 80386 and 80486
9/23/2009
9/23/2009
Computer Systems
9/23/2009
Computer Systems
9/23/2009
Turing Machine
Mathematical model of a device that can perform any computation Alan Turing (1937)
ability to read/write symbols on an infinite tape state transitions, based on current state and symbol
=
PDA Workstation
=
Supercomputer
a,b
Tadd
a+b
a,b
Tmul
ab
9/23/2009
Computer Systems
9/23/2009
Computer Systems
c(a+b)
cost
cell phone, automotive engine controller, ...
power
cell phone, handheld video game, ...
9/23/2009
Computer Systems
9/23/2009
Computer Systems
10
Algorithm
step-by-step procedure, guaranteed to finish definiteness, effective computability, finiteness
Program
express the algorithm using a computer language high-level language, low-level language
Instruction Set Architecture (ISA) Process Engineering & Fabrication: develop and manufacture lowest-level components
specifies the set of instructions the computer can perform data types, addressing mode
Devices
9/23/2009
Computer Systems
11
9/23/2009
Computer Systems
12
Course Outline
Bits and Bytes
How do we represent information using electrical signals?
Digital Logic
How do we build circuits to process information?
Logic Circuits
combine basic operations to realize microarchitecture many different ways to implement a single function (e.g., addition)
Devices
properties of materials, manufacturability
C Programming
How do we write programs in C? How do we implement high-level programming constructs?
9/23/2009
Computer Systems
13
9/23/2009
Computer Systems
14
Could base state on value of voltage, but control and detection circuits more complex.
compare turning on a light switch to measuring or regulating voltage
9/23/2009
9/23/2009
Computer Systems
16
Basic unit of information is the binary digit, or bit. Values with more than two states require multiple bits.
A collection of two bits has four possible states: 00, 01, 10, 11 A collection of three bits has eight possible states: 000, 001, 010, 011, 100, 101, 110, 111 A collection of n bits has 2n possible states.
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
Data type:
representation and operations within the computer
17
9/23/2009
Computer Systems
18
Unsigned Integers
Non-positional notation
could represent a number (5) with a string of ones (11111) problems?
329
102 101 100
3x100 + 2x10 + 9x1 = 329
101
21
22
20
9/23/2009
Computer Systems
19
9/23/2009
Computer Systems
20
Signed Integers
With n bits, we have 2n distinct values.
assign about half to positive integers (1 through 2 n-1) and about half to negative (- 2n-1 through -1) that leaves two values: one for 0, and one extra
1111 + 1 10000
Positive integers
just like unsigned zero in most significant (MS) bit 00101 = 5
Negative integers
sign-magnitude set MS bit to show negative, other bits are the same as unsigned 10101 = -5 ones complement flip every bit to represent negative 11010 = -5 in either case, MS bit indicates sign: 0=positive, 1=negative
9/23/2009
Computer Systems
21
9/23/2009
Computer Systems
22
Twos Complement
Problems with sign-magnitude and 1s complement
two representations of zero (+0 and 0) arithmetic circuits are complex How to add two sign-magnitude numbers?
e.g., try 2 + (-3)
If number is negative,
start with positive number flip every bit (i.e., take the ones complement) then add one
01001 (9) +
(-9) 00000 (0)
Computer Systems
01001 (9)
(1s comp)
1
(-9)
23
9/23/2009
Computer Systems
24
-23
22 0 0 0 0 1 1 1 1
21 0 0 1 1 0 0 1 1
20 0 1 0 1 0 1 0 1 0 1 2 3 4 5 6 7
9/23/2009
-23 22 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1
21 0 0 1 1 0 0 1 1
20 0 1 0 1 0 1 0 1 -8 -7 -6 -5 -4 -3 -2 -1
Computer Systems
011010000
(1s comp) (flip) (copy)
0 0 0 0 0 0 0 0
100110000
9/23/2009
Computer Systems
25
26
More Examples X = 00100111two = 25+22+21+20 = 32+4+2+1 = 39ten X = -X = = = X= 11100110two 00011010 24+23+21 = 16+8+2 26ten -26ten
n 2n
0 1 2 3 4 5 6 7 8 9 10 1 2 4 8 16 32 64 128 256 512 1024
n 2n
0 1 2 3 4 5 6 7 8 9 10 1 2 4 8 16 32 64 128 256 512 1024
27
9/23/2009
Computer Systems
28
n 2n
0 1 2 3 4 5 6 7 8 9 10 1 2 4 8 16 32 64 128 256 512 1024
X = 104ten
= = = = = =
52 r0 26 r0 13 r0 6 r1 3 r0 1 r1
X = 104ten X = 01101000two
29
Microprocessor and Microcontroller Course
104 - 64 = 40 40 - 32 = 8 8-8 = 0
X = 01101000two
Microprocessor and Microcontroller Course 9/23/2009
1/2 = 0 r1
9/23/2009
Computer Systems
30
Addition
As weve discussed, 2s comp. addition is just binary addition.
assume all integers have the same number of bits ignore carry out for now, assume that sum fits in n-bit 2s comp. representation
Well also look at overflow conditions for addition. Multiplication, division, etc., can be built from these basic operations. Logical operations are also useful:
AND OR NOT
11110110 (-10) +
(-9) (-19)
9/23/2009
Computer Systems
31
9/23/2009
Computer Systems
32
Subtraction
Negate subtrahend (2nd no.) and add.
assume all integers have the same number of bits ignore carry out for now, assume that difference fits in n-bit 2s comp. representation
Sign Extension
To add two numbers, we must represent them with the same number of bits. If we just pad with zeroes on the left: 4-bit 0100 1100
(4) (-4)
11110110 (-10) +
(-9)
11110110 (-10)
(9) (-1)
Instead, replicate the MS bit -- the sign bit: 4-bit 0100 1100 8-bit 00000100 11111100
(4) (-4)
33
9/23/2009
Computer Systems
34
Overflow
If operands are too big, then sum cannot be represented as an n-bit 2s comp number.
Logical Operations
Operations on logical TRUE or FALSE
two states -- takes one bit to represent: TRUE=1, FALSE=0
signs of both operands are the same, and sign of sum is different.
A 0 0 1 1
B 0 1 0 1
A AND B 0 0 0 1
A 0 0 1 1
B 0 1 0 1
A OR B 0 1 1 1
A 0 1
NOT A 1 0
9/23/2009
Computer Systems
35
9/23/2009
Computer Systems
36
Hexadecimal Notation 11000101 00001111 00000101 11000101 00001111 11001111 11000101 00111010
Computer Systems
AND
It is often convenient to write binary (base-2) numbers as hexadecimal (base-16) numbers instead.
fewer digits -- four bits per hex digit less error prone -- easy to corrupt long string of 1s and 0s
OR
useful for setting bits OR with zero = no change OR with one = 1
Binary
0000 0001 0010 0011 0100 0101 0110 0111
Hex
0 1 2 3 4 5 6 7
Decimal
0 1 2 3 4 5 6 7
Binary
1000 1001 1010 1011 1100 1101 1110 1111
Hex
8 9 A B C D E F
Decimal
8 9 10 11 12 13 14 15
OR
NOT
unary operation -- one argument flips every bit
NOT
9/23/2009
37
9/23/2009
Computer Systems
38
Fractions: Fixed-Point
How can we represent fractions?
Use a binary point to separate positive from negative powers of two -- just like decimal point. 2s comp addition and subtraction still work. if binary points are aligned
011101010001111010011010111
3 A 8 F 4 D 7
+
This is not a new machine representation, just a convenient way to write the number.
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
40
10
1023
Sign is 1 number is negative. Exponent field is 01111110 = 126 (decimal). Fraction is 0.100000000000 = 0.5 (decimal).
S Exponent
Fraction
N ( 1)S 1.fraction 2exponent 127 , 1 exponent 254 N ( 1)S 0.fraction 2126 , exponent 0
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
41
9/23/2009
Computer Systems
42
Floating-Point Operations
Will regular 2s complement arithmetic work for Floating Point numbers?
(Hint: In decimal, how do we compute 3.07 x 10 12 + 9.11 x 108?)
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
@ A B C D E F G H I J K L M N O
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
P Q R S T U V W X Y Z [ \ ] ^ _
60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
` a b c d e f g h i j k l m n o
70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
Computer Systems
9/23/2009
Computer Systems
9/23/2009
44
11
9/23/2009
9/23/2009
Computer Systems
46
n-type
when Gate has positive voltage, short circuit between #1 and #2 (switch closed) when Gate has zero voltage, open circuit between #1 and #2 (switch open)
Gate = 1
Gate = 1
Gate = 0
Terminal #2 must be connected to GND (0V).
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
Gate = 0
Computer Systems
48
12
Logic Gates
Use switch behavior of MOS transistors to implement logical functions: AND, OR, NOT. Digital symbols:
recall that we assign a range of analog voltages to each digital (logic) symbol
CMOS Circuit
Complementary MOS Uses both n-type and p-type MOS transistors
p-type Attached to + voltage Pulls output voltage UP when input is zero n-type Attached to GND Pulls output voltage DOWN when input is one For all inputs, make sure that output is either connected to GND or to +, but not both!
assignment of voltage ranges depends on electrical properties of transistors being used typical values for "1": +5V, +3.3V, +2.9V from now on we'll use +2.9V
9/23/2009
Computer Systems
49
9/23/2009
Computer Systems
50
NOR Gate
Truth table
A In 2.9 V Out 0V In 0 1 Out 1 0
9/23/2009 Computer Systems
B 0 1 0 1
C 1 0 0 0
Computer Systems
0 0 1
Note: Serial structure on top, parallel on bottom.
51
Microprocessor and Microcontroller Course 9/23/2009
0 V 2.9 V
52
13
OR Gate
A 0 0 1 1 B 0 1 0 1 C 0 1 1 1
A 0
B 0 1 0 1
C 1 1 1 0
Computer Systems
0 1 1
9/23/2009
Computer Systems
53
9/23/2009
54
AND Gate
A 0 0 1 1 B 0 1 0 1 C 0 0 0 1
9/23/2009
Computer Systems
55
9/23/2009
Computer Systems
56
14
DeMorgan's Law
Converting AND to OR (with some help from NOT) Consider the following gate:
A B 0 0 0 1 1 0 1 1
A B
A B
0 1 1 1
1 1 0 0
1 0 1 0
1 0 0 0
Can implement with multiple two-input gates, or with single CMOS circuit.
Same as A+B!
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
57
9/23/2009
Computer Systems
58
Summary
MOS transistors are used as switches to implement logic functions.
n-type: connect to GND, turn on (with 1) to pull down to 0 p-type: connect to +2.9V, turn on (with 0) to pull up to 1
DeMorgan's Law
Convert AND to OR (and vice versa) by inverting inputs and output
We'll first look at some useful combinational circuits, then show how to use sequential circuits to store information.
9/23/2009
Computer Systems
59
9/23/2009
Computer Systems
60
15
Decoder
n inputs, 2n outputs
exactly one output is 1 for each possible input pattern
Multiplexer (MUX)
n-bit selector and 2n inputs, one output
output equals one of the inputs, depending on selector
2-bit decoder
4-to-1 MUX
9/23/2009
Computer Systems
61
9/23/2009
Computer Systems
62
Full Adder
Add two bits and carry-in, produce one-bit sum and carry-out.
A B Cin S Cout 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 0 0 1 0 1 1 1
Four-bit Adder
9/23/2009
Computer Systems
63
9/23/2009
Computer Systems
64
16
Logical Completeness
Can implement ANY truth table with AND, OR, NOT. A 0 0 0 0 1 1 1 1 B 0 0 1 1 0 0 1 1 C 0 1 0 1 0 1 0 1 D 0 0 1 0 0 1 0 0
2. OR the results of the AND gates. 1. AND combinations that yield a "1" in the truth table.
Sequential Circuit
stores information output depends on stored information (state) plus input so a given input might produce different outputs, depending on the stored information example: ticket counter advances when you push the button output depends on previous state useful for building memory elements and state machines
9/23/2009
Computer Systems
65
9/23/2009
Computer Systems
66
1 1 1 quiescent state -- holds its previous value note: if a is 1, b is 0, and vice versa 0 0 0 1 1 1
67
9/23/2009
Computer Systems
68
17
S = 0, R=1
set value to 1
R = 0, S = 1
0 1 1 set value to 0
R=S=0
both outputs equal one final state determined by electrical properties of gates Dont do it!
Computer Systems
69
9/23/2009
Computer Systems
70
Gated D-Latch
Two inputs: D (data) and WE (write enable)
when WE = 1, latch is set to value of D S = NOT(D), R = D when WE = 0, latch holds previous value S = R = 1
Register
A register stores a multi-bit value.
We use a collection of D-latches, all controlled by a common WE. When WE=1, n-bit value D is written to register.
9/23/2009
Computer Systems
71
9/23/2009
Computer Systems
72
18
Memory
Now that we know how to store bits, we can build a memory a logical k m array of stored bits.
Use brackets to denote range: D[l:r] denotes bit l to bit r, from left to right
15 0
A = 0101001101010101
A[14:9] = 101001
May also see A<14:9>, especially in hardware block diagrams.
A[2:0] = 101
k = 2n locations
m bits
Computer Systems
9/23/2009
Computer Systems
73
9/23/2009
74
22 x 3 Memory
address write enable word select word WE input bits
Two basic kinds of RAM (Random Access Memory) Static RAM (SRAM)
fast, maintains data as long as power applied
75
9/23/2009
Computer Systems
76
19
State Machine
Another type of sequential circuit
Combines combinational logic with storage Remembers state, and changes output (and state) based on inputs and current state
30
State Machine
25
5 10 15
4 1 8 4 Outputs
Combinational Success depends only on the values, not the order in which they are set.
20
Inputs
Storage Elements
Sequential Success depends on the sequence of values (e.g, R-13, L-22, R-3).
9/23/2009
Computer Systems
77
9/23/2009
Computer Systems
78
State
The state of a system is a snapshot of all the relevant elements of the system at the moment the snapshot is taken. Examples:
The state of a basketball game can be represented by the scoreboard. Number of points, time remaining, possession, etc. The state of a tic-tac-toe game can be represented by the placement of Xs and Os on the board.
9/23/2009
Computer Systems
79
9/23/2009
Computer Systems
80
20
State Diagram
Shows states and actions that cause a transition between states.
9/23/2009
Computer Systems
81
9/23/2009
Computer Systems
82
The Clock
Frequently, a clock circuit triggers transition from one state to the next.
1 0
Storage elements
Maintain state representation.
One Cycle At the beginning of each clock cycle, state machine makes a transition, based on the current state and the external inputs.
time
State Machine
Inputs
Outputs
Not always required. In lock example, the input itself triggers a transition.
Clock
Storage Elements
9/23/2009
Computer Systems
83
9/23/2009
Computer Systems
84
21
Storage
Each master-slave flipflop stores one state bit. The number of storage elements (flipflops) needed is determined by the number of states (and the representation of each state). Examples:
Sequential lock Four states two bits Basketball scoreboard 7 bits for each score, 5 bits for minutes, 6 bits for seconds, 1 bit for possession arrow, 1 bit for half,
During 1st phase (clock=1), previously-computed state becomes current state and is sent to the logic circuit.
Microprocessor and Microcontroller Course 9/23/2009
During 2nd phase (clock=0), next state, computed by logic circuit, is stored in Latch A.
Computer Systems
85
9/23/2009
Computer Systems
86
Complete Example
A blinking traffic sign
No lights on 1 & 2 on 1, 2, 3, & 4 on 1, 2, 3, 4, & 5 on (repeat as long as switch is turned on)
3 4 1 2 5
DANGER
MOVE RIGHT State bit S1 State bit S0 Outputs
9/23/2009
Computer Systems
87
9/23/2009
Computer Systems
88
22
In 0 1 1 1 1
S1 X 0 0 1 1
S0 S1 S0 X 0 1 0 1 0 0 1 1 0 0 1 0 1 0
Master-slave flipflop
S1 0 0 1 1
S0 0 1 0 1
Z 0 1 1 1
Y 0 0 1 1
X 0 0 0 1
9/23/2009
Computer Systems
89
9/23/2009
Computer Systems
90
Sequential Logic
State machine -- coordinate control signals and data movement Registers and latches -- storage elements
9/23/2009
Computer Systems
91
9/23/2009
23
INPUT
Keyboard Mouse Scanner Disk
The basic structure proposed in the draft became known as the von Neumann machine (or model).
a memory, containing instructions and data a processing unit, for performing arithmetic and logical operations a control unit, for interpreting instructions For more history, see http://www.maxmon.com/history.htm
CONTROL UNIT
PC IR
9/23/2009
Computer Systems
93
9/23/2009
Computer Systems
94
Memory
2k x m array of stored bits Address
unique (k-bit) identifier of location 0000 0001 0010 0011 0100 0101 0110 1101 1110 1111
Interface to Memory
How does processing unit get data to/from memory? MAR: Memory Address Register MEMORY MDR: Memory Data Register
00101101
Contents
m-bit value stored in location
MAR
MDR
1. Write the address (A) into the MAR. 2. Send a read signal to the memory. 3. Read the data from MDR.
STORE
write a value to a memory location
9/23/2009
Computer Systems
95
9/23/2009
Computer Systems
96
24
Processing Unit
Functional Units
ALU = Arithmetic and Logic Unit could have many functional units. some of them special-purpose (multiply, square root, )
PROCESSING UNIT
ALU TEMP
Each device has its own interface, usually a set of registers like the memorys MAR and MDR
keyboard: data register (KBDR) and status register (KBSR) monitor: data register (DDR) and status register (DSR)
Registers
Small, temporary storage Operands and results of functional units
Word Size
number of bits normally processed by ALU in one instruction also width of registers
OUTPUT
Monitor Printer LED Disk
9/23/2009
Computer Systems
97
9/23/2009
Computer Systems
98
Control Unit
Orchestrates execution of the program
Instruction Processing
CONTROL UNIT
PC IR
Fetch instruction from memory Decode instruction Evaluate address Fetch operands from memory Execute operation Store result
Computer Systems
Instruction Register (IR) contains the current instruction. Program Counter (PC) contains the address of the next instruction to be executed. Control unit:
reads an instruction from memory
the instructions address is in the PC
interprets the instruction, generating signals that tell the other components what to do
an instruction may take many machine cycles to complete
9/23/2009
99
9/23/2009
Computer Systems
100
25
Instruction
The instruction is the fundamental unit of work. Specifies two things:
opcode: operation to be performed operands: data/locations to be used for operation
F D EA
An instruction is encoded as a sequence of bits. (Just like data!) Often, but not always, instructions have a fixed length, such as 16 or 32 bits. Control unit interprets instruction: generates sequence of control signals to carry out operation. Operation is either executed completely, or not at all. A computers instructions and their formats is known as its Instruction Set Architecture (ISA).
OP EX S
9/23/2009
Computer Systems
101
9/23/2009
Computer Systems
102
F D EA OP EX S
9/23/2009
Computer Systems
103
9/23/2009
Computer Systems
104
26
F D EA OP EX S
9/23/2009
Computer Systems
105
9/23/2009
Computer Systems
106
Need special instructions that change the contents of the PC. These are called control instructions.
jumps are unconditional -- they always change the PC branches are conditional -- they change the PC only if some condition is true (e.g., the result of an ADD is zero)
9/23/2009
Computer Systems
107
9/23/2009
Computer Systems
108
27
F D EA OP EX S
not all phases are needed by every instruction phases may take variable number of machine cycles
9/23/2009
Computer Systems
109
9/23/2009
Computer Systems
110
Chapter 5 Programming
9/23/2009
Computer Systems
111
9/23/2009
28
Stepwise Refinement
Also known as systematic decomposition. Start with problem statement:
We wish to count the number of occurrences of a character in a file. The character in question is to be input from the keyboard; the result is to be displayed on the monitor.
Debugging
How do we figure out why it didnt work? Examining registers and memory, setting breakpoints, etc.
Decompose task into a few simpler subtasks. Decompose each subtask into smaller subtasks, and these into even smaller subtasks, etc.... until you get to the machine instruction level.
9/23/2009
Computer Systems
113
9/23/2009
Computer Systems
114
Problem Statement
Because problem statements are written in English, they are sometimes ambiguous and/or incomplete.
Where is file located? How big is it, or how do I know when Ive reached the end? How should final count be printed? A decimal number? If the character is a letter, should I count both upper-case and lower-case occurrences?
Task
Subtask 1
Test condition
False
Subtask 1 Subtask 2
Subtask 2 Subtask
Sequential
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
Conditional
9/23/2009
Iterative
Computer Systems
115
116
29
Sequential
Do Subtask 1 to completion, then do Subtask 2 to completion, etc.
Conditional
If condition is true, do Subtask 1; else, do Subtask 2.
True
False
Count = Count + 1
9/23/2009
Computer Systems
117
9/23/2009
Computer Systems
118
Iterative
Do Subtask over and over, as long as the test condition is true.
Check each element of the file and count the characters that match.
False
9/23/2009
Computer Systems
119
9/23/2009
Computer Systems
120
30
Instruction
A Generate Condition
? C
Instruction
A Generate Condition
0000 ? C
PC offset to address C
B 0000
Subtask 1
Subtask 2
C
Subtask 1
0000 111 D
B Subtask
0000 111 A
Subtask
C
Subtask 2
Next Subtask
D Next Subtask
PC offset to address D
Next Subtask
Next Subtask
PC offset to address A
Assuming all addresses are close enough that PC-relative branch can be used.
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
122
Refining B
B
START
Yes
Input a character. Then scan a file, counting occurrences of that character. Finally, display on the monitor the number of occurrences of the character (up to 9).
Done? No
Scan the file, location by location, incrementing the counter if the character matches.
B1
STOP
C
Scan the file, location by location, incrementing the counter if the character m atches.
STOP
Computer Systems
123
9/23/2009
Computer Systems
124
31
Refining B1
Refining B2 and B3
Yes Done? No
B2
B
Yes
Done?
Yes
Yes
Done? No
Done? No
Yes
R1 = R0?
No
R2 = R2 + 1
B1
B3
B3 Get next character.
R3 = R3 + 1 R1 = M[R3]
Conditional (B2) and sequential (B3). Use of LC-2 registers and instructions.
125
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
126
Debugging
Youve written your program and it doesnt work. Now what? What do you do when youre lost in a city?
Drive around randomly and hope you find it?
Debugging Operations
Any debugging environment should provide means to:
1. 2. 3. 4. Display values in memory and registers. Deposit values in memory and registers. Execute instruction sequence in a program. Stop execution when desired. High-level languages (C, Java, ...) usually have source-code debugging tools. For debugging at the machine instruction level: simulators operating system monitor tools in-circuit emulators (ICE)
plug-in hardware replacements that give instruction-level control
9/23/2009
Computer Systems
127
9/23/2009
Computer Systems
128
32
Types of Errors
Syntax Errors
You made a typing error that resulted in an illegal operation. Not usually an issue with machine language, because almost any bit pattern corresponds to some legal instruction. In high-level languages, these are often caught during the translation from language to machine code.
Breakpoints
Tell the simulator to stop executing when it reaches a specific instruction. Check overall results at specific points in the program. Lets you quickly execute sequences to get a high-level overview of the execution behavior. Quickly execute sequences that your believe are correct.
Logic Errors
Your program is legal, but wrong, so the results dont match the problem statement. Trace the program to see whats really happening and determine how to get the proper behavior.
Data Errors
Input data is different than what you expected. Test the program with a wide variety of inputs.
Watchpoints
Tell the simulator to stop when a register or memory location changes or when it equals a specific value. Useful when you dont know where or when a value is changed.
9/23/2009
Computer Systems
129
9/23/2009
Computer Systems
130
Example 1: Multiply
This program is supposed to multiply the two unsigned integers in R4 and R5.
R2
-0 10 10 10 20 20 20 30 30 30 40 40 40 40
R4
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
R5
3 3 3 2 2 2 1 1 1 0 0 0 -1 -1 -1
R2
10 20 30 40 40
R4
10 10 10 10 10
R5
2 1 0 -1 -1
No
x3203
R5 = 0?
Yes
x3201
HALT
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
Executing loop one time too many. Branch at x3203 should be based on Z bit only, not Z and P.
Computer Systems
131
9/23/2009
132
33
No
R4 = 0?
Yes
x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009
0101001001100000 0101100100100000 0001100100101010 0010010011111100 0110011010000000 0001010010100001 0001001001000011 0001100100111111 0000001111111011 1111000000100101
x3100 x3101 x3102 x3103 x3104 x3105 x3106 x3107 x3108 x3109
x3107 x2819 x0110 x0310 x0110 x1110 x11B1 x0019 x0007 x0004
R1
-0 0 0 0
R2
----x3107
R4
--0 10 10
Should be x3100! Loading contents of M[x3100], not address. Change opcode of x3003 from 0010 (LD) to 1110 (LEA).
9/23/2009 Computer Systems
HALT
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
133
134
x3100 x3101 x3102 x3103 x3104 x3105 x3106 x3107 x3108 x3109
9 7 32 0 -8 19 6 13 5 61
Perhaps we didnt look at all the data? Put a breakpoint at x300D to see how many times we branch back .
PC
x300D x300D x300D
R0
1 1 1 0
R2
7 32 0 0
R3
9 8 7 7
R4
x3101 x3102 x3103 x3103
R2 = 5?
No
Yes
No
R3 = 0?
Yes
R4 = R4 + 1 R3 = R3-1 R2 = M[R4]
R0 = 0
Microprocessor and Microcontroller Course
HALT
9/23/2009 Computer Systems
Branch uses condition code set by loading R2 with M[R4], not by decrementing R3. Swap x300B and x300C, or remove x300C and branch back to x3007.
9/23/2009 Computer Systems
135
136
34
R2[15] = 1?
No
No
R2[15] = 1?
Yes
x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009
9/23/2009
0101001001100000 0001001001101111 1010010000000110 0000100000000100 0001001001111111 0001010010000010 0000100000000001 0000111111111100 1111000000100101 0011000100000000
Computer Systems
PC
x3007 x3007 x3007 x3007 x3007 x3007 x3007 x3007 x3007 x3007
R1
14 13 12 11 10 9 8 7 6 5
PC
x3007 x3007 x3007 x3007 x3007 x3007 x3007 x3007 x3007 x3007
R1
4 3 2 1 0 -1 -2 -3 -4 -5
If no ones, then branch to HALT never occurs! This is called an infinite loop. Must change algorithm to either (a) check for special case (R2=0), or (b) exit loop if R1 < 0.
HALT
Microprocessor and Microcontroller Course
137
9/23/2009
Computer Systems
138
When tracing, make sure to notice whats really happening, not what you think should happen.
In summing program, it would be easy to not notice that address x3107 was loaded instead of x3100.
9/23/2009
Computer Systems
139
9/23/2009
35
0001110010000110
Humans like symbols
ADD
R6,R2,R6
9/23/2009
Computer Systems
141
9/23/2009
Computer Systems
142
Operands
registers -- specified by Rn, where n is the register number numbers -- indicated by # (decimal) or x (hex) 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
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
9/23/2009
Computer Systems
143
9/23/2009
Computer Systems
144
36
Assembler Directives
Pseudo-operations
do not refer to operations executed by program used by assembler look like instruction, but opcode starts with dot Opcode .ORIG .END .BLKW .FILL .STRINGZ n n n-character string Operand address Meaning starting address of program end of program allocate n words of storage
Trap Codes
Assembler provides pseudo-instructions for each trap code, so you dont have to remember them.
Code HALT IN Equivalent TRAP x25 TRAP x23 Description Halt execution and print message to console. Print prompt on console, read (and echo) one character from keybd. Character stored in R0[7:0]. Write one character (in R0[7:0]) to console. Read one character from keyboard. Character stored in R0[7:0]. Write null-terminated string to console. Address of string is in R0.
OUT GETC
allocate one word, initialize with value n allocate n+1 locations, initialize w/characters and null terminator PUTS
9/23/2009
Computer Systems
145
9/23/2009
Computer Systems
146
Style Guidelines
Use the following style guidelines to improve the readability and understandability of your programs:
1. Provide a program header, with authors 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.
Sample Program
Count the occurrences of a character in a file.
Remember this?
Count = 0
(R2 = 0) YES
Done?
(R1 ?= EOT)
NO
9/23/2009
Computer Systems
147
9/23/2009
Computer Systems
148
37
; ; Test character for end of file ; TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04) BRz OUTPUT ; If done, prepare the output
9/23/2009
Computer Systems
149
9/23/2009
Computer Systems
150
Assembly Process
Convert assembly language file (.asm) into an executable file (.obj) for the LC-3 simulator.
First Pass:
scan program file find all labels and calculate the corresponding addresses; this is called the symbol table
Second Pass:
convert instructions to machine language, using information from symbol table
9/23/2009
Computer Systems
151
9/23/2009
Computer Systems
152
38
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 cant use PC-relative addressing mode
9/23/2009
Computer Systems
153
9/23/2009
Computer Systems
154
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
9/23/2009
Computer Systems
155
9/23/2009
Computer Systems
156
39
Chapter 7 I/O
But where does data in memory come from? And how does data get out of the system so that humans can use it?
9/23/2009
9/23/2009
Computer Systems
158
I/O Controller
Control/Status Registers
CPU tells device what to do -- write to control register CPU checks whether task is done -- read status register
Data Registers
CPU transfers data to/from device Control/Status Graphics Controller Electronics
CPU
Output Data
display
Device electronics
performs actual operation pixels to screen, bits to/from disk, characters from keyboard
9/23/2009
Computer Systems
159
9/23/2009
Computer Systems
160
40
Programming Interface
How are device registers identified?
Memory-mapped vs. special instructions
Memory-mapped
assign a memory address to each device register use data movement instructions (LD/ST) for control and data transfer
9/23/2009
Computer Systems
161
9/23/2009
Computer Systems
162
Transfer Timing
I/O events generally happen much slower than CPU cycles. Synchronous
data supplied at a fixed, predictable rate CPU reads/writes every X cycles
Transfer Control
Who determines when the next data transfer occurs? Polling
CPU keeps checking status register until new data arrives OR device ready for next data Are we there yet? Are we there yet? Are we there yet?
Asynchronous
data rate less predictable CPU must synchronize with device, so that it doesnt miss data or write too quickly
Interrupts
Device sends a special signal to CPU when new data arrives OR device ready for next data CPU can be performing other tasks instead of polling device. Wake me when we get there.
9/23/2009
Computer Systems
163
9/23/2009
Computer Systems
164
41
NO
1514
Polling
YES
read character
ready bit
When KBDR is read:
KBSR[15] is set to zero keyboard is enabled
9/23/2009
Computer Systems
165
9/23/2009
Computer Systems
166
Output to Monitor
When Monitor is ready to display another character:
the ready bit (DSR[15]) is set to one
Address Control Logic determines whether MDR is loaded from Memory or from KBSR/KBDR.
15
8 7
1514
ready bit
9/23/2009
Computer Systems
167
9/23/2009
Computer Systems
168
42
POLL
NO
LDI R1, DSRPtr BRzp POLL STI R0, DDRPtr ... .FILL xFE04 .FILL xFE06
Polling
169
9/23/2009
Computer Systems
170
Interrupt-Driven I/O
External device can: (1) Force currently executing program to stop; (2) Have the processor satisfy the devices needs; and (3) Resume the stopped program as if nothing happened. Why?
Polling consumes a lot of cycles, especially for rare events these cycles can be used for more computation. Example: Process previous input while collecting current input. (See Example 8.1 in text.)
POLL2
R0, KBSRPtr POLL1 R0, KBDRPtr R1, DSRPtr POLL2 R0, DDRPtr
NO
new char?
YES
read character
screen ready?
YES
write character
9/23/2009
Computer Systems
171
9/23/2009
Computer Systems
172
43
Interrupt-Driven I/O
To implement an interrupt mechanism, we need:
A way for the I/O device to signal the CPU that an interesting event has occurred. A way for the CPU to test whether the interrupt signal is set and whether its priority is higher than the current program.
Priority
Every instruction executes at a stated level of urgency. LC-3: 8 priority levels (PL0-PL7)
Example: Payroll program runs at PL0. Nuclear power correction program runs at PL6. Its OK for PL6 device to interrupt PL0 program, but not the other way around.
Generating Signal
Software sets "interrupt enable" bit in device register. When ready bit is set and IE bit is set, interrupt is signaled.
1514 13
KBSR
interrupt signal to processor
Priority encoder selects highest-priority device, compares to current processor priority level, and generates interrupt signal if appropriate.
9/23/2009
Computer Systems
173
9/23/2009
Computer Systems
174
D EA OP EX S
Transfer to ISR
YES
interrupt signal?
9/23/2009
Computer Systems
175
9/23/2009
44
System Calls
Certain operations require specialized knowledge and protection:
specific knowledge of I/O device registers and the sequence of operations needed to use them I/O resources shared among multiple users/programs; a mistake could affect lots of other users!
System Call
1. User program invokes system call. 2. Operating system code performs operation. 3. Returns control to user program.
Not every programmer knows (or wants to know) this level of detail Provide service routines or system calls (part of operating system) to safely and conveniently perform low-level, privileged operations
9/23/2009
Computer Systems
177
9/23/2009
Computer Systems
178
TRAP Instruction
TRAP
Trap vector
identifies which system call to invoke 8-bit index into table of service routine addresses in LC-3, this table is stored in memory at 0x0000 0x00FF 8-bit trap vector is zero-extended into 16-bit memory address
Where to go
lookup starting address from table; place in PC
179
9/23/2009
Computer Systems
180
45
Must make sure that service routine does not change R7, or we wont know where to return.
9/23/2009
Computer Systems
181
9/23/2009
Computer Systems
182
9/23/2009
Computer Systems
183
9/23/2009
Computer Systems
184
46
vector
symbol routine
read a single character (no echo) output a character to the monitor write a string to the console print prompt to console, read and echo character from keyboard halt the program
9/23/2009
Computer Systems
185
9/23/2009
Computer Systems
186
Example
LEA R3, Binary LD R6, ASCII LD R7, COUNT TRAP x23 ADD R0, R0, R6 STR R0, R3, #0 ADD R3, R3, #1 ADD R7, R7, -1 BRp AGAIN BRnzp NEXT .FILL xFFD0 .FILL #10 .BLKW #10 ; char->digit template ; initialize to 10 ; Get char ; convert to number ; store number ; incr pointer ; decr counter ; more?
AGAIN
9/23/2009
187
9/23/2009
Computer Systems
188
47
Subroutines
A subroutine is a program fragment that:
lives in user space performs a well-defined task is invoked (called) by another user program returns control to the calling program when finished
JSR Instruction
Jumps to a location (like a branch but unconditional), and saves current PC (addr of next instruction) in R7.
saving the return address is called linking target address is PC-relative (PC + Sext(IR[10:0])) bit 11 specifies addressing mode if =1, PC-relative: target address = PC + Sext(IR[10:0]) if =0, register: target address = contents of register IR[8:6]
9/23/2009
Computer Systems
189
9/23/2009
Computer Systems
190
JSR
JSRR Instruction
What important feature does JSRR provide that JSR does not?
191
9/23/2009
Computer Systems
192
48
JSRR
9/23/2009
Computer Systems
193
9/23/2009
Computer Systems
194
To call from a program (within 1024 instructions): ; need to compute ADD JSR ADD ... R4 = R1 - R3 R0, R3, #0 ; copy R3 to R0 2sComp ; negate R4, R1, R0 ; add to R1
Return Values
A value passed out of a subroutine is called a return value. This is the value that you called the subroutine to compute. Examples: In 2sComp routine, negated value is returned in R0. In GETC service routine, character read from the keyboard is returned in R0.
9/23/2009
Computer Systems
195
9/23/2009
Computer Systems
196
49
Using Subroutines
In order to use a subroutine, a programmer must know:
its address (or at least a label that will be bound to its address) its function (what does it do?) NOTE: The programmer does not need to know how the subroutine works, but what changes are visible in the machines state after the routine has run. its arguments (where to pass data in, if any) its return values (where to get computed data, if any)
Remember: You MUST save R7 if you call any other subroutine or service routine (TRAP).
Otherwise, you wont be able to return to caller.
9/23/2009
Computer Systems
197
9/23/2009
Computer Systems
198
Example
(1) Write a subroutine FirstChar to:
find the first occurrence of a particular character (in R0) in a string (pointed to by R1); return pointer to character or to end of string (NULL) in R2.
call FirstChar
save R7, since were using JSR
Can write the second subroutine first, without knowing the implementation of FirstChar!
return
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
199
9/23/2009
Computer Systems
200
50
CountChar Implementation
; CountChar: subroutine to count occurrences of a char CountChar ST R3, CCR3 ; save registers ST R4, CCR4 ST R7, CCR7 ; JSR alters R7 ST R1, CCR1 ; save original string ptr AND R4, R4, #0 ; initialize count to zero CC1 JSR FirstChar ; find next occurrence (ptr in R2) LDR R3, R2, #0 ; see if char or null BRz CC2 ; if null, no more chars ADD R4, R4, #1 ; increment count ADD R1, R2, #1 ; point to next char in string BRnzp CC1 CC2 ADD R2, R4, #0 ; move return val (count) to R2 LD R3, CCR3 ; restore regs LD R4, CCR4 LD R1, CCR1 LD R7, CCR7 RET ; and return
FirstChar Algorithm
save regs R3=R0
no yes
R2 <- R1
return
201
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
9/23/2009
Computer Systems
202
FirstChar Implementation
; FirstChar: subroutine to find first occurrence of a char FirstChar ST R3, FCR3 ; save registers ST R4, FCR4 ; save original char NOT R4, R0 ; negate R0 for comparisons ADD R4, R4, #1 ADD R2, R1, #0 ; initialize ptr to beginning of string FC1 LDR R3, R2, #0 ; read character BRz FC2 ; if null, were done ADD R3, R3, R4 ; see if matches input char BRz FC2 ; if yes, were done ADD R2, R2, #1 ; increment pointer BRnzp FC1 FC2 LD R3, FCR3 ; restore registers LD R4, FCR4 ; RET ; and return
Library Routines
Vendor may provide object files containing useful subroutines
dont want to provide source code -- intellectual property assembler/linker must support EXTERNAL symbols (or starting address of routine must be supplied to user)
... .EXTERNAL ... LD R2, SQAddr JSRR R2 ... .FILL SQRT ; load SQRT addr SQRT
SQAddr
Using JSRR, because we dont know whether SQRT is within 1024 instructions.
9/23/2009
Computer Systems
203
9/23/2009
Computer Systems
204
51
9/23/2009
9/23/2009
Computer Systems
206
Stacks
A LIFO (last-in first-out) storage structure.
The first thing you put in is the last thing you take out. The last thing you put in is the first thing you take out.
A Physical Stack
Coin rest in the arm of an automobile
1995 1996 1998 1982 1995 1998 1982 1995
This means of access is what defines a stack, not the specific implementation. Two main operations: PUSH: add an item to the stack POP: remove an item from the stack
Initial State After One Push
9/23/2009
Computer Systems
207
9/23/2009
Computer Systems
208
52
A Hardware Implementation
Data items move between registers
Empty:
Yes
A Software Implementation
Data items don't move in memory, just our idea about there the TOP of the stack is.
No
Empty: TOP
No
Empty: TOP
Empty: TOP
No
TOP
TOP R6
TOP
TOP
TOP
R6
R6
R6
209
9/23/2009
Computer Systems
210
Pop LDR R0, R6, #0 ; load data from TOS ADD R6, R6, #1 ; decrement stack ptr
9/23/2009
Computer Systems
211
9/23/2009
Computer Systems
212
53
FAIL
MAX
9/23/2009
Computer Systems
213
9/23/2009
Computer Systems
214
Processor State
What state is needed to completely capture the state of a running process? Processor Status Register
Privilege [15], Priority Level [10:8], Condition Codes [2:0]
Registers
All temporary state of the process thats not stored in memory.
Use a stack!
Location of stack hard-wired. Push state to save, pop to restore.
9/23/2009
Computer Systems
215
9/23/2009
Computer Systems
216
54
Supervisor Stack
A special region of memory used as the stack for interrupt service routines.
Initial Supervisor Stack Pointer (SSP) stored in Saved.SSP. Another register for storing User Stack Pointer (USP): Saved.USP.
When switching from User mode to Supervisor mode (as result of interrupt), save R6 to Saved.USP.
Note: This all happens between the STORE RESULT of the last user instruction and the FETCH of the first ISR instruction.
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
217
9/23/2009
Computer Systems
218
Example (1)
Program A
Saved.SSP
1. 2. 3. Pop PC from supervisor stack. (PC = M[R6]; R6 = R6 + 1) Pop PSR from supervisor stack. (PSR = M[R6]; R6 = R6 + 1) If PSR[15] = 1, R6 = Saved.USP. (If going back to user mode, need to restore User Stack Pointer.)
x3006
ADD
219
9/23/2009
Computer Systems
220
55
Example (2)
Program A x6200 ISR for Device B
Example (3)
Program A x6200 ISR for Device B
R6
x3006
ADD R6
x6210
x3006
ADD
x6202
AND RTI
RTI
////// PC x6200
////// PC x6203
x6210
Saved.USP = R6. R6 = Saved.SSP. Push PSR and PC onto stack, then transfer to Device B service routine (at x6200).
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems 221
222
Example (4)
Program A x6200 ISR for Device B
Example (5)
Program A x6200 ISR for Device B
R6
x6203
PSR for B
x3007
PSR for A
x3006
ADD
x6202
AND
ISR for Device C
x6203
PSR for B
R6
x3007
PSR for A
x3006
ADD
x6202
AND
ISR for Device C
////// PC x6300
x6210
RTI
x6300
////// PC
x6315
x6210
RTI
x6300
x6203
x6315
RTI
Execute RTI at x6315; pop PC and PSR from stack.
RTI
Push PSR and PC onto stack, then transfer to Device C service routine (at x6300).
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
223
9/23/2009
Computer Systems
224
56
Example (6)
Program A
Saved.SSP
When something unexpected happens inside the processor, it may cause an exception. Examples:
ISR for Device C
x6203
PSR for B
x3007
PSR for A
x3006
ADD
x6202
AND RTI
////// PC x3007
x6210
x6300
Privileged operation (e.g., RTI in user mode) Executing an illegal opcode Divide by zero Accessing an illegal address (e.g., protected system memory)
RTI
Vector is determined internally by type of exception Priority is the same as running program
Execute RTI at x6210; pop PSR and PC from stack. Restore R6. Continue Program A as if nothing happened.
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
225
9/23/2009
Computer Systems
226
Example: OpAdd
POP two values, ADD, then PUSH result.
START
POP
POP
ADD
OK?
Yes
OK?
No
Yes
Yes
Why use a stack? Limited registers. Convenient calling convention for subroutines. Algorithm naturally expressed using FIFO data structure.
9/23/2009 Computer Systems
No
PUSH
RETURN
227
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems
228
57
Example: OpAdd
OpAdd JSR ADD BRp ADD JSR ADD BRp ADD JSR BRp JSR RET Restore2 ADD Restore1 ADD Exit RET POP R5,R5,#0 Exit R1,R0,#0 POP R5,R5,#0 Restore1 R0,R0,R1 RangeCheck Restore2 PUSH R6,R6,#-1 R6,R6,#-1 ; ; ; ; ; ; ; ; ; ; ; Get first operand. Check for POP success. If error, bail. Make room for second. Get second operand. Check for POP success. If err, restore & bail. Compute sum. Check size. If err, restore & bail. Push sum onto stack.
User inputs 2 and 3 -- what happens? Result displayed: e Why? ASCII '2' (x32) + ASCII '3' (x33) = ASCII 'e' (x65)
9/23/2009
Computer Systems
229
9/23/2009
Computer Systems
230
ASCII to Binary
Useful to deal with mult-digit decimal numbers Assume we've read three ASCII digits (e.g., "259") into a memory buffer. How do we convert this to a number we can use?
Convert first character to digit (subtract x30) and multiply by 100. Convert second character to digit and multiply by 10. Convert third character to digit. Add the three digits together.
Since we have a small range of numbers (0-9), use number as an index into a lookup table.
Entry 0: 0 x 100 = 0 Entry 1: 1 x 100 = 100 Entry 2: 2 x 100 = 200 Entry 3: 3 x 100 = 300 etc.
9/23/2009
Computer Systems
231
9/23/2009
Computer Systems
232
58
9/23/2009
Computer Systems
233
9/23/2009
Computer Systems
234
Conversion Routine (2 of 3)
ADD R1, R1, #-1 ; one less digit BRz DoneAtoB ; done if zero ADD R2, R2, #-1 ; points to tens digit ; LDR ADD LEA ADD LDR ADD ; ADD BRz ADD R1, R1, #-1 DoneAtoB R2, R2, #-1 ; ; ; ; one less digit done if zero points to hundreds digit R4, R4, R5, R5, R4, R0, R2, #0 R4, R3 Lookup10 R5, R4 R5, #0 R0, R4 ; load digit ; convert to number ; multiply by 10
Conversion Routine (3 of 3)
LDR R4, R2, #0 ; load digit ADD R4, R4, R3 ; convert to number LEA R5, Lookup100 ; multiply by 100 ADD R5, R5, R4 LDR R4, R5, #0 ADD R0, R0, R4 ; adds 100's contrib ; DoneAtoB NegZero ASCIIBUF Lookup10 RET .FILL .BLKW .FILL .FILL .FILL
xFFD0 4 0 10 20
; -x30
9/23/2009
Computer Systems
235
9/23/2009
Computer Systems
236
59
9/23/2009
Computer Systems
237
9/23/2009
Computer Systems
238
Conversion (2 of 3)
Begin100 Loop100 LD R2, ASCIIoffset LD R3, Neg100 ADD R0, R0, R3 BRn End100 ADD R2, R2, #1 ; add one to digit BRnzp Loop100 STR R2, R1, #1 ; store ASCII 100's digit LD R3, Pos100 ADD R0, R0, R3 ; restore last subtract LD R2, ASCIIoffset LD R3, Neg10 ADD R0, R0, R3 BRn End10 ADD R2, R2, #1 ; add one to digit BRnzp Loop10
Conversion Code (3 of 3)
End10 ; LD R2, ASCIIoffset ADD R2, R2, R0 ; convert one's digit STR R2, R1, #3 ; store one's digit RET ; ASCIIplus ASCIIneg ASCIIoffset Neg100 Pos100 Neg10 STR R2, R1, #2 ; store ASCII 10's digit ADD R0, R0, #10 ; restore last subtract
End100
Loop100
; ; ; ;
; -10
9/23/2009
Computer Systems
239
9/23/2009
Computer Systems
240
60
C: A High-Level Language
Gives symbolic names to values
dont need to know which register or memory location
operations do not depend on instruction set example: can write a = b * c, even though LC-3 doesnt have a multiply instruction
Provides expressiveness
use meaningful symbols that convey meaning simple expressions for common control patterns (if-then-else)
9/23/2009
9/23/2009
Computer Systems
242
Compilation
translates statements into machine language does not execute, but creates executable program performs optimization over multiple statements change requires recompilation can be harder to debug, since executed code may be different languages: C, C++, Fortran, Pascal
If interpreting, how many arithmetic operations occur? If compiling, we can analyze the entire program and possibly reduce the number of operations. Can we simplify the above algorithm to use a single arithmetic operation?
9/23/2009
Computer Systems
243
9/23/2009
Computer Systems
244
61
Compiling a C Program
Entire mechanism is usually called the compiler Preprocessor
macro substitution conditional compilation source-level transformations output is still C
Compiler
Source Code Analysis
front end parses programs to identify its pieces variables, expressions, statements, functions, etc. depends on language (not on target machine)
C Preprocessor
Code Generation
back end generates machine code from analyzed source may optimize machine code to make it run more efficiently very dependent on target machine
Compiler
generates object file machine instructions
Linker
combine object files (including libraries) into executable image
Symbol Table
Linker
map between symbolic names and items like assembler, but more kinds of information
Executable Image
9/23/2009
Computer Systems
245
9/23/2009
Computer Systems
246
A Simple C Program
#include <stdio.h> #define STOP 0 /* Function: main */ /* Description: counts down from user input to STOP */ main() { /* variable declarations */ int counter; /* an integer to hold count values */ int startPoint; /* starting point for countdown */ /* prompt user for input */ printf("Enter a positive number: "); scanf("%d", &startPoint); /* read into startPoint */ /* count down and print count */ for (counter=startPoint; counter >= STOP; counter--) printf("%d\n", counter); }
Preprocessor Directives
#include <stdio.h>
Before compiling, copy contents of header file (stdio.h) into source code. Header files typically contain descriptions of functions and variables needed by the program. no restrictions -- could be any C source code
#define STOP 0
Before compiling, replace all instances of the string "STOP" with the string "0" Called a macro Used for values that won't change during execution, but might change if the program is reused. (Must recompile.)
9/23/2009
Computer Systems
247
9/23/2009
Computer Systems
248
62
Comments
Begins with /* and ends with */ Can span multiple lines Cannot have a comment within a comment Comments are not recognized within a string
example: "my/*don't print this*/string" would be printed as: my/*don't print this*/string
main Function
Every C program must have a function called main(). This is the code that is executed when the program is run. The code for the function lives within brackets: main() { /* code goes here */ }
As before, use comments to help reader, not to confuse or to restate the obvious
9/23/2009
Computer Systems
249
9/23/2009
Computer Systems
250
Variable Declarations
Variables are used as names for data items. Each variable has a type, which tells the compiler how the data is to be interpreted (and how much space it needs, etc.). int counter; int startPoint; int is a predefined integer type in C.
scanf("%d", &startPoint);
String contains formatting directions for looking at input. This call says to read a decimal integer and assign it to the variable startPoint. (Don't worry about the & yet.)
9/23/2009
Computer Systems
251
9/23/2009
Computer Systems
252
63
Examples
This code:
printf("%d printf("43 printf("43 printf("43 is a plus plus plus prime 59 in 59 in 59 as number.\n", 43); decimal is %d.\n", 43+59); hex is %x.\n", 43+59); a character is %c.\n", 43+59);
9/23/2009
Computer Systems
253
9/23/2009
Computer Systems
254
Examples of Input
Many of the same formatting characters are available for user input. scanf("%c", &nextChar);
reads a single character and stores it in nextChar
scanf("%f", &radius);
reads a floating point number and stores it in radius
9/23/2009
Computer Systems
255
9/23/2009
Computer Systems
256
64