Вы находитесь на странице: 1из 64

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

MICROPROCESSOR & MICROCONTROLLER

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

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Module 0: Computing Systems From Bits and Gates to C and Beyond

Chapter 1 Welcome Aboard

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Big Idea #1: Universal Computing Device


All computers, given enough time and memory, are capable of computing exactly the same things.

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

Every computation can be performed by some Turing machine. (Turings thesis)

a,b

Tadd

a+b

a,b

Tmul

ab

Turing machine that adds


For more info about Turing machines, see http://www.wikipedia.org/wiki/Turing_machine/

Turing machine that multiplies


For more about Alan Turing, see http://www.turing.org.uk/turing/

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Universal Turing Machine


A machine that can implement all Turing machines -- this is also a Turing machine!
inputs: data, plus a description of computation (other TMs)

From Theory to Practice


In theory, computer can compute anything thats possible to compute
given enough memory and time

Tadd, Tmul a,b,c

c(a+b)

In practice, solving problems involves computing under constraints.


time
weather forecast, next frame of animation, ...

Universal Turing Machine U is programmable so is a computer!


instructions are part of the input data a computer can emulate a Universal Turing Machine

cost
cell phone, automotive engine controller, ...

power
cell phone, handheld video game, ...

A computer is a universal computing device.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Big Idea #2: Transformations Between Layers


Problems Algorithms Language

How do we solve a problem using a computer?


A systematic sequence of transformations between layers of abstraction.
Problem

Software Design: choose algorithms and data structures


Algorithm

Instruction Set Architecture Microarchitecture Circuits Devices


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

Programming: use language to express design


Program

Instr Set Architecture


9
Microprocessor and Microcontroller Course

Compiling/Interpreting: convert language to machine instructions

9/23/2009

Computer Systems

10

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Deeper and Deeper


Instr Set Architecture

Descriptions of Each Level


Problem Statement
stated using "natural language" may be ambiguous, imprecise

Processor Design: choose structures to implement ISA


Microarch

Algorithm
step-by-step procedure, guaranteed to finish definiteness, effective computability, finiteness

Logic/Circuit Design: gates and low-level circuits to implement components


Circuits

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

11

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

12

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Descriptions of Each Level (cont.)


Microarchitecture
detailed organization of a processor implementation different implementations of a single ISA

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)

Processor and Instruction Set


How do we build a processor out of logic elements? What operations (instructions) will we implement?

Assembly Language Programming


How do we use processor instructions to implement algorithms? How do we write modular, reusable code? (subroutines)

Devices
properties of materials, manufacturability

I/O, Traps, and Interrupts


How does processor communicate with outside world?

C Programming
How do we write programs in C? How do we implement high-level programming constructs?

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

13

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

14

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

How do we represent data in a computer?


At the lowest level, a computer is an electronic machine.
works by controlling the flow of electrons

Chapter 2 Bits, Data Types, and Operations

Easy to recognize two conditions:


1. presence of a voltage well call this state 1 2. absence of a voltage well call this state 0

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

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

16

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Computer is a binary digital system.


Digital system:
finite number of symbols

What kinds of data do we need to represent?


Numbers signed, unsigned, integers, floating point, complex, rational, irrational, Text characters, strings, Images pixels, colors, shapes, Sound Logical true, false Instructions

Binary (base two) system:


has two states: 0 and 1

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

Well start with numbers

17

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

18

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Unsigned Integers
Non-positional notation
could represent a number (5) with a string of ones (11111) problems?

Unsigned Integers (cont.)


An n-bit unsigned integer represents 2n values: from 0 to 2n-1. 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

Weighted positional notation


like decimal numbers: 329 3 is worth 300, because of its position, while 9 is only worth 9 most significant least significant

329
102 101 100
3x100 + 2x10 + 9x1 = 329

101
21

22

20

1x4 + 0x2 + 1x1 = 5

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

19

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

20

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Unsigned Binary Arithmetic


Base-2 addition just like base-10!
add from right to left, propagating carry
carry

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

10010 + 1001 11011

10010 + 1011 11101 10111 + 111

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

Subtraction, multiplication, division,

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

21

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

22

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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)

Twos Complement Representation


If number is positive or zero,
normal binary representation, zeroes in upper bit(s)

If number is negative,
start with positive number flip every bit (i.e., take the ones complement) then add one

How to add to ones complement numbers?


e.g., try 4 + (-3)

Twos complement representation developed to make circuits easy for arithmetic.


for each positive number (X), assign value to its negative (-X), such that X + (-X) = 0 with normal addition, ignoring carry out

00101 (5) + 11011 (-5) 00000 (0)


Microprocessor and Microcontroller Course 9/23/2009

01001 (9) +
(-9) 00000 (0)
Computer Systems

00101 (5) 11010 (1s comp) + 1 11011 (-5)

01001 (9)
(1s comp)

1
(-9)

23

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

24

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Twos Complement Shortcut


To take the twos complement of a number:
copy bits from right to left until (and including) the first 1 flip remaining bits to the left

Twos Complement Signed Integers


MS bit is sign bit it has weight 2n-1. Range of an n-bit number: -2n-1 through 2n-1 1.
The most negative number (-2n-1) has no positive counterpart.

-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 100101111 1 100110000

011010000
(1s comp) (flip) (copy)

0 0 0 0 0 0 0 0

100110000

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

25

Microprocessor and Microcontroller Course

26

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Converting Binary (2s C) to Decimal


1. If leading bit is one, take twos complement to get a positive number. 2. Add powers of 2 that have 1 in the corresponding bit positions. 3. If original number was negative, add a minus sign.

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

X = 01101000two = 26+25+23 = 64+32+8 = 104ten


Assuming 8-bit 2s complement numbers.

Assuming 8-bit 2s complement numbers.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

27

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

28

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Converting Decimal to Binary (2s C)


First Method: Division
1. 2. 3. 4. Find magnitude of decimal number. (Always positive.) Divide by two remainder is least significant bit. Keep dividing by two until answer is zero, writing remainders from right to left. Append a zero as the MS bit; if original number was negative, take twos complement.

Converting Decimal to Binary (2s C)


