Download CSCI 150 Introduction to Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Flexible electronics wikipedia , lookup

Computer program wikipedia , lookup

Computer science wikipedia , lookup

Transcript
Introduction to Computer Science
CSCI 150 Section 002
Session 21
Dr. Richard J. Bonneau
IONA Technologies
[email protected]
5/8/2003
CSCI 150: Introduction to Computer Science
1
Today’s Outline
 Today’s ‘notices’
 Tonight’s Topics
– Continuation of Chapter 7 “Digital Circuits”
– Review Binary numbers and Binary arithmetic
– Actually build small circuit to add 1 and then 2 bit
numbers!
 Next Class Topics
5/8/2003
CSCI 150: Introduction to Computer Science
2
Today’s Notices
 Office
 Extension
 Office hours




Swords 339
2282
Tu-Th 4-6 and after class
Extra credit assignments results available !!!!
Any Review of Truth Table to Circuit Procedure Needed??
Any Review of Lab Assignment #7 Exercises Needed??
Any Problems with Circuit Maker 5.0? Or Student Version ??
– F5 - show pin connectors – easier to do/see wiring?
– Right mouse click = clicking on arrow button! ??
– If you use Student Version, you cannot then take your designs to the lab – they are
incompatible
 Homework 7 Due Thurs April 10th
– Please ask any questions about this assignment now!
– Remember printout as well as circuit on diskette
 FINAL EXAM DATE/TIME
– Saturday, May 3 – 2:30 – 5:30 PM in Swords 359
5/8/2003
CSCI 150: Introduction to Computer Science
3
Chapter 7 Outline
 How to Computers Really Work??
 Computer Organization
 Circuits for Computing Primitive Functions
 Circuits for Computing Complex Functions
Tonight !
 Relays
Tonight !
 Binary Number System
 Circuit for Adding Two Binary Numbers
5/8/2003
CSCI 150: Introduction to Computer Science
4
Summary of Last Lecture Sessions
 Computer Organization
 Simple Circuits
– OR
– AND
– NOT
 Switches and logic displays
 Truth tables
 More complex circuits
 Relays as electronic switches
 Converting a Truth Table to an equivalent circuit
 Circuit Maker Lab – last session
5/8/2003
CSCI 150: Introduction to Computer Science
5
Where do truth tables come from???
 So far, you have learned to put
devices/components down and wire them with
switches and lights and see what happens when
you change the input switches
 Or, you can start with a truth table and produce an
equivalent circuit.
 But now we want to design our own circuits to do
things - so SOMEHOW we need to produce truth
tables that represents something we want to do!
 Truth tables are just functions of 0’s and 1’s - we
as humans provide the interpretation of what they
do - what might we wish to do??
 Doing arithmetic is a natural first step in
designing circuits for a “compute”-r!
5/8/2003
CSCI 150: Introduction to Computer Science
6
Number Systems
 To do arithmetic we need NUMBERS
 First there was : The decimal number system
 A positional number system based on the number
ten (10) in which each digit in a number has an
overall value based on which position it occupies
in the number
Recall:
100 = 1
101 = 10
102 = 100
… !
 Example:
hundreds
tens
units
2
1
367 = 3 x 10 + 6 x 10 + 7 (x 10 0)
 Uses only: Digits 0 - 9 and the place values of 10x
5/8/2003
CSCI 150: Introduction to Computer Science
7
Now think (only) in 2’s !
 Binary number system based on powers of 2 and not 10.
 Thus there are only 2 digits - or binary digits - or bits!
and 1 - nothing else
 So all binary numbers look like a series of 0, 1’s:
 Two examples
0001 = 0 x 23 + 0 x 22 + 0 x 21 + 1 x 20
=
0
+
0 +
0
+
1 = 1
(decimal)
1011 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
=
8
+
4
+
0
+
1 = 13
(decimal)
 How to convert from binary to decimal more generally??
5/8/2003
CSCI 150: Introduction to Computer Science
0
Powers
of 2
-----0
1
1
2
2
4
3
8
4 16
5 32
6 64
7 128
8
Binary to Decimal- The Algorithm
 First ask for how many binary digits (bits) ?
 Accept this as the “length”
 Compute 2 to (length-1) power = base = biggest power
of 2
 Set decimal variable = 0 (accumulating decimal value)
23
8
 In a loop while base >= 1
