+ All Categories
Home > Documents > 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

Date post: 05-Jan-2016
Category:
Upload: martin-chandler
View: 214 times
Download: 1 times
Share this document with a friend
41
1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012
Transcript
Page 1: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

1

i206: Lecture 4:The CPU, Instruction Sets, and How Computers Work

Marti HearstSpring 2012

Page 2: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

2

Summary from Last Time• Quick Binary Exercise• Boolean Logic

– deMorgan’s Law

• Relating Boolean Logic to Circuits– Logic gates

• Special gates:– Flip Flops– Adders– Decoders

Page 3: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

3

Revisit DeMorgan’s Laws

AB =

A

B

The inverse or complement of any Boolean expression can be found by successively applying the following theorems: (x + y)' = x'y' (xy)' = x' + y’

Page 4: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

4

How fast is a logic gate?• “Computers used to work in milliseconds

(1,000ths), then moved up to microseconds (millionths), and now are approaching nanoseconds (billionths) for logic operations - and picoseconds (trillionths!) for the switches and gates in chips.” 

• A nanosecond (ns) is one billionth of a second, 10^-9 seconds.

• http://science.nasa.gov/science-news/science-at-nasa/2000/ast28apr_1m/

Page 5: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

5

Example; Linking Boolean to Circuits

Lighting up a digital clock

Page 6: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

6

7 Segment LEDs: How to set the digits

Images from http://www.ee.ed.ac.uk/~gaa/DigilabNotes/Digilab/Components/node7.html

Need a lot of gates to implement this truth table.

Page 7: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

7

7 segment Digital DisplayHow to turn on only 1 of the 4 digits at a time?

Images from http://www.ee.ed.ac.uk/~gaa/DigilabNotes/Digilab/Components/node7.html

a b c d e f g p

i1 i2 i3 i4

Page 8: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

8

n-bit Decoder:Takes as input n inputs; turns on 1 (and only 1) of outputs

Righthand image from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/Circuits/index.html

n2

A B

W

X

Y

Z a b c d e f g p

w x y z

Page 9: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

9

n-bit Decoder:Takes as input n inputs; turns on 1 (and only 1) of outputs

Righthand image from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/Circuits/index.html

n2

A B

W

X

Y

Z

What else could you use this for?

How about selecting between 4 kinds of instructions?(add, subtract, multiply, divide)

Page 10: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

10

How a Flip Flop Works: Try out this tool:

http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/Circuits/index.html

Page 11: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

11

The CPU

CentralProcessingUnit

Page 12: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

12

The CPU• Registers

– Are basically rows of latches– Store values– Can be used to hold values of variables as well as memory

addresses.

• Special registers – PC (program counter)

• Points to the location in memory that has the current instruction.

– Instruction Register• Holds the current instruction.

– Accumulator• Hold the current value of the ALU

Page 13: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

13

The CPU

• ALU (Arithmetic Logic Unit)– Made of a long series of adder units– Addition, Subtraction (Mult, Div)– Lots of gates, so often the slowest circuit in the CPU

Image from http://eleceng.ucd.ie/~rreilly/elecengnotes/LECTURE18/sld017.htm

Page 14: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

14

Circuits for Binary AdditionFull Adder

Images from http://www.play-hookey.com/digital/adder.html

Page 15: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

15

Circuits for Binary AdditionFull Adder, 4 bit output + carry

• Can link up an arbitrary number of input bits

• Can modify this easily to do subtraction

• Can also modify to do multiplication– How?– Usually division is done with a

special floating point chip

Images from http://www.play-hookey.com/digital/adder.html

Page 16: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

16

Arithmetic Logic Unit (ALU)Add, Subtract, and Logic Operations

http://www.seas.upenn.edu/~ee201/lab/LabALU/ALU.html

Page 17: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

17

A key computer science concept:

Code and data can be represented in the same way.

Page 18: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

18

The Stored-Program Concept

• Programs (code) are represented in the same way that data is.

• This way, both code and data can be stored in the same place as well.

• This is a key concept for understanding how the CPU works.

Page 19: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

19

Computer Memory

• We represent data as 0’s and 1’s (bits)– 8 bits in a byte– 2 to 4 bytes in a word – Words represented schematically as rows in memory– Each word has

• High order bit (most significant bit)• Low order bit (least significant bit)

• We also represent locations as 0’s and 1’s– These locations are called addresses

Page 20: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

20

RAM(Random Access Memory)• One set of wires activates the appropriate location• Another set of wires

– reads the data value from that location or– writes the data value to that location

• RAM is arranged as a grid– Each column is a word (or some number of bytes)– First activate the correct column– Then either

• Read the word’s values into the row output, or• Write the row’s values into the word

• Read: get the value (don’t change it)• Write: set the value (change it)

Page 21: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

21

How DRAM WorksThis example memory holds 8 bytes.Say we want to change the contents of memory address number 0101 (which is the fifth one from the right)

Image adapted from http://www.howstuffworks.com/ram.htm

First, activate the column that corresponds to this addressConverting 0101 to 00010000 requires circuitry (not shown)

Page 22: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

22

How DRAM WorksSaw we want to set this memory address to contain the value 01001011 When two activated lines cross, the cellat the intersection gets turned on.

Image adapted from http://www.howstuffworks.com/ram.htm

0 1

0 0

1 0

1 1

Page 23: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

23

