comp 4—power tools for the mind 1 what’s in the box? computer architecture, part 1: what we’ll...

28
COMP 4—Power Tools for the Mind 1 What’s in the box? 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!

Post on 20-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

COMP 4—Power Tools for the Mind 1What’s in the box?

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 2What’s in the box?

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 3What’s in the box?

—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

CU ALUTwo mainfunctional units:

clock

Registers

COMP 4—Power Tools for the Mind 4What’s in the box?

• 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 5What’s in the box?

Sample of a partial Basic instruction set

Instr: Meaning:STO Store data in a particular memory locationADD Add two numbers togetherSUB Subtract one number from anotherMUL Multiply two numbersDIV Divide two numbersINC Increment a number by adding 1CMP Compare two numbers to see if they are equalJMP 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 6What’s in the box?

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 7What’s in the box?

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 8What’s in the box?

E.G.: An ADD instruction in a 16-bit machine language: 0101 110011 111100

Op-code: Operands: (RAM or Register addresses)

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

Putting it all togetherProcessor: simple minded! repeats same steps over & over...

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.

Instruction Cycle: in the Control Unit

MACHINE CYCLE: “Instruction-Execution cycle”

Processing of a single machine-level instruction (one Op Code) in a basic machine.

9

,A single Instruction cycle (Add)

CONTROL UNITCONTROL UNIT

Pointer

Storage register

Address reg

Address reg

000001PROCESSOR

Instr. reg.

Imprecise data representation, and a vast simplification--but general concepts are correct.

2b Enter execute cycle

ALUALU3000011

Advance pointer

MEMORYMEMORY0101 1100 1111 1100 85 data shown in base 10

65 data shown in base 10

#000001

#000011

#110011 ….

10

#000010

0111 1110 0111 1110#000100 #111100 ….

0101

110011

111100Copy & Decode

2a

0101 110011 111100Fetch1

,Another single Inst Cycle (Divide)

CONTROL UNITCONTROL UNIT

Pointer

Storage register

Address reg

Address reg

000011PROCESSOR

Instr. reg.

Imprecise data representation, and a vast simplification--but general concepts are correct.

2b Enter execute cycle

ALUALU3000101

Advance pointer

11

0111

111001

111110Copy & Decode

2a

MEMORYMEMORY0101 1100 1111 1100 85 data shown in base 10

65 data shown in base 10

#000001

#000011

#110011 ….#000010

0111 1110 0111 1110#000100 #111100 ….

0111 111001 111110Fetch1

– 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, …

Execution Cycle: by the CU/ALU

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

COMP 4—Power Tools for the Mind 13What’s in the box?

MEMORYMEMORY

PROCESSOR

Execution cycle

C.U.C.U.

Enter execute cycle

ALUALU

Adder

Register

Register

Accumulator1 Data loaded to registers

85

65

Data values added

2

85 + 65= 150

Result copied to Accumulator

3

150

Result stored in memory

150

4

0101 1100 1111 1100 85 data shown in base 10

65 data shown in base 10

#000001

#000011

#110011 ….#000010

0111 1110 0111 1110#000100 #111100 ….

#111110 ….

COMP 4—Power Tools for the Mind 14What’s in the box?

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 15What’s in the box?

Truth tables

NOT truth table

1 Input1 Output

01

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

AND truth tableAND truth table AND truth table

2 Inputs1 Output

0 00 11 01 1

OR truth table

1 Output

0 00 11 01 1

2 Inputs

0001

10

0111

Homework? Clean?

STRICT Parents

Homework? Clean?

COOL Parents Stubborn child

COMP 4—Power Tools for the Mind 16What’s in the box?

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 17What’s in the box?

Transistor

How does a transistor work?

Transistor(Switch)

Current trying to flow through

Input Voltage

If input voltage is low (binary 0) the switch opens and the current cannot pass through.

If input voltage is high (binary 1), the switch closes and the current passes through freely.

COMP 4—Power Tools for the Mind 18What’s in the box?

Logic GatesTransistors 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 19What’s in the box?

NOT gate

Input Voltage

Steady current (high)

Ground(low)

Output Voltage

When input is low, then switch is open, and high current flows to output.

When input is high, then switch is closed, and the current is grounded (instead of passing into output), therefore the output voltage is low.

logical operator: NOT Low input becomes high output.High input become low output.

IN OUT

0 1

1 0

COMP 4—Power Tools for the Mind 20What’s in the box?

NAND gate (NOT AND)

Input Voltage 2

Steady current(high)

Ground(low) Output

Voltage

Input Voltage 1

If both inputs are High, then current passes through, and output is Low.

If either input is Low, switch closes, and output is High.

IN1 IN2 OUT0 0 10 1 11 0 11 1 0

COMP 4—Power Tools for the Mind 21What’s in the box?

NOR gate (NOT OR)

Input Voltage 2

Steady current (high)

Ground(low)

Output Voltage

Input Voltage 1

Ground(low)

If both inputs are Low, then neither switch is closed, and output is High.

If either input is High, a switch opens, and output is Low.

IN1 IN2 OUT0 0 10 1 01 0 01 1 0

COMP 4—Power Tools for the Mind 22What’s in the box?

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

How can binary circuits ADD?

10

1

0+

You will not have to reconstruct diagrams!But you should know how to evaluate Boolean ops.

OutputBit

CarryBit

OR AND

AND NOT

1

0

0

1

1

16

How can binary circuits ADD?

01

1

1+

OutputBit

CarryBit

OR AND

AND NOT

1

1

1

0

0

17

COMP 4—Power Tools for the Mind 25What’s in the box?

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 26What’s in the box?

Important digression!– What kind of code do programmers use?

Translator(Compiler)

High-level language(source code) Machine language

(object code)

e.g.: Turing

Source Code

Machine Codeor Object code

Translationor Compilation

– What is the only kind of code a CPU understands?

– So, what has to occur before the CPU can execute a source-code program?

20

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

Basic Instruction Set: Primitive commands (log’l) it can do Computer circuits (phy’l) it uses

Source code

Answer := Num1 + Num2

Translator prog: Source to object

Object code (Low-level ML) so CPU can understand it: LOAD M012 R07

LOAD M013 R08 ADD R07 R08 R02 STORE R09 M033

Silicon-based unit

CPU (obeys orders to

transform raw data into meaningful info.)

as binary codes

STO; ADD; SUB; MUL; DIV;INC; CMP; JMP…

Johnny

Grandpa’s

command

Mom translates

Child talk

what Johnny can do

21

0010 00001100 0111 0010 00001101 1000

0101 0111 1000 0010 0011 0010 00100001