–
–
–
–
ask for next bit: 0 or 1
multiply bit value versus base = current power of 2
add to accumulating sum varaible = decimal
reduce base by dividing by 2 – going to next lower position in
number
 When done loop print decimal value
22 21 20
4
2 1
------------b3 b2 b1 b0
Value =
b3 x 8 +
b2 x 4 +
b1 x 2 +
b0 x 1
 Consider The Pascal Program Binary to Decimal
(pp. 241-242) and BinaryTo.PAS
 Also consider two useful circuit elements: Hexstuff.ckt
– For base 16, but works for all the decimal digits !
5/8/2003
CSCI 150: Introduction to Computer Science
9
5/8/2003
CSCI 150: Introduction to Computer Science
10
Adding Decimal numbers - the mechanics
Add 3 decimal digits now!
Carry
1
First line up the
Two numbers – with
Units over units,
10s over 10s, etc.
0
3
2
1
+
5
9
8
_____________________
9
+
0
1
2
3
0
0
1
2
3
1
1
2
3
4
2
2
3
4
5
3
3
4
5
6
4
4
5
6
7
5
5
6
7
8
and so on ...
5/8/2003
6
6
7
8
9
7
8
7
8
8
9
9 10
10 11
9
9
10
11
12
1*
Carry!
9
Then add, digit by
digit, with
possible carry to
the next position
Using
No carry!
And we learned our addition
tables so that we could
add digits together, especially
all combinations of single digits!
CSCI 150: Introduction to Computer Science
11
How to do arithmetic in binary?
 Adding - first we need the addition table, but for binary
numbers this is quite simple!!
+ | 0
1
------------0 | 0
1
1 | 1
‘10’(= 0 with carry of 1)
 Rest of process is same as decimal, with adding individual
digits (‘bits’) and remembering to carry:
1
Carry
Need to
add
3 bits
now!
5/8/2003
1001 = 8 + 1
+
0101 = 4 + 1
------1110 = 8+4+2
= 9 (decimal)
= 5 (decimal)
---= 14 (decimal)
CSCI 150: Introduction to Computer Science
12
“Algorithm” for 4-Bit Binary Addition
 First we line up the binary numbers (x3 x2 x1
x0) and (y3 y2 y1 y0) so that the ‘units’ are
aligned.
 Then add the two bits in the units column (x0
y0)
 Record the single bit result below the line
(z0)
 AND REMEMBER TO BRING THE CARRY
C1 FORWARD TO THE TOP OF NEXT
COLUMN
 Now we repeat the above on the bits in the
next column except we also add in(c4
any
extra
c3 c2 c1
)
carry bit: add c1 x1 and y1
X3 x2 x1 x0
+ y3 y2 -y1 y0
 Do the same for all the remaining columns
----------5/8/2003 the result is
CSCI
Introduction
to Computer
Science
a 150:
n-bit
number
z3 z2
z1 z0
and
z3 z2 z1 z0
13
4 Bit Addition Example - Revisited
Carry
+
1
1 0 0 1
0 1 0 1
------1 1 1 0
Line up both
binary numbers
Add bits in column
Carry forward to
next column
Major step: Add two bits and
produce sum (and carry) bit
Variation: Add two bits and a carry
5/8/2003
CSCI 150: Introduction to Computer Science
14
Circuit for Adding Two Binary Digits
 So how to do all of this with digital circuits??
 Theme: Break down problem into simple small
(sub)circuits and build up to the full circuit
 First: build a circuit to add 2 1 bit numbers
– (half adder)
 Then: add circuitry to be able to do a carry to the
next digit as well as accept a carry in!
– (full adder)
 Then: combine this 1-bit add with carry with
another to produce 2-bit add with carry and test it
out!
– (2bit adder)
5/8/2003
CSCI 150: Introduction to Computer Science
15
So where are those truth tables???
 Now we are in a position to build truth tables to represent the various (sub)circuits
 From the simplest –
– adding two single bits to produce a single sum bit (and
possible carry bit) – this is known as a half-adder
circuit
 To the more complex –