Second Method: Subtract Powers of Two 1. Find magnitude of decimal number. 2. Subtract largest power of two less than or equal to number. 3. Put a one in the corresponding bit position. 4. Keep subtracting until result is zero. 5. Append a zero as MS bit; if original was negative, take twos complement.

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

104/2 52/2 26/2 13/2 6/2 3/2

= = = = = =

52 r0 26 r0 13 r0 6 r1 3 r0 1 r1

bit 0 bit 1 bit 2 bit 3 bit 4 bit 5 bit 6


Computer Systems

X = 104ten X = 01101000two
29
Microprocessor and Microcontroller Course

104 - 64 = 40 40 - 32 = 8 8-8 = 0

bit 6 bit 5 bit 3

X = 01101000two
Microprocessor and Microcontroller Course 9/23/2009

1/2 = 0 r1

9/23/2009

Computer Systems

30

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Operations: Arithmetic and Logical


Recall: a data type includes representation and operations. We now have a good representation for signed integers, so lets look at some arithmetic operations:
Addition Subtraction Sign Extension

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

01101000 (104) 11110000 (-16) 01011000 (98)

11110110 (-10) +
(-9) (-19)

Assuming 8-bit 2s complement numbers.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

31

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

32

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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)

01101000 00010000 01101000 11110000 01011000

(104) (16) (104) (-16) (88)

11110110 (-10) +
(-9)

8-bit 00000100 00001100

(still 4) (12, not -4)

11110110 (-10)
(9) (-1)

Instead, replicate the MS bit -- the sign bit: 4-bit 0100 1100 8-bit 00000100 11111100

(4) (-4)

(still 4) (still -4)

Assuming 8-bit 2s complement numbers.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

33

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

34

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

01000 (8) + 01001 (9) 10001 (-15)


We have overflow if:

11000 (-8) + 10111 (-9) 01111 (+15)

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

Another test -- easy for hardware:


carry into MS bit does not equal carry out

View n-bit number as a collection of n logical values


operation applied to each bit independently

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

35

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

36

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Examples of Logical Operations


AND
useful for clearing bits AND with zero = 0 AND with one = no change

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

Microprocessor and Microcontroller Course

9/23/2009

37

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

38

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Converting from Binary to Hexadecimal


Every four bits is a hex digit.
start grouping from right-hand side

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

2-1 = 0.5 2-2 = 0.25 2-3 = 0.125

+
This is not a new machine representation, just a convenient way to write the number.
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

00101000.101 (40.625) 11111110.110 (-1.25) 00100111.011 (39.375)

No new operations -- same as integer arithmetic.


39
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

40

10

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Very Large and Very Small: Floating-Point


Large values: 6.023 x -- requires 79 bits Small values: 6.626 x 10-34 -- requires >110 bits Use equivalent of scientific notation: F x 2E Need to represent F (fraction), E (exponent), and sign. IEEE 754 Floating-Point Standard (32-bits):
1b 8b 23b

Floating Point Example


Single-precision IEEE floating point number: 10111111010000000000000000000000
sign exponent fraction

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

Value = -1.5 x 2(126-127) = -1.5 x 2-1 = -0.75.

41

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

42

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Text: ASCII Characters


ASCII: Maps 128 characters to 7-bit code.
both printable and non-printable (ESC, DEL, ) characters
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
43

nul soh stx etx eot enq ack bel bs ht nl vt np cr so si

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

20 sp 30 21 ! 31 22 " 32 23 # 33 24 $ 34 25 % 35 26 & 36 27 ' 37 28 ( 38 29 ) 39 2a * 3a 2b + 3b 2c , 3c 2d - 3d 2e . 3e 2f / 3f

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

Microprocessor and Microcontroller Course

9/23/2009

44

11

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Transistor: Building Block of Computers


Microprocessors contain millions of transistors
Intel Pentium 4 (2000): 48 million IBM PowerPC 750FX (2002): 38 million IBM/Apple PowerPC G5 (2003): 58 million

Chapter 3 Digital Logic Structures

Logically, each transistor acts as a switch Combined to implement logic functions


AND, OR, NOT

Combined to build higher-level structures


Adder, multiplexer, decoder, register,

Combined to build processor


8086, 80586

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

46

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

n-type MOS Transistor


MOS = Metal Oxide Semiconductor
two types: n-type and p-type

p-type MOS Transistor


p-type is complementary to n-type
when Gate has positive voltage, open circuit between #1 and #2 (switch open) when Gate has zero voltage, short circuit between #1 and #2 (switch closed)

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

Terminal #1 must be connected to +2.9V.


47
Microprocessor and Microcontroller Course 9/23/2009

Gate = 0
Computer Systems

48

12

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

49

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

50

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Inverter (NOT Gate)

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

Microprocessor and Microcontroller Course

52

13

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

OR Gate
A 0 0 1 1 B 0 1 0 1 C 0 1 1 1

NAND Gate (AND-NOT)

A 0

B 0 1 0 1

C 1 1 1 0
Computer Systems

Add inverter to NOR.

0 1 1

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

53

Microprocessor and Microcontroller Course

9/23/2009

54

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

AND Gate
A 0 0 1 1 B 0 1 0 1 C 0 0 0 1

Basic Logic Gates

Add inverter to NAND

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

55

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

56

14

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

DeMorgan's Law
Converting AND to OR (with some help from NOT) Consider the following gate:

More than 2 Inputs?


AND/OR can take any number of inputs.
AND = 1 if all inputs are 1. OR = 1 if any input is 1. Similar for NAND/NOR.

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

To convert AND to OR (or vice versa), invert inputs and output.

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

58

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Building Functions from Logic Gates


Combinational Logic Circuit
output depends only on the current inputs stateless

Sequential Logic Circuit


output depends on the sequence of inputs (past and present) stores information (state) from past inputs

Basic gates: NOT, NOR, NAND


Logic functions are usually expressed with AND, OR, and NOT

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

59

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

60

15

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

61

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

62

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

63

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

64

16

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Combinational vs. Sequential


Combinational Circuit
always gives the same output for a given set of inputs ex: adder always generates sum and carry, regardless of previous inputs

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

65

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

66

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

R-S Latch: Simple Storage Element


R is used to reset or clear the element set it to zero. S is used to set the element set it to one.
1 0 1 1 1 1 0

Clearing the R-S latch