How DRAM WorksNow we’ve set the value.Reading moves the data the other direction, onto the row (a special control wire, not shown, tells the circuit whether to read or write).

Image adapted from http://www.howstuffworks.com/ram.htm

Page 24: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

24

Machine Language• Only a very few unique instructions (commands) are

needed for all CPU functionality.• Called:

– Machine instructions– Machine language– Machine code

• Represented in binary• Assembly language: the human-readable version

– Each statement of assembly language corresponds to one machine instruction.

• Need an assembler program to convert assembly language to machine code.

Page 25: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

25

Machine Language

• Why so few instructions?

• (Nearly) everything is built up from:– Add/subtract/compare values (for jumps)– Load (into registers from memory)– Store (from registers into memory)– Get next instruction (or jump to another instruction)

Page 26: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

26

Relation to High Level Languages

• Java, Python, C are all high level programming languages.

• They are translated to assembly language by a compiler and/or an interpreter.

Page 27: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

27

Instructions and Their Representation

Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

The op-code area holds operations.How many unique op-codes can this setup support?

Page 28: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

28

Instructions and Their Representation

Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

Page 29: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

29

Instructions and Their Representation

Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

The Operand area holds memory addressesand data values.

111111 = 1 + 2 + 4 + 8 + 16 + 32 = 63

What is the largest memory address?

Page 30: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

30

The CPU

Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

Bus: bundles of wires connecting things.

Instruction Register: holds instructions.

Program Counter: points to the current memory location of the executing program.

RAM: stores the code (instructions) and the data.

Page 31: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

31

The CPU

Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

MUX: multiplexer – chose 1 out of N (ALU or RAM in this case)

ALU: adds, subtracts, compares two numbers(it has 2 words of storage, on the left and the right)

Accumulator: stores ALU’s output

Decoder: uses the 3-bit op-code and the number bit to control the MUX and the ALU and the memory buses.

Page 32: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

32

Machine code to add two numbers that are stored in memory (locations 13 and 14) and then store the result into memory (location 15).

The Program Counter starts at memory location 0.

Image adapted from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

Page 33: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

33Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

The Program Counter starts at memory location 0.

(Notice: the instructions and the data are both stored in the RAM.)

This instruction moves along the bus into the instruction register.

The decoder checks the number bit to see how to handle the op-code.

Page 34: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

34Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

If the op-code number bit = 0, we want the operand to be used as a memory address.

So the decoder switches the MUX to connect the operand to the RAM.

If the bit were 1, the MUX would connect the operand to the ALU.

The green wires do the addressing of the RAM.

The blue wires do the reading to and writing from the RAM.

Page 35: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

35Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

The Decoder tells the ALU this is a LOAD instruction.

The memory location 13 is activated, and its value placed on the blue bus. This goes through the multiplexer into the ALU. It also trickles down into the Accumulator (Load is implemented as an ADD value to 0).

The last step (always, except for a jump) is for the Program Counter to get incremented. Now it points to memory location 1.

Page 36: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

36Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

Instruction: ADD 14Similar to Load 13, except the ALU is given the ADD operation.

The contents of the Accumulator are fed into the left hand operand, and the contents of memory location 14 becomes the right hand operand.

Results of the AND end up in the Accumulator.

The Program Counter increments.

Page 37: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

37Images from http://courses.cs.vt.edu/~csonline/MachineArchitecture/Lessons/

Instruction: STORE 15

Memory address 15 is activated via the green bus.

The contents of the Accumulator are written to RAM in location 15 using the blue bus.

The Program Counter increments.

The next instruction is HALT.

Page 38: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

38

CPU Performance• CPU cycles are not the whole story• The number of INSTRUCTIONS per second play

a big role.– Each operation on the computer requires some

instructionse.g., about 6 instructions required for the loop part of a simple for-loop.

– Each instruction takes some number of cycles to execute.

– So … a CPU with an instruction set and circuit design that requires fewer cycles to get operations done might have better performance than a CPU with a faster clock but more complicated circuitry.

Page 39: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

39

Measuring CPU Performance

• MIPS: – millions of instructions per second– “meaningless indicator of performance” (a joke)– Some instructions require more time than others– No standard way to measure MIPS

• FLOPS:– floating-point operations per second– Measures the speed of the floating point unit

• A special circuit that does math on real numbers• So, doesn’t measure performance on non-numeric applications

• Benchmark:– A test used to compare performance of hardware and/or software.– Different benchmarks measure different aspects of performance

• SPEC:– Standard Performance Evaluation Corporation– A set of standardized tasks (benchmarks) that CPUs are run on in order to

assess their performance.

Adapted form http://webopedia.internet.com/

Page 40: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

40

CPU Performance

CPU clocks are related to another commonly used term…

Real Time– Means regular clock time as opposed to CPU clock time– Other factors also prevent a program from running in real

time• CPU scheduling, discussed in today’s lecture

In common usage today,– Real time means right now, as I’m talking, as opposed to– Offline, when I have to think on my own, or when we’re

not in the middle of a timed event with others listening.

Page 41: 1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.

41

Summary• Boolean logic is used to make logic gates, which build up into circuits.• Binary is used to represent both code and data in memory, in instruction registers, and in the arithmetic logic unit.• Binary instructions move through the circuits, controlling the computer’s operation and also doing the operations

themselves.– Adding, subtracting, moving locations.

• This is pretty much what it takes to do computation!– After you write the program, compile it, and load it into the CPU and memory.


Recommended