Transcript Lec10

Computer architecture, part 1:
What we’ll cover for this
lecture topic:
– What is a CPU?
– How is a program executed
by the CPU?
– Boolean logic
– Binary circuits that can add!
COMP 4—Power Tools for the Mind
What’s in the box? 1
Central Processing Unit (CPU):
– Carries out the program’s instructions!
• Operates on data it finds in the computer’s memory.
– Also called Microprocessor
• Includes all binary circuits that carry out arithmetic &
logic operations---reduced to a single IC.
– CPU has four key parts that we will examine:
• Control Unit
• Arithmetic & Logic Unit
• Registers
• Clock
And, of course, wires that connect everything
together.
COMP 4—Power Tools for the Mind
What’s in the box? 2
—CONTROL UNIT (CU): circuitry for coordinating
machine’s activities. Controls sequence of operations.
—ARITHMETIC & LOGIC UNIT (ALU): circuitry
to perform data manipulation (arithmetic & logic).
—Registers: Temporary storage areas. Holds
information applicable to the current operation.
—Clock: Triggers start and stop of all CPU operations.
(heartbeat)
CPU
Two main
functional
units:
COMP 4—Power Tools for the Mind
CU
ALU
clock
Registers
What’s in the box? 3
• CPUs support a set of very simple
instructions that typically fall into the
following categories:
• Data movement (load, store, copy…)
• Arithmetic/logical (add, subtract,
compare..)
• Program control (branch, halt…)
– Very primitive commands (operations) executed
by the CPU [logical structure]
– These commands are implemented as electronic
binary circuits [physical structure] which can
transform the 0s and 1s.
BASIC INSTRUCTION SET
COMP 4—Power Tools for the Mind
What’s in the box? 4
Sample of a partial Basic instruction set
Instr:
Meaning:
STO
ADD
SUB
MUL
DIV
INC
CMP
JMP
Store data in a particular memory location
Add two numbers together
Subtract one number from another
Multiply two numbers
Divide two numbers
Increment a number by adding 1
Compare two numbers to see if they are equal
Jump to a specific position in the instruction code
• Instructions are given to the processor in the form of
a program … so it knows what circuits to use, in
what order; and from where the data should be read
or to where it should be stored.
COMP 4—Power Tools for the Mind
What’s in the box? 5
More specifically:
• A program consists of a sequence of
instructions.
• EACH instruction specifies both:
– The operation to perform
– The address of the data it will transform in that
operation (if necessary).
• Instructions are stored and processed in machine
language--also called microcode.
• Like everything else (e.g. like ASCII characters,
pixels of an image, …) machine language consists
solely of bit patterns.
COMP 4—Power Tools for the Mind
What’s in the box? 6
Machine language or Machine Code
a/k/a microcode
– ML bit patterns are based directly on CPU’s instruction
set (on its binary circuits).
So who created them?
– CPU chip: designed to recognize certain bit patterns as
representing certain ML instructions, which correspond
directly to certain available binary circuits.
– Each ML instruction contains a fixed-length
instruction code that:
• Identifies the operation to perform: op code
• Tells the CPU how to determine the operands
For example:
assume a 4-bit op-code + two 6-bit operands
= one 16 bit instruction
COMP 4—Power Tools for the Mind
What’s in the box? 7
E.G.: An ADD instruction in a 16-bit machine language:
0101 110011 111100
Op-code:
Operands:
0101
(RAM or Register addresses)
110011 111100
• Before it can process a M.L. instruction, the CPU
must fetch it from main memory.
• The CPU must keep track of its position in the
instruction code. It uses a bookmark of sorts.
• The offset at which the next instruction starts is stored
in a special register, called the Program Counter or
Instruction Pointer. Important digression…
– who knows if bits in memory are data or program … ?
– increments to memory address of next instruction ...
COMP 4—Power Tools for the Mind
What’s in the box? 8
Putting it all together
Processor: simple minded! repeats same steps over & over...
MACHINE CYCLE: “Instruction-Execution cycle”
Processing of a single machine-level instruction (one
Op Code) in a basic machine.
Instruction Cycle: in the Control Unit
1. FETCH
• Fetches instruction (from memory) at address given by
instruction pointer; copies it into CU storage register.
2. COPY & DECODE
• Copies op code into instruction register, operands into address
registers.
• Interprets instruction. ALU is invoked to perform decoded
operation for any ALU ops (enters Execution Cycle)
3. ADVANCE INSTRUCTION POINTER (counter)
• Pointer incremented; contains memory address of next
instruction that will be fetched.
9
Imprecise data representation, and a vast simplification-but general concepts are correct.
A single Instruction cycle (Add)
3
,
2b
000001 Advance CONTROL UNIT
000011 pointer
110011
Pointer
0101
Instr. reg.
1
Fetch
2a
Copy &
Decode
0101 110011 111100
PROCESSOR
ALU
Enter execute
cycle
Address reg
111100
Address reg
Storage register
0101 1100 1111 1100
#000001
#000010
0111 1110 0111 1110
#000011
#000100
85 data shown in base 10 MEMORY
#110011 ….
65 data shown in base 10
#111100 ….
10
Imprecise data representation, and a vast simplification-but general concepts are correct.
Another single Inst Cycle (Divide)
3
,
2b
000011 Advance CONTROL UNIT
000101 pointer
111001
Pointer
0111
Instr. reg.
1
Fetch
2a
Copy &
Decode
0111 111001 111110
PROCESSOR
ALU
Enter execute
cycle
Address reg
111110
Address reg
Storage register
0101 1100 1111 1100
#000001
#000010
0111 1110 0111 1110
#000011
#000100
85 data shown in base 10 MEMORY
#110011 ….
65 data shown in base 10
#111100 ….
11
Execution Cycle: by the CU/ALU
– Execution cycles vary, depending on the op code.
• Eg: load register R03 with contents of memory cell 47:
Just for culture: The CU causes the load to occur.
• CU activates ALU circuitry, which performs the actual
Op Code. EG: MUL, ADD, DIV, …
E.G: an arithmetic operation: ADD
1. LOAD
• Data copied from memory to ALU register.
2. ADD
• Data values are added in ALU adder circuitry.
3. COPY
• Result stored in ALU accumulator.
4. STORE
• Result copied from ALU to memory.
12
Execution cycle
PROCESSOR
C.U.
Enter execute
cycle
AL
U
85 + 65
= 150
3
Result
copied to
Accumulator
2
Data values
Adder added
85
Register
65
Register
150
4
Accumulator
1 Data loaded to registers
MEMORY
0101 1100 1111 1100
#000001
#000010
0111 1110 0111 1110
#000011
#000100
COMP 4—Power Tools for the Mind
85 data shown in base 10
#110011 ….
65 data shown in base 10
#111100 ….
150
#111110 ….
Result
stored in
memory
What’s in the box? 13
Boolean logic
• Boolean algebra: solve complex math problems
by reducing them to questions answered by Y/N.
– Logic gate: Mathematical operation that handles
only data that is represented in one of only two states:
TRUE/FALSE
– Physical gate: electronic device that transforms
binary input(s) to produce a single binary output,
according to its transformation rule.
– Many computer binary circuits employ complex
Boolean logic to perform various tasks.
• Such circuits can test the truth of propositions by making
comparisons, as well as carry out complex calculations.
• Gates: AND, OR, NOT
COMP 4—Power Tools for the Mind
What’s in the box? 14
Truth tables
STRICT Parents
AND
AND
truth
truth
table
table
COOL Parents
OR truth table
Homework? Clean?
Homework? Clean?
2 Inputs 1 Output
0
0
1
1
0
1
0
1
0
0
0
1
2 Inputs 1 Output
0
0
1
1
0
1
0
1
0
1
1
1
Stubborn child
NOT truth table
1 Input 1 Output
0
1
1
0
– Engineers design binary computer circuits
using gates based on truth tables.
– Gates: building blocks of all digital devices!
– Such circuits can carry out +, –, *, /,
comparisons, and many complex operations.
COMP 4—Power Tools for the Mind
What’s in the box? 15
Digital Logic
• Remember, the fundamental part of the digital
computer is the switch.
• A switch simply lets current either flow
completely or not at all (binary) :
–
–
–
–
Relay: mechanical switch
Vacuum tubes: electronic switch
Transistors: electronic switch
ICs: use many transistors on a circuit board
• Speed of computer determined by how fast a
switch “switches”!
COMP 4—Power Tools for the Mind
What’s in the box? 16
Transistor
How does a transistor work?
If input voltage is low (binary 0) the
switch opens and the current
cannot pass through.
Input
Voltage
Current trying
to flow
through
Transistor
(Switch)
If input voltage is high (binary 1), the
switch closes and the current passes
through freely.
COMP 4—Power Tools for the Mind
What’s in the box? 17
Logic Gates
Transistors are only switches. They simply
determine whether or not current can pass
through a wire.
Transistors are used to create logic gates.
These gates perform logic operations on the
input voltages and give the result in form of
an output voltage.
Let’s construct a few simple gates to see how
this works…
COMP 4—Power Tools for the Mind
What’s in the box? 18
NOT gate
When input is low, then switch is open,
and high current flows to output.
Steady current
(high)
When input is high, then switch is
closed, and the current is grounded
(instead of passing into output), therefore
the output voltage is low.
Output
Voltage
Input
Voltage
logical operator: NOT
Low input becomes high
output.
High input become low
output.
COMP 4—Power Tools for the Mind
IN
OUT
0
1
1
0
Ground
(low)
What’s in the box? 19
NAND gate (NOT AND)
IN1 IN2 OUT
0
0
1
0
1
1
1
0
1
1
1
0
Input
Voltage 1
Input
Voltage 2
If both inputs are
High, then current
passes through, and
output is Low.
If either input is Low,
switch closes, and
output is High.
Steady
current
(high)
Ground
(low)
COMP 4—Power Tools for the Mind
Output
Voltage
What’s in the box? 20
NOR gate (NOT OR)
If both inputs are Low, then
neither switch is closed, and
Steady current
output is High.
(high)
If either input is High, a switch
opens, and output is Low.
Output
Voltage
Input
Voltage 1
IN1 IN2 OUT
0
0
1
0
1
0
1
0
0
1
1
0
Input
Voltage 2
Ground
(low)
COMP 4—Power Tools for the Mind
Ground
(low)
What’s in the box? 21
Digital Logic
• We have only created NOT, NAND, and
NOR. We wanted ANDs and ORs as well…
• Just feed output of NAND and NOR into
NOT gate.
• Actually, all boolean logic expressions can
be constructed just from NAND or from
NOR – complete!
• Demo: 4-bit half adder
COMP 4—Power Tools for the Mind
What’s in the box? 22
How can binary circuits ADD?
1
1
OR
1
AND
Output
Bit
+
0
0
AND
0
NOT
1
1
You will not have to reconstruct diagrams!
But you should know how to evaluate Boolean ops.
0
Carry
Bit
16
How can binary circuits ADD?
1
1
OR
0
AND
Output
Bit
+
1
1
0
AND
1
NOT
0
1
Carry
Bit
17
What about other operations?
• Subtraction
– Addition using two’s complement representation
• Multiplication
– Repeated addition (with bit shifting)
• Division
– Repeated subtraction (with bit shifting)
Basically, a computer just adds.
COMP 4—Power Tools for the Mind
What’s in the box? 25
Important digression!
– What kind of code do programmers use? Source Code
– What is the only kind of code a CPU Machine Code
understands?
or Object code
– So, what has to occur before the CPU Translation
can execute a source-code program? or Compilation
High-level language
(source code)
Machine language
(object code)
e.g.: Turing
Translator
(Compiler)
COMP 4—Power Tools for the Mind
What’s in the box? 26
An Analogy
Carbon-based unit
JOHNNY, age 4 (obeys orders to
transform raw material into tasty sandwiches.)
Basic Instruction Set:
Simple commands he can do: Go, Bring, Unwrap, …
Child brain, hands, skills he uses.
Grandpa’s commands:
“Please make 2 p&j sandw’s on rye!”
Mom: Grandpa language to Child Talk
Child-talk instructions to
Johnny that he understands:
GO to pantry.
BRING bread & PB to kitchen.
UNWRAP bread.
PULL OUT 4 slices ….
20
Silicon-based unit
CPU (obeys orders to
Johnny
transform raw data into meaningful info.)
STO; ADD; SUB; MUL; DIV;
INC; CMP; JMP…
what Johnny
Basic Instruction Set: can do
Primitive commands (log’l) it can do
Computer circuits (phy’l) it uses
Grandpa’s command
Source code
Answer := Num1 + Num2
0010 00001100 0111
0010 00001101 1000
0101 0111 1000 0010
0011 0010 00100001
Translator prog: Mom translates
Source to object
Object code (Low-level ML) Child
talk
so CPU can understand it:
as binary codes
LOAD
LOAD
ADD
STORE
M012 R07
M013 R08
R07 R08 R02
R09 M033
21