Suppose we start with output = 1, then change R to zero.
1 0 1 1

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

Output changes to zero.


1 1 0 1

If both R and S are one, out could be either zero or one.


0 0 Then set R=1 to store value in quiescent state.
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

67

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

68

17

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Setting the R-S Latch


Suppose we start with output = 0, then change S to zero.
1 1 0

R-S Latch Summary


R=S=1
hold current value in latch

S = 0, R=1
set value to 1

R = 0, S = 1
0 1 1 set value to 0

Output changes to one.


0 0 1

R=S=0
both outputs equal one final state determined by electrical properties of gates Dont do it!

1 1 Then set S=1 to store value in quiescent state.


Microprocessor and Microcontroller Course 9/23/2009

Computer Systems

69

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

70

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

71

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

72

18

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Representing Multi-bit Values


Number bits from right (0) to left (n-1)
just a convention -- could be left to right, but must be consistent

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.

Address Space: number of locations


(usually a power of 2)

A[2:0] = 101

k = 2n locations

Addressability: number of bits per location


(e.g., byte-addressable)

m bits
Computer Systems

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

73

Microprocessor and Microcontroller Course

9/23/2009

74

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

22 x 3 Memory
address write enable word select word WE input bits

More Memory Details


This is a not the way actual memory is implemented.
fewer transistors, much more dense, relies on electrical properties

But the logical structure is very similar.


address decoder word select line word write enable

Two basic kinds of RAM (Random Access Memory) Static RAM (SRAM)
fast, maintains data as long as power applied

Dynamic RAM (DRAM)


slower but denser, bit storage decays must be periodically refreshed address decoder Also, non-volatile memories: ROM, PROM, flash, output bits
9/23/2009 Computer Systems

Microprocessor and Microcontroller Course

75

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

76

19

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Combinational vs. Sequential


Two types of combination locks

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

Combinational Logic Circuit

Storage Elements

Sequential Success depends on the sequence of values (e.g, R-13, L-22, R-3).

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

77

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

78

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

State of Sequential Lock


Our lock example has four different states, labelled A-D: A: The lock is not open, and no relevant operations have been performed. B: The lock is not open, and the user has completed the R-13 operation. C: The lock is not open, and the user has completed R-13, followed by L-22. D: The lock is open.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

79

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

80

20

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

State Diagram
Shows states and actions that cause a transition between states.

Finite State Machine


A description of a system with the following components: 1. 2. 3. 4. 5. A finite number of states A finite number of external inputs A finite number of external outputs An explicit specification of all state transitions An explicit specification of what determines each external output value

Often described by a state diagram.


Inputs trigger state transitions. Outputs are associated with each state (or with each transition).

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

81

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

82

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

The Clock
Frequently, a clock circuit triggers transition from one state to the next.
1 0

Implementing a Finite State Machine


Combinational logic
Determine outputs and next state.

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

Combinational Logic Circuit

Outputs

Not always required. In lock example, the input itself triggers a transition.

Clock

Storage Elements

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

83

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

84

21

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Storage: Master-Slave Flipflop


A pair of gated D-latches, to isolate next state from current state.

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

86

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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)

Traffic Sign State Diagram

3 4 1 2 5

Switch on Switch off

DANGER
MOVE RIGHT State bit S1 State bit S0 Outputs

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

87

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

88

22

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Traffic Sign Truth Tables


Outputs (depend only on state: S1S0)
Lights 1 and 2 Lights 3 and 4 Light 5

Traffic Sign Logic


Next State: S1S0 (depend on state and input)
Switch

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

Whenever In=0, next state is 00.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

89

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

90

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

From Logic to Data Path


The data path of a computer is all the logic used to process information. Combinational Logic
Decoders -- convert instructions into control signals Multiplexers -- select inputs and outputs ALU (Arithmetic and Logic Unit) -- operations on data

Chapter 4 The Von Neumann Model

Sequential Logic
State machine -- coordinate control signals and data movement Registers and latches -- storage elements

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

91

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

23

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

The Stored Program Computer


1943: ENIAC
Presper Eckert and John Mauchly -- first general electronic computer. (or was it John V. Atanasoff in 1939?) Hard-wired program -- settings of dials and switches.

Von Neumann Model


MEMORY
MAR MDR

1944: Beginnings of EDVAC


among other improvements, includes program stored in memory

INPUT
Keyboard Mouse Scanner Disk

OUTPUT PROCESSING UNIT


ALU TEMP
Monitor Printer LED Disk

1945: John von Neumann


wrote a report on the stored program concept, known as the First Draft of a Report on EDVAC

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

93

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

94

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

To LOAD a location (A):



10100010

MAR

MDR

Basic Operations: LOAD


read a value from a memory location

1. Write the address (A) into the MAR. 2. Send a read signal to the memory. 3. Read the data from MDR.

To STORE a value (X) to a location (A):


1. Write the data (X) to the MDR. 2. Write the address (A) into the MAR. 3. Send a write signal to the memory.

STORE
write a value to a memory location

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

95

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

96

24

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Processing Unit
Functional Units
ALU = Arithmetic and Logic Unit could have many functional units. some of them special-purpose (multiply, square root, )

Input and Output


Devices for getting data into and out of computer memory

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

Some devices provide both input and output


disk, network
INPUT
Keyboard Mouse Scanner Disk

Word Size
number of bits normally processed by ALU in one instruction also width of registers

OUTPUT
Monitor Printer LED Disk

Program that controls access to a device is usually called a driver.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

97

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

98

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Microprocessor and Microcontroller Course

9/23/2009

99

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

100

25

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Instruction Processing: FETCH


Load next instruction (at address stored in PC) from memory into Instruction Register (IR).
Copy contents of PC into MAR. Send read signal to memory. Copy contents of MDR into IR.

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

Then increment PC, so that it points to the next instruction in sequence.


PC becomes PC+1.

OP EX S

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

101

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

102

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Instruction Processing: DECODE


First identify the opcode.
In LC-3, this is always the first four bits of instruction. A 4-to-16 decoder asserts a control line corresponding to the desired opcode.

Instruction Processing: EVALUATE ADDRESS


F D EA OP EX S
For instructions that require memory access, compute address used for access. Examples:
add offset to base register (as in LDR) add offset to PC add offset to zero

