Upload
phunghanh
View
225
Download
6
Embed Size (px)
Citation preview
Chapter 4Hardware Basics
We assume that a computing agent will exe-
cute all the algorithms we have been discussing.
Now, we will see how this agent looks like and
how it is able to execute those algorithms.
We will first look at the very basics, such as
how to represent data, how to follow the logic
principles to design gates, and how to combine
gates to construct logic circuits that manipu-
late data. This part is called hardware design
or logic design.
1
Binary system
We are used to follow the decimal represen-
tation of numbers, e.g., 261. When we use
computers to process data, we still use this
system as the external representation, but it
will not be used as the internal representation.
Instead, the computer will use the binary num-
bering system.
2
Binary or decimal?
Question: What does 261 mean?
Answer: It means two hundreds and sixty one.
It has the following decimal representation:
261 = 2 × 100 + 6 × 10 + 1
= 2 × 102 + 6 × 101 + 1 × 100
= (261)10.
Similarly, it also could be represented in a bi-
nary system.
261 = 256 + 4 + 1 = 1 × 28 + 0 × 27
+ 0 × 26 + 0 × 25 + 0 × 24 + 0 × 23
+ 1 × 22 + 0 × 21 + 1 × 20
= (100000101)2.
The two digits 0, 1, are called bits.
3
A conversion table
Binary Decimal Binary Decimal0 0 10000 161 1 10001 17
10 2 10010 1811 3 10011 19
100 4 10100 20101 5 10101 21110 6 10110 22111 7 10111 23
1000 8 11000 241001 9 11001 251010 10 11010 261011 11 11011 271100 12 11100 281101 13 11101 291110 14 11110 301111 15 11111 31
Generally speaking, any whole number can berepresented in this “base-2” representation.
Homework: Exercises 1–6.4
Representing fractions
Decimal numbers, such as 12.34 and -43.21,
can also be represented in the binary system,
by first converting them into the scientific sys-
tem: ±M × B±E, where M is the mantissa, B
is the base, and E is the exponent.
For example,
(5.75)10 = 4 + 1 + 0.5 + 0.25
= 1 × 22 + 0 × 21 + 1 × 20
+1 × 2−1 + 1 × 2−2
= (101.11)2.
Homework: Exercises 7.
5
A couple of notes
1. As computer is a finite device, there is a
maximal number of binary digits that can be
used to store digits, e.g., 16, 32, or 64. Once
this number is fixed, the maximal integer that
can be represented is also fixed. For example,
for a 16 bit machine, the maximum integer is
216 = 65,535. Any operation in this machine
that produces a larger integer will lead to an
overflow error.
2. Binary system can be used to represent
other things as well. For example, to represent
a signed number, we can use the leftmost bit
to represent the sign, 0 for ‘+’, and 1 for ‘-’.
Hence, -49 and 3 will be represented, respec-
tively, as (1110001) and (0000011), assuming
we use 7 digits.
Homework: Exercise 8.6
Moreover, we can also use it represent textinformation. There is no the way to do so, wejust assign a binary number, usually with 8 bits,to every text symbol we want to represent.ASCII is the most widely used representationschema. For example,
Symbol Decimal Value Binary Value! 33 00100001... ... .../ 47 001011110 48 00110000... ... ...9 57 00111001... ... ...A 65 01000001... ... ...Z 90 01011010... ... ... 126 01111110
Hence, BAD=00000010︸ ︷︷ ︸B
00000001︸ ︷︷ ︸A
00000100︸ ︷︷ ︸D
.
Homework: Exercise 9.7
UNICODE representation
Since ASCII code has only 8 bits, it can repre-
sent up to 256 different symbols, not enough
for today’s world. Thus, the new scheme of
UNICODE gains popularity since it uses 16
bits, and can represent up to 65, 536 different
symbols. It can be used to represent symbols
drawn from other alphabets, such as Russian,
Chinese, etc..
This is also the representation used in the JAVA
language.
For a full discussion of UNICODE, please have
a look at www.unicode.org.
8
Sound and images
The key to represent other forms of informa-
tion, such as sound and image, is based on thedigitization of those information.
For example, given a B/W picture, we can putit in a grid to convert it into a bunch of pixels,namely, elements of picture. We then assign
either a 1 or 0 to each pixel, depending on ifthat picture is black or white. For a color pic-
ture, besides identifying the position, we alsoneed to assign its share in three of the basic
colors: red, blue, and green, each of which istypically represented using a byte (8 bits).
Hence, a picture taken with a 3 MegaPixel digi-
tal camera using 24 bit color occupies 72 Megabits.
Homework: Complete §4.2.2 and completeExercises 10 and 11.
9
Data compression
Since the information storage needs so muchspace, the subject of data compression be-comes really important. A simple compressiontechnique is run-length encoding, which re-places a sequence of identical values v1, v2, · · · , vn
with a pair (v, n). If each v and n requires 1byte, then the total number of bytes will bereduced from n to 2, if n ≥ 2. For example,the following data
255 255 255255 0 0255 255 255255 0 0255 255 255
can be represented as the following list:
(255, 4) (0, 2) (255, 4) (0, 2) (255, 3), thusreducing the total bytes from 15 to 10, a ratioof 15/10=1.5.
10
Variable length code
In ASCII code, every character is coded in 8
bits. So, if we have a text file with 1,000 char-
acters, we have to use 8,000 bits to store it.
In reality, some characters are used more often
than the others (Think about Wheel of For-
tune). It makes sense to assign shorter codes
to those used more often, and longer codes to
those used less often. The question is how?
One approach is to assign code to symbols
based on their frequencies. For example, in
the string aaxuaxz, the frequency of a, x, u
and z are 3, 2, 1 and 1. When frequency
varies a great deal, it makes sense to assign
shortest code to the most frequently occurring
one, while assigns longest code to the least fre-
quently occurring symbol.
11
For the above example, we can assign the codesas follows: 0=a, 10=x, 110=u, 111=z). Hence,aaxuaxz will be coded as 0010110010111. Thelength is 13 bits, compared with 14, if we giveeach of them two bits. On the other hand, ifthe frequency of the four symbols are (996, 2,1, 1), then the 2 bits per code method, for a1,000 character file will lead to 2,000 bits long,while our code will lead to a file of only 1,006bits, a ratio of almost 2.
How could we decode? For 0010110010111,since we have no code ”00”, the first piece ofcode must be ”0”, which is an ’a’, the nextis also ’a’. As we do have a code ”10”, weread off an ’x’, etc.. So, we always read offthe longest possible piece from the undecodedstring. The reason that this method worksis because this is a prefix code, i.e., no codeis a prefix of another. Such a code is calledHuffman code.
Homework: Exercises 12 and 13.12
Why binary?
There is absolutely no theoretical reason why
we can’t build up a base-10 computer, or a
computer based on any notation system. The
use of binary system is wholly a reliability con-
cern.
The computer is an electronic equipment so
that the internal representation must be based
on such quantities as currents or voltages. As
a result, we must, say the least, use 10 dif-
ferent voltages to represent the 10 digits. For
example, 0v for 0, 5v for 1, 10v for 2, · · · , 45v
for 9. So, to represent 28, we uses two de-
vices, one is charged to 10v, and the other to
40v.
The problem is that voltages are not stable.
When the voltage of the second drops to 37.5,
what number does it represent?
13
The trouble is that in representing a base-10
system, we need 10 stable states. Such a sys-
tem is difficult to come by. On the other hand,
an electrical system works best in a bi-stable
environment, where there are only 2 states,
separated by “huge” energy gap. For example,
on-off, fully charged-fully discharged, charged
positively-charged negatively, etc.
In such a system, there are two stable states.
We can let one, e.g., 0v, represent the digit
0, the other, e.g., 45v, represent the digit 1.
Now, a mere 10% voltage change would not
cause any problem.
The use of the binary system for the internal
data representation significantly increases the
inherent reliability of modern computer.
14
Binary storage devices
combined with our earlier results, we know that
any information can be represented by using bi-
stable devices that 1) has two stable states; 2)
it is possible to know what state the device is
in; and 3) it is possible to switch the device
from one state to the other.
There are many such devices, such as light
switch. Thus, at least theoretically, we can
build a computer out of a bunch of light switches.
Modern computers are built on such other much
smaller, more efficient, and stable devices. We
discuss two of them: magnetic core and tran-
sistor.
15
Magnetic core
This type of device was used to construct com-
puter memories, roughly from 1955 to 1975.
It is a small magnetizable iron oxide-coated
“doughnut”, about 1/50 of an inch in its di-
ameter, with one or more wires through its
center hole. If electric current is sent through
the wire in specific way, the core will be mag-
netized in one of two ways, to represent 0 or
1.
Since magnetic fields do not change much over
time, this device is very stable.
16
Transistors
Today, transistor is the building block for com-puter memories. It is simply a tiny switch. But,it has no mechanical or moving parts. Theswitching is done electronically, rather thanmechanically. This allows it to be extremelyfast as well as small. It can be switched inabout 10-20 billions of a second, while severalmillions of transistors can fit in a space onecentimeter square.
Constructed from semi-conductor material, tran-sistors, together with some connecting wires,can be printed photographically on a layer ofsilicon to produce integrated circuits (chips),which will be used as functional parts inside acomputer.
Photographic techniques, such as VLSI andULSI, allow us to produce chip with billionsof transistor/cm2, which leads to smaller andfaster computers.
17
Boolean logic and gates
The construction of chips is based on Boolean
logic, which makes operations in base-2 possi-
ble. This logic has two variables, T(rue) and
F(alse.) Interpreting 0 as F and 1 as T, the re-
lationship between this logic and binary system
is immediately established.
There are three operations, AND(∧), OR(∨)
and NOT(¬), in Boolean logic. A Boolean
expression is essentially a combination of those
operation applied on boolean variables. For
example, (a ∧ b) ∨ ¬c is a boolean expression.
18
Boolean operations
Let a and b be two Boolean expression. Then
a∧b is true iff both a and b are true, a∨b is true
iff at least one of a and b is true, ¬a is true iff a
is false. These rules can be demonstrated with
the following truth tables, when true and false
are represented with 1, and 0, respectively.
Homework: Exercises 15 and 16.
19
Gates
A gate is an electronic device that operates ona collection of binary inputs to produce a binaryoutput. Although there are a wide range ofgates, we only consider those that implementthe three Boolean operations, as they are theonly things we need.
Below show the implementation of OR gate,one of the three basic ones, by using transis-tors.
20
Building circuits
A (combinational) circuit is a collection of (AND,
OR and NOT) gates that 1) transforms a set
of binary inputs into a set of binary outputs,
and 2) where the values of the outputs depend
only on the current values of inputs. For exam-
ple, the following diagram shows such a circuit,
where it implements the following Boolean ex-
pression:
c = a ∨ b
d = ¬((a ∨ b) ∧ (¬b))
21
What to output?
For any circuit, once the input values are de-
termined, we can use truth tables to decide
the output(s). Thus, given any circuit, we can
construct a truth table to show its behavior.
For example, below is the truth table for the
previous circuit.
a b c d0 0 0 10 1 1 11 0 1 01 1 1 1
22
Circuit construction
We study the opposite problem: how could we
construct a circuit to solve a problem?
There are a number of such circuit construc-
tion algorithms. They all consist of a few ma-
jor steps:
1. Construct a truth table for the expression.
2. Construct sub-expressions using AND and
NOT gates.
3. Combine sub-expressions using OR gates.
4. Draw diagram(s).
23
An example
1. We begin with the following truth table.
a b c O1 O20 0 0 0 10 0 1 0 00 1 0 1 10 1 1 0 11 0 0 0 01 0 1 0 01 1 0 1 11 1 1 0 0
24
Output construction
2. We fix up one output column, say O1. For
every 1 occurs in that column, we construct
a subexpression with the corresponding inputs
by using ∧, in which, if the value for an input
is 1, we use the input itself; otherwise, we use
its negation. For example, as there are only
two 1’s in the column for O1, there are two
such subexpressions, (¬a ∧ b ∧ ¬c) and (a ∧ b ∧¬c). Similarly, we have four expression for O2 :
(¬a ∧ ¬b ∧ ¬c), (¬a ∧ b ∧ ¬c), (¬a ∧ b ∧ c) and
(a ∧ b ∧ ¬c).
3. As either of these subexpressions leads to
a 1, we finish the construction by combining
these subexpression, for each output, with the
∨ operator. For example,
O1 = (¬a ∧ b ∧ ¬c) ∨ (a ∧ b ∧ ¬c).
25
Draw circuits
4. Based on the Boolean expression, we can
draw diagrams for corresponding circuits. For
example, we have the following implementa-
tion for O1.
26
Is this the simplest?
Generally speaking, there could be lots of equiv-
alent circuits for the same truth table. We
always want to have an optimal circuit, in the
sense that it uses the smallest number of gates.
For example, we actually have the following
simpler expression for O1, which leads to a
much simpler circuit.
O1 = b ∧ ¬c.
Homework: Exercises 17 and 18.
27
Another example
We want to construct a circuit to test two un-
signed binary numbers for exact equality. It will
produce a 1, if those two numbers are equal,
a 0 otherwise.
We begin with a simpler circuit, 1-CA, which
tests equality for two 1-bit numbers. We find
out that
O = (a ∧ b) ∨ (¬a ∧ ¬b),
based on the following truth table.
a b O0 0 10 1 01 0 01 1 1
28
We then combine n 1-CE’s to construct a CA
circuit to test equality for two n−bit numbers.
Homework: Exercises 19 and 20.
29
An addition circuit
We now construct a circuit to add two n− bit
binary numbers, a full adder. For example,
0 0 1 1 0 1+ 00 01 11 10 10 0
0 1 1 0 1 1
Hence, the circuit should have three inputs,
two for digits, one for a carry; and two outputs,
one for sum, the other for a carry for the next
bit.
30
Generally, we have the following truth table.
ai bi ci−1 si ci0 0 0 0 00 0 1 1 00 1 0 1 00 1 1 0 11 0 0 1 01 0 1 0 11 1 0 0 11 1 1 1 1
Following the construction algorithm, we have
the following:
si = ((¬ai)(¬bi)ci) ∨ ((¬ai)bi(¬ci))
∨ (ai(¬bi)(¬ci)) ∨ (aibici),
ci = ((¬ai)bici) ∨ (ai(¬bi)ci)
∨ (aibi(¬ci)) ∨ (aibici).
31
Based on the expressions for si and bi, we can
draw the following diagram.
32
The n−bit adder
We combine n one-bit full adder to form ann−bit adder, as follows:
As each 1-bit adder uses 3 NOT gates, 16AND gates and 6 OR gates, a 32-bit adderuses 800 gates, or 1,504 transistors, in total.
Homework: Exercise 21.33
Control circuits
We saw how could we implement circuits for
arithmetic and logic operations, now, we have
a brief look at some of the other essential cir-
cuits: multiplexer and decoder. A multiplexer
has 2N input lines, N select lines, and 1 output
line. Each of the select line can be feed a value
of 0 or 1, the collection of which decides which
input line will be selected to send its value out.
34
Implementing a multiplexer
It is quite easy to construct such a control
device. Below is the diagram for a multiplexer
with two input lines.
When the select line sends in a value of 0, the
upper input is selected; otherwise, the lower
line is selected. The Boolean expression is
O = (I0 ∧ ¬S) ∨ (I1 ∧ S).
35
Decoder
A decoder has N input lines, numbered 0, 1,
. . . , n-1 and 2N output line, 0, 1, . . . ,2N − 1.
Again, each of the input can be either 0, or 1.
With the collection of all the inputs, it decides
which of the 2N outputs should be selected.
In application, it can be used to select which
operation unit should be used.
36
Implementing a decoder
It is also easy to design a decoder. Below is a
diagram for a decoder with 2 input lines.
Homework: Exercises 23 and 24, as well as
Challenge work 1 and 2.
37