– adding two bits with a possible carry in and a possible
carry out bit! – this is known as a full-adder circuit
5/8/2003
CSCI 150: Introduction to Computer Science
16
Truth tables for 1 bit add
with carry out - half-adder
Inputs
X
Y
--------------0
0
0
1
1
0
1
1
Outputs
Sum
Carry
------------0
0
1
0
1
0
0
1
So we need a circuit with two inputs (x and y)
and two outputs (sum and carry) which
satisfy the functions defined by truth table
Consider the circuit HalfAdder.ckt
Note SUM function looks like XOR – answer is 1
if exactly one input is 1
Consider HalfAdderEquiv.ckt which uses XOR –
simpler circuit?
5/8/2003
CSCI 150: Introduction to Computer Science
17
5/8/2003
CSCI 150: Introduction to Computer Science
18
5/8/2003
CSCI 150: Introduction to Computer Science
19
But what about the carry in to the
next bit add?
+
1
1
0
1
1
1
1
-------------------0
with carry out
We have a circuit for this first column add
= “Half adder”
But need more! To add in the next column we
need to accept the carry in and add it in too!
5/8/2003
CSCI 150: Introduction to Computer Science
20
Next column needs another bit – Carry in
1
1
0
1
1
1
1
-------------------??
0 with carry out
Need circuit to add 3 bits
and still produce a sum bit
and a carry out
So let’s update the
truth table !!!
5/8/2003
CSCI 150: Introduction to Computer Science
21
Now we need to have add Carry IN
Three Inputs
And
Two Outputs
X
Y
Cin |
---------------0
0
0 |
No
0
1
0 |
Carry in!
1
0
0 |
1
1
0 |
Carry in!
0
0
1
1
0
1
0
1
1
1
1
1
|
|
|
|
Sum
Cout
----- --------0
0
1
0 Same
1
0
0
1
1
0
0
1
0
1
1
1
as before !!
This is
different !!
Let’s look at the FullAdder.ckt enhanced version of the half adder!!
5/8/2003
CSCI 150: Introduction to Computer Science
22
Re-using half-adder subcircuit
5/8/2003
CSCI 150: Introduction to Computer Science
23
Phew - all this to add 1 + 1 !!
 Next step - create multi bit numbers by
combining lower level single bit (full adder)
circuits
 Here we cascade the carry out of lower bits to
the carry in of the higher bits
 Known as ‘ripple circuits’ since the carry bits
can ‘ripple’ from one circuit to the next
 See next slide!!
5/8/2003
CSCI 150: Introduction to Computer Science
24
2 bit adder assembly
Input Bits
Bit 1
X1
Bit 0
X0
1
+
Y1
Y0
------------------------------Cin
Final carry
bit
Full
Adder
Example
Full
Adder
1
0
1
+ 1
1
----------0
0
1
1
So result is
Ripple Adder
Output bits
x =
y =
1
3
Sum bits
z=0
Carry bits
1 0 0 = 4!
Cout
Cout
Z1
Z0
See 2bitadder.ckt for a real version of this design!!
Built up from the Full Adder subcircuits!
5/8/2003
CSCI 150: Introduction to Computer Science
25
Bit 0 Add
Key
Wire
Connects
Bit 0 add with
Bit 1 add
Bit 1 Add
5/8/2003
CSCI 150: Introduction to Computer Science
26
Bigger and Bigger Numbers
 By connecting these simpler circuits, can build
circuits which can add arbitrarily large number
circuits - common are 16 bit integer adders
and 32 bit integer adders, even 64 bit!!!
6 bit
adder
ckt
 Primary design methodology:
– build something for a single bit operations
– connect them together to make multi bit ops
 Can do similar circuits for subtraction (borrow
bit is important here) and multiplication and
division - even comparisons - all major math
operations
 Collection of these circuits: ALU
-
arithmetic logic unit
5/8/2003
CSCI 150: Introduction to Computer Science
27
Summary of Session Topics
 Binary Number System Basics
– Use 2 instead of 10 as base for number representations
– Binary digit = BIT = can only be a 0 or a 1
– Simplified addition table (compared to decimal!)
 Adding in Binary
– Sum bit and carry bit concepts
 Circuits for
–
–
–
–
5/8/2003
1 bit add truth tables
1 bit add with carry out - half adder
1 bit add with carry in and carry out - full adder
2 bit add with carry out - 2 bit adder
CSCI 150: Introduction to Computer Science
28
Next Session
Thursday Evening:
More advanced circuit components
and circuits
Finish up chapter 7 concepts
Homework 7 due on Thursday
Homework 8 to be passed out due in 2 weeks
5/8/2003
CSCI 150: Introduction to Computer Science
29