F D EA OP EX S

Depending on opcode, identify other operands from the remaining bits.


Example: for LDR, last six bits is offset for ADD, last three bits is source operand #2

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

103

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

104

26

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Instruction Processing: FETCH OPERANDS


Obtain source operands needed to perform operation. Examples:
load data from memory (LDR) read data from register file (ADD)

Instruction Processing: EXECUTE


F D EA OP EX S
Perform the operation, using the source operands. Examples:
send operands to ALU and assert ADD signal do nothing (e.g., for loads and stores)

F D EA OP EX S

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

105

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

106

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Instruction Processing: STORE RESULT


Write results to destination. (register or memory) Examples:
result of ADD is placed in destination register result of memory load is placed in destination register for store instruction, data is stored to memory write address to MAR, data to MDR assert WRITE signal to memory

Changing the Sequence of Instructions


F D EA OP EX S
In the FETCH phase, we increment the Program Counter by 1. What if we dont want to always execute the instruction that follows this one?
examples: loop, if-then, function call

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)

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

107

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

108

27

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Instruction Processing Summary


Instructions look just like data -- its all interpretation. Three basic kinds of instructions:
computational instructions (ADD, AND, ) data movement instructions (LD, ST, ) control instructions (JMP, BRnz, )

Control Unit State Diagram


The control unit is a state machine. Here is part of a simplified state diagram:

Six basic phases of instruction processing:

F D EA OP EX S
not all phases are needed by every instruction phases may take variable number of machine cycles

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

109

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

110

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Stopping the Clock


Control unit will repeat instruction processing sequence as long as clock is running.
If not processing instructions from your application, then it is processing instructions from the Operating System (OS). The OS is a special program that manages processor and other resources.

To stop the computer:


AND the clock generator signal with ZERO When control unit stops seeing the CLOCK signal, it stops processing.

Chapter 5 Programming

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

111

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

28

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Solving Problems using a Computer


Methodologies for creating computer programs that perform a desired function. Problem Solving
How do we figure out what to tell the computer to do? Convert problem statement into algorithm, using stepwise refinement.

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

113

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

114

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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?

Three Basic Constructs


There are three basic ways to decompose a task:

Task

How do you resolve these issues?


Ask the person who wants the problem solved, or Make a decision and document it.
True

Subtask 1

Test condition

False Test condition True

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

Microprocessor and Microcontroller Course

116

29

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Get character input from keyboard

True

file char = input?

False

Count and print the occurrences of a character in a file

Examine file and count the number of characters that match

Test character. If match, increment counter.

Count = Count + 1

Print number to the screen

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

117

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

118

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Iterative
Do Subtask over and over, as long as the test condition is true.

Problem Solving Skills


Learn to convert problem statement into step-by-step description of subtasks.
Like a puzzle, or a word problem from grammar school math. What is the starting state of the system? What is the desired ending state? How do we move from one state to another? Recognize English words that correlate to three basic constructs: do A then do B sequential if G, then do H conditional for each X, do Y iterative do Z until W iterative

Check each element of the file and count the characters that match.

more chars to check? True

False

Check next char and count if matches.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

119

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

120

30

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Code for Conditional


Exact bits depend on condition being tested
True Test Condition False

Code for Iteration


PC offset to address C

Instruction
A Generate Condition
? C

Exact bits depend on condition being tested


Test Condition True False

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

Unconditional branch to Next Subtask

D Next Subtask

PC offset to address D

Next Subtask

Next Subtask

Unconditional branch to retest condition

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

Assuming all addresses are on the same page.


121
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

122

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example: Counting Characters


START A Initialize: Put initial values into all locations that w ill be needed to carry out this task. - Input a character. - Set up a pointer to the first location of the file that will be scanned. - Get the first character from the file. - Zero the register that holds the count. B

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

Test character. If a match, increment counter. Get next character.

STOP
C

Scan the file, location by location, incrementing the counter if the character m atches.

Initial refinement: Big task into three sequential subtasks.


Microprocessor and Microcontroller Course 9/23/2009

Display the count on the m onitor.

Refining B into iterative construct.

STOP
Computer Systems

123

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

124

31

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Refining B1

Refining B2 and B3
Yes Done? No

B2
B

Yes

Done?
Yes

Yes

Done? No

No B1 B2 Test character. If matches, increment counter. B3 Get next character.


B1

Done? No

Yes

R1 = R0?

No

R2 = R2 + 1

B1

Test character. If a match, increment counter. Get next character.

B2 Test character. If matches, increment counter.

B3
B3 Get next character.

R3 = R3 + 1 R1 = M[R3]

Refining B1 into sequential subtasks.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

Conditional (B2) and sequential (B3). Use of LC-2 registers and instructions.
125
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

126

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Return to a known point and look at a map?


In debugging, the equivalent to looking at a map is tracing your program.
Examine the sequence of instructions being executed. Keep track of results being produced. Compare result from each instruction to the expected result.

Different programming levels offer different tools.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

127

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

128

32

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Tracing the Program


Execute the program one piece at a time, examining register and memory to see results at each step. Single-Stepping
Execute one instruction at a time. Tedious, but useful to help you verify each step of your program.

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

129

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

130

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example 1: Multiply
This program is supposed to multiply the two unsigned integers in R4 and R5.

Debugging the Multiply Program


Single-stepping
PC PC and registers at the beginning of each instruction
x3200 x3201 x3202 x3203 x3201 x3202 x3203 x3201 x3202

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

Breakpoint at branch (x3203)


PC
x3203 x3203 x3203 x3203

clear R2 add R4 to R2 decrement R5

x3200 x3201 x3202 x3203 x3204

0101010010100000 0001010010000100 0001101101111111 0000011111111101 1111000000100101

R2
10 20 30 40 40

R4
10 10 10 10 10

R5
2 1 0 -1 -1

No

x3203

Should stop looping here!

R5 = 0?
Yes

x3201

Set R4 = 10, R5 =3. Run program. Result: R2 = 40, not 30.

x3202 x3203 x3204

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

Microprocessor and Microcontroller Course

9/23/2009

132

33

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example 2: Summing an Array of Numbers


This program is supposed to sum the numbers stored in 10 locations beginning with x3100, leaving the result in R1.
R1 = 0 R4 = 10 R2 = x3100 R1 = R1 + M[R2] R2 = R2 + 1 R4 = R4 - 1

Debugging the Summing Program


Running the the data below yields R1 = x0024, but the sum should be x8135. What happened?
Address Contents

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

Start single-stepping program...


PC
x3000 x3001 x3002 x3003 x3004

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

Microprocessor and Microcontroller Course

134

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example 3: Looking for a 5


This program is supposed to set R0=1 if theres a 5 in one ten memory locations, starting at x3100. Else, it should set R0 to 0.
R0 = 1, R1 = -5, R3 = 10 R4 = x3100, R2 = M[R4]

Debugging the Fives Program


x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 x300A x300B x300C x300D x300E x300F x3010 0101000000100000 0001000000100001 0101001001100000 0001001001111011 0101011011100000 0001011011101010 0010100000001001 0110010100000000 0001010010000001 0000010000000101 0001100100100001 0001011011111111 0110010100000000 0000001111111010 0101000000100000 1111000000100101 0011000100000000 Running the program with a 5 in location x3108 results in R0 = 0, not R0 = 1. What happened?
Address Contents

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]

Didnt branch back, even though R3 > 0?

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

Microprocessor and Microcontroller Course

136

34

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example 4: Finding First 1 in a Word


This program is supposed to return (in R1) the bit position of the first 1 in a word. The address of the word is in location x3009 (just past the end of the program). If there are no ones, R1 should be set to 1.
R1 = 15 R2 = data
Yes

Debugging the First-One Program


Program works most of the time, but if data is zero, it never seems to HALT. Breakpoint at backwards branch (x3007)

R2[15] = 1?
No

decrement R1 shift R2 left one bit

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

138

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Debugging: Lessons Learned


Trace program to see whats going on.
Breakpoints, single-stepping

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.

Chapter 6 Assembly Language

Test your program using a variety of input data.


In Examples 3 and 4, the program works for many data sets. Be sure to test extreme cases (all ones, no ones, ...).

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

139

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

35

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Human-Readable Machine Language


Computers like ones and zeros

An Assembly Language Program


; ; Program to multiply a number by the constant 6 ; .ORIG x3050 LD R1, SIX LD R2, NUMBER AND R3, R3, #0 ; Clear R3. It will ; contain the product. ; The inner loop ; AGAIN ADD R3, R3, R2 ADD R1, R1, #-1 ; R1 keeps track of BRp AGAIN ; the iteration. ; HALT ; NUMBER .BLKW 1 SIX .FILL x0006 ; .END

0001110010000110
Humans like symbols

ADD

R6,R2,R6

; increment index reg.

Assembler is a program that turns symbols into machine instructions.


ISA-specific: close correspondence between symbols and instruction set mnemonics for opcodes labels for memory locations additional operations for allocating storage and initializing data

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

141

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

142

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Opcodes and Operands


Opcodes
reserved symbols that correspond to LC-3 instructions listed in Appendix A ex: ADD, AND, LD, LDR,

Labels and Comments


Label
placed at the beginning of the line assigns a symbolic name to the address corresponding to line ex: LOOP ADD R1,R1,#-1 BRp LOOP

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

143

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

144

36

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

TRAP x21 TRAP x20 TRAP x22

allocate one word, initialize with value n allocate n+1 locations, initialize w/characters and null terminator PUTS

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

145

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

146

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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)

Convert count to ASCII character


(R0 = x30, R0 = R2 + R0)

Ptr = 1st file character


(R3 = M[x3012]) YES

NO

Print count Match?


(R1 ?= R0) NO (TRAP x21)

Input char from keybd


(TRAP x23)

HALT Incr Count Load char from file


(R1 = M[R3]) (R2 = R2 + 1) (TRAP x25)

Load next char from file


(R3 = R3 + 1, R1 = M[R3])

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

147

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

148

37

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Char Count in Assembly Language (1 of 3)


; ; ; ; ; ; ; ; ; Program to count occurrences of a character in a file. Character to be input from the keyboard. Result to be displayed on the monitor. Program only works if no more than 9 occurrences are found. Initialization .ORIG AND LD GETC LDR x3000 R2, R2, #0 ; R2 is counter, initially 0 R3, PTR ; R3 is pointer to characters ; R0 gets character input R1, R3, #0 ; R1 gets first character

Char Count in Assembly Language (2 of 3)


; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; If no match, do not increment ADD R2, R2, #1 ; ; Get next character from file. ; GETCHAR ADD R3, R3, #1 ; Point to next character. LDR R1, R3, #0 ; R1 gets next char to test BRnzp TEST ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Covert binary count to ASCII OUT ; ASCII code in R0 is displayed. HALT ; Halt machine

; ; Test character for end of file ; TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04) BRz OUTPUT ; If done, prepare the output

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

149

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

150

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Char Count in Assembly Language (3 of 3)


; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x4000 .END

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

151

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

152

38

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

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.

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.

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

3. Stop when .END statement is reached.

NOTE: A line that contains only a comment is considered an empty line.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

153

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

154

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Multiple Object Files


An object file is not necessarily a complete program.
system-provided library routines code blocks written by multiple developers

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

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

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

155

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

156

39

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

I/O: Connecting to Outside World


So far, weve learned how to:
compute with values in registers load data from memory to registers store data from registers to memory

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?

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

158

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

I/O: Connecting to the Outside World


Types of I/O devices characterized by:
behavior: input, output, storage input: keyboard, motion detector, network interface output: monitor, printer, network interface storage: disk, CD-ROM data rate: how fast can data be transferred? keyboard: 100 bytes/sec disk: 30 MB/s network: 1 Mb/s - 1 Gb/s

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

159

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

160

40

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Programming Interface
How are device registers identified?
Memory-mapped vs. special instructions

Memory-Mapped vs. I/O Instructions


Instructions
designate opcode(s) for I/O register and operation encoded in instruction

How is timing of transfer managed?


Asynchronous vs. synchronous

Who controls transfer?


CPU (polling) vs. device (interrupts)

Memory-mapped
assign a memory address to each device register use data movement instructions (LD/ST) for control and data transfer

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

161

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

162

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

163

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

164

41

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Input from Keyboard


When a character is typed:
its ASCII code is placed in bits [7:0] of KBDR (bits [15:8] are always zero) the ready bit (KBSR[15]) is set to one keyboard is disabled -- any typed characters will be ignored
15 8 7 0

Basic Input Routine

POLL new char?

LDI R0, KBSRPtr BRzp POLL LDI R0, KBDRPtr ...

keyboard data KBDR KBSR

NO

1514

Polling

YES
read character

KBSRPtr .FILL xFE00 KBDRPtr .FILL xFE02

ready bit
When KBDR is read:
KBSR[15] is set to zero keyboard is enabled

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

165

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

166

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Simple Implementation: Memory-Mapped Input

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

output data DDR DSR

1514

ready bit

When data is written to Display Data Register:


DSR[15] is set to zero character in DDR[7:0] is displayed any other character data written to DDR is ignored (while DSR[15] is zero)

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

167

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

168

42

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Basic Output Routine

Simple Implementation: Memory-Mapped Output

POLL

NO

screen ready? YES write character


DSRPtr DDRPtr

LDI R1, DSRPtr BRzp POLL STI R0, DDRPtr ... .FILL xFE04 .FILL xFE06

Polling

Sets LD.DDR or selects DSR as input.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

169

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

170

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Keyboard Echo Routine


Usually, input character is also printed to screen.
User gets feedback on character typed and knows its ok to type the next character. POLL1 LDI BRzp LDI LDI BRzp STI ... KBSRPtr KBDRPtr DSRPtr DDRPtr .FILL .FILL .FILL .FILL xFE00 xFE02 xFE04 xFE06
NO

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

171

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

172

43

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

interrupt enable bit ready bit

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

173

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

174

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Testing for Interrupt Signal


CPU looks at signal between STORE and FETCH phases. If not set, continues with next instruction. If set, transfers control to interrupt service routine.
F
NO

D EA OP EX S

Transfer to ISR

YES

interrupt signal?

Chapter 8 TRAP Routines and Subroutines

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

175

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

44

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

177

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

178

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

How to get back


save address of next instruction (current PC) in R7
NOTE: PC has already been incremented during instruction fetch stage.
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

179

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

180

45

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

RET (JMP R7)


How do we transfer control back to instruction following the TRAP? We saved old PC in R7. JMP R7 gets us back to the user program at the right spot. LC-3 assembly language lets us use RET (return)
in place of JMP R7.

TRAP Mechanism Operation

1. Lookup starting address. 2. Transfer to service routine. 3. Return (JMP R7).

Must make sure that service routine does not change R7, or we wont know where to return.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

181

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

182

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example: Using the TRAP Instruction


.ORIG x3000 LD R2, TERM ; Load negative ASCII 7 LD R3, ASCII ; Load ASCII difference AGAIN TRAP x23 ; input character ADD R1, R2, R0 ; Test for terminate BRz EXIT ; Exit if done ADD R0, R0, R3 ; Change to lowercase TRAP x21 ; Output to monitor... BRnzp AGAIN ; ... again and again... TERM .FILL xFFC9 ; -7 ASCII .FILL x0020 ; lowercase bit EXIT TRAP x25 ; halt .END

Example: Output Service Routine


.ORIG x0430 ; syscall address ST R7, SaveR7 ; save R7 & R1 ST R1, SaveR1 ; ----- Write character TryWrite LDI R1, CRTSR ; get status BRzp TryWrite ; look for bit 15 on WriteIt STI R0, CRTDR ; write char ; ----- Return from TRAP Return LD R1, SaveR1 ; restore R1 & R7 LD R7, SaveR7 RET ; back to user CRTSR .FILL xF3FC CRTDR .FILL xF3FF SaveR1 .FILL 0 stored in table, SaveR7 .FILL 0 location x21 .END

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

183

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

184

46

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

TRAP Routines and their Assembler Names

Saving and Restoring Registers


Must save the value of a register if: Its value will be destroyed by service routine, and We will need to use the value after that action. Who saves?
caller of service routine? knows what it needs later, but may not know what gets altered by called routine called service routine? knows what it alters, but does not know what will be needed later by calling routine

vector

symbol routine

x20 x21 x22 x23 x25

GETC OUT PUTS IN HALT

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

185

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

186

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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?

Saving and Restoring Registers


Called routine -- callee-save
Before start, save any registers that will be altered (unless altered value is desired by calling program!) Before return, restore those same registers

AGAIN

Calling routine -- caller-save


Save registers destroyed by own instructions or by called routines (if known), if values needed later save R7 before TRAP save R0 before TRAP x23 (input character) Or avoid using those registers altogether

ASCII COUNT Binary

Whats wrong with this routine? What happens to R7?


Computer Systems

Values are saved by storing them in memory.

Microprocessor and Microcontroller Course

9/23/2009

187

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

188

47

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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]

Like a service routine, but not part of the OS


not concerned with protecting hardware resources no special privilege required

Reasons for subroutines:


reuse useful (and debugged!) code without having to keep typing it in divide task among multiple programmers use vendor-supplied library of useful routines

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

189

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

190

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

JSR

JSRR Instruction

Just like JSR, except Register addressing mode.


target address is Base Register bit 11 specifies addressing mode

What important feature does JSRR provide that JSR does not?

NOTE: PC has already been incremented during instruction fetch stage.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

191

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

192

48

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

JSRR

Returning from a Subroutine


RET (JMP R7) gets us back to the calling routine.
just like TRAP

NOTE: PC has already been incremented during instruction fetch stage.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

193

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

194

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example: Negate the value in R0


2sComp NOT ADD RET R0, R0 R0, R0, #1 ; flip bits ; add one ; return to caller

Passing Information to/from Subroutines


Arguments
A value passed in to a subroutine is called an argument. This is a value needed by the subroutine to do its job. Examples: In 2sComp routine, R0 is the number to be negated In OUT service routine, R0 is the character to be printed. In PUTS routine, R0 is address of string to be printed.

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.

Note: Caller should save R0 if well need it later!

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

195

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

196

49

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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)

Saving and Restore Registers


Since subroutines are just like service routines, we also need to save and restore registers, if needed. Generally use callee-save strategy, except for return values.
Save anything that the subroutine will alter internally that shouldnt be visible when the subroutine returns. Its good practice to restore incoming arguments to their original values (unless overwritten by return value).

Remember: You MUST save R7 if you call any other subroutine or service routine (TRAP).
Otherwise, you wont be able to return to caller.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

197

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

198

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

CountChar Algorithm (using FirstChar)


save regs R1 <- R2 + 1

(2) Use FirstChar to write CountChar, which:


counts the number of occurrences of a particular character (in R0) in a string (pointed to by R1); return count in R2.

call FirstChar
save R7, since were using JSR

R3 <- M(R2) restore regs R3=0


yes no

Can write the second subroutine first, without knowing the implementation of FirstChar!

return
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

199

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

200

50

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

R2 <- R2 + 1 R3 <- M(R2) restore regs R3=0


yes no

return
201
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

202

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

203

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

204

51

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Stack: An Abstract Data Type


An important abstraction that you will encounter in many applications.

Chapter 9 And, Finally... The Stack

We will describe three uses: Interrupt-Driven I/O


The rest of the story

Evaluating arithmetic expressions


Store intermediate results on stack instead of in registers

Data type conversion


2s comp binary to ASCII strings

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

206

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

After Three More Pushes

After One Pop

First quarter out is the last quarter in.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

207

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

208

52

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

////// ////// ////// ////// //////


Initial State

#18 ////// ////// ////// //////


After One Push

#12 #5 #31 #18 //////


After Three More Pushes

#31 #18 ////// ////// //////


After Two Pops

TOP

////// ////// ////// ////// ////// x4000


Initial State

TOP R6

////// ////// ////// #18 ////// x3FFF


After One Push

TOP

#12 #5 #31 #18 ////// x3FFC


After Three More Pushes

TOP

#12 #5 #31 #18 ////// x3FFE


After Two Pops

TOP

R6

R6

R6

By convention, R6 holds the Top of Stack (TOS) pointer.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

209

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

210

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Basic Push and Pop Code


For our implementation, stack grows downward (when item added, TOS moves closer to 0) Push ADD R6, R6, #-1 ; decrement stack ptr STR R0, R6, #0 ; store data (R0)

Pop with Underflow Detection


If we try to pop too many items off the stack, an underflow condition occurs.
Check for underflow by checking TOS before removing data. Return status code in R5 (0 for success, 1 for underflow) POP R1, EMPTY ADD R2, R6, BRz FAIL LDR R0, R6, ADD R6, R6, AND R5, R5, RET FAIL AND R5, R5, ADD R5, R5, RET EMPTY .FILL xC000 LD ; EMPTY = -x4000 R1 ; Compare stack pointer ; with x3FFF #0 #1 #0 ; SUCCESS: R5 = 0 #0 ; FAIL: R5 = 1 #1

Pop LDR R0, R6, #0 ; load data from TOS ADD R6, R6, #1 ; decrement stack ptr

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

211

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

212

53

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Push with Overflow Detection


If we try to push too many items onto the stack, an overflow condition occurs.
Check for underflow by checking TOS before adding data. Return status code in R5 (0 for success, 1 for overflow) PUSH LD R1, MAX ADD R2, R6, BRz FAIL ADD R6, R6, STR R0, R6, AND R5, R5, RET AND R5, R5, ADD R5, R5, RET .FILL xC005 ; MAX = -x3FFB R1 ; Compare stack pointer ; with x3FFF #-1 #0 #0 ; SUCCESS: R5 = 0 #0 ; FAIL: R5 = 1 #1

Interrupt-Driven I/O (Part 2)


Interrupts were introduced in Chapter 8.
1. External device signals need to be serviced. 2. Processor saves state and starts service routine. 3. When finished, processor restores state and resumes program.

Interrupt is an unscripted subroutine call, triggered by an external event.


Chapter 8 didnt explain how (2) and (3) occur, because it involves a stack. Now, were ready

FAIL

MAX

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

213

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

214

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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]

Where to Save Processor State?


Cant use registers.
Programmer doesnt know when interrupt might occur, so she cant prepare by saving critical registers. When resuming, need to restore state exactly as it was.

Memory allocated by service routine? Program Counter


Pointer to next instruction to be executed. Must save state before invoking routine, so we wouldnt know where. Also, interrupts may be nested that is, an interrupt service routine might also get interrupted!

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

215

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

216

54

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Invoking the Service Routine The Details


1. 2. 3. 4. 5. 6. 7. 8. If Priv = 1 (user), Saved.USP = R6, then R6 = Saved.SSP. Push PSR and PC to Supervisor Stack. Set PSR[15] = 0 (supervisor mode). Set PSR[10:8] = priority of interrupt being serviced. Set PSR[2:0] = 0. Set MAR = x01vv, where vv = 8-bit interrupt vector provided by interrupting device (e.g., keyboard = x80). Load memory location (M[x01vv]) into MDR. Set PC = MDR; now first instruction of ISR will be fetched.

Want to use R6 as stack pointer.


So that our PUSH/POP routines still work.

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

218

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Returning from Interrupt


Special instruction RTI that restores state.

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

////// ////// ////// ////// ////// PC x3006

x3006

ADD

RTI is a privileged instruction.


Can only be executed in Supervisor Mode. If executed in User Mode, causes an exception. (More about that later.)

Executing ADD at location x3006 when Device B interrupts.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

219

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

220

55

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example (2)
Program A x6200 ISR for Device B

Example (3)
Program A x6200 ISR for Device B

R6

////// ////// x3007


PSR for A

x3006

ADD R6
x6210

////// ////// x3007


PSR for A

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

Executing AND at x6202 when Device C interrupts.


Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

222

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

224

56

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Example (6)
Program A

Exception: Internal Interrupt


ISR for Device B x6200

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)

Handled just like an interrupt


x6315

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

226

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Arithmetic Using a Stack


Instead of registers, some ISA's use a stack for source and destination operations: a zero-address machine.
Example: ADD instruction pops two numbers from the stack, adds them, and pushes the result to the stack.

Example: OpAdd
POP two values, ADD, then PUSH result.
START

POP

POP

ADD

Evaluating (A+B)(C+D) using a stack:


(1) push A (2) push B (3) ADD (4) push C (5) push D (6) ADD (7) MULTIPLY (8) pop result
Range OK?
No

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

Put back first

Put back both

PUSH

RETURN
227
Microprocessor and Microcontroller Course 9/23/2009 Computer Systems

Microprocessor and Microcontroller Course

228

57

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Data Type Conversion


Keyboard input routines read ASCII characters, not binary values. Similarly, output routines write ASCII. Consider this program:
TRAP x23 ; input from keybd ADD R1, R0, #0 ; move to R1 TRAP x23 ; input from keybd ADD R0, R1, R0 ; add two inputs TRAP x21 ; display result TRAP x25 ; HALT

; Decr stack ptr (undo POP) ; Decr stack ptr

User inputs 2 and 3 -- what happens? Result displayed: e Why? ASCII '2' (x32) + ASCII '3' (x33) = ASCII 'e' (x65)

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

229

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

230

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Multiplication via a Lookup Table


How can we multiply a number by 100?
One approach: Add number to itself 100 times. Another approach: Add 100 to itself <number> times. (Better if number < 100.)

x32 x35 x39

'2' '5' '9'

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.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

231

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

232

58

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Code for Lookup Table


; multiply R0 by 100, using lookup table ; LEA R1, Lookup100 ; R1 = table base ADD R1, R1, R0 ; add index (R0) LDR R0, R1, #0 ; load from M[R1] ... Lookup100 .FILL 0 ; entry 0 .FILL 100 ; entry 1 .FILL 200 ; entry 2 .FILL 300 ; entry 3 .FILL 400 ; entry 4 .FILL 500 ; entry 5 .FILL 600 ; entry 6 .FILL 700 ; entry 7 .FILL 800 ; entry 8 .FILL 900 ; entry 9

Complete Conversion Routine (1 of 3)


; Three-digit buffer at ASCIIBUF. ; R1 tells how many digits to convert. ; Put resulting decimal number in R0. ASCIItoBinary AND R0, R0, #0 ; clear result ADD R1, R1, #0 ; test # digits BRz DoneAtoB ; done if no digits ; LD R3, NegZero ; R3 = -x30 LEA R2, ASCIIBUF ADD R2, R2, R1 ADD R2, R2, #-1 ; points to ones digit ; LDR R4, R2, #0 ; load digit ADD R4, R4, R3 ; convert to number ADD R0, R0, R4 ; add ones contrib

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

233

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

234

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

; adds tens contrib

xFFD0 4 0 10 20

; -x30

... Lookup100 ...

.FILL 0 .FILL 100

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

235

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

236

59

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Binary to ASCII Conversion


Converting a 2's complement binary value to a three-digit decimal number
Resulting characters can be output using OUT

Binary to ASCII Conversion Code (part 1 of 3)


; R0 is between -999 and +999. ; Put sign character in ASCIIBUF, followed by three ; ASCII digit characters. BinaryToASCII LEA R1, ASCIIBUF ; pt to result string ADD R0, R0, #0 ; test sign of value BRn NegSign LD R2, ASCIIplus ; store '+' STR R2, R1, #0 BRnzp Begin100 LD R2, ASCIIneg ; store '-' STR R2, R1, #0 NOT R0, R0 ; convert value to pos ADD R0, R0, #1

Instead of multiplying, we need to divide by 100 to get hundreds digit.


Why wouldn't we use a lookup table for this problem? Subtract 100 repeatedly from number to divide. NegSign

First, check whether number is negative.


Write sign character (+ or -) to buffer and make positive.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

237

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

238

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

.FILL .FILL .FILL .FILL .FILL .FILL

x2B x2D x30 xFF9C #100 xFFF6

; ; ; ;

plus sign neg sign zero -100

; -10

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

239

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

240

60

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

C: A High-Level Language
Gives symbolic names to values
dont need to know which register or memory location

Provides abstraction of underlying hardware

Chapter 10 Introduction to Programming in C

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)

Enhances code readability Safeguards against bugs


can enforce rules or conditions at compile-time or run-time

Microprocessor and Microcontroller Course

9/23/2009

Microprocessor and Microcontroller Course

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

242

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Compilation vs. Interpretation


Different ways of translating high-level language Interpretation
interpreter = program that executes program statements generally one line/command at a time limited processing easy to debug, make changes, view intermediate results languages: BASIC, LISP, Perl, Java, Matlab, C-shell

Compilation vs. Interpretation


Consider the following algorithm:
Get W X = W Y = X Z = Y Print from the keyboard. + W + X + Y Z to screen.

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?

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

243

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

244

61

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

Compiling a C Program
Entire mechanism is usually called the compiler Preprocessor
macro substitution conditional compilation source-level transformations output is still C

C Source and Header Files

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

Compiler Source Code Analysis Symbol Table Target Code Synthesis

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

Library Object Files

Symbol Table
Linker

map between symbolic names and items like assembler, but more kinds of information

Executable Image

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

245

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

246

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

247

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

248

62

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

249

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

250

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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.

Input and Output


Variety of I/O functions in C Standard Library. Must include <stdio.h> to use them. printf("%d\n", counter);
String contains characters to print and formatting directions for variables. This call says to print the variable counter as a decimal integer, followed by a linefeed (\n).

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

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

251

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

252

63

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

More About Output


Can print arbitrary expressions, not just variables printf("%d\n", startPoint - counter); Print multiple expressions with a single statement printf("%d %d\n", counter, startPoint - counter); Different formatting options: %d decimal integer %x hexadecimal integer %c ASCII character %f floating-point number

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);

produces this output:


43 43 43 43 is a + 59 + 59 + 59 prime number. in decimal is 102. in hex is 66. as a character is f.

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

253

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

254

Department of Computer Engineering, University of Information Technology

Department of Computer Engineering, University of Information Technology

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

Compiling and Linking


Various compilers available
cc, gcc includes preprocessor, compiler, and linker

Lots and lots of options!


level of optimization, debugging preprocessor, linker options intermediate files -object (.o), assembler (.s), preprocessor (.i), etc.

scanf("%f", &radius);
reads a floating point number and stores it in radius

scanf("%d %d", &length, &width);


reads two decimal integers (separated by whitespace), stores the first one in length and the second in width

Must use ampersand (&) for variables being modified.


(Explained in Chapter 16.)

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

255

Microprocessor and Microcontroller Course

9/23/2009

Computer Systems

256

64

Вам также может понравиться