31
1 1 John Magee 10 February 2014 Some material copyright Jones and Bartlett CS140 Lecture 09b: The Machinery of Computation: Computer Architecture 2 Overview/Questions – What did we do last time? – How can we control the computer’s circuits? – How does the von Neumann Computer work? – What is machine language? – What is computer programming?

CS140 Lecture 09b: The Machinery of Computation: Computer

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS140 Lecture 09b: The Machinery of Computation: Computer

1

1

John Magee 10 February 2014

Some material copyright Jones and Bartlett

CS140 Lecture 09b:

The Machinery of Computation:

Computer Architecture

2

Overview/Questions

– What did we do last time?

– How can we control the computer’s circuits?

– How does the von Neumann Computer work?

– What is machine language?

– What is computer programming?

Page 2: CS140 Lecture 09b: The Machinery of Computation: Computer

2

3

Accumulator Example

Suppose we want to add 3 numbers:

1) Clear the accumulator (set to all 0s)

2) Load the first input into the adder

3) Compute the sum of accumulator + input

4) Result flows back into accumulator

5) Go to step 2 with next input

Random Access Memory

Memory cells are circuits which

each hold a 1 bit value, grouped

into 8-bit bytes.

Each byte of memory has a

unique address corresponding

to its location within the circuit,

so that it can be located.

Page 3: CS140 Lecture 09b: The Machinery of Computation: Computer

3

5

Memory

Memory Address

A physical location in the computer’s memory.

Addressability

The number of bits stored in each addressable location in memory. (A byte in our example.)

Word Size

The number of bits used in each memory address.

– This dictates how much physical memory can be addressed.

– Example: A 32-bit machine has 232 = 4,294,967,296 possible memory addresses.

6

From Adding Machine… What we’ve got is an machine that can do addition/subtraction in circuitry.

It can read data from memory, and write data back to memory.

We haven’t dealt with how to:

– Specify from which address memory to read.

– Specify which operation to perform (add/subtract).

– Specify to which address to write.

Page 4: CS140 Lecture 09b: The Machinery of Computation: Computer

4

7

… to Automatic Computer

We need a way to tell the machine to:

– Load some data from memory (by address) into the adder.

– Perform Add or Subtract

– Store data from the accumulator into the memory (by address)

What we need is a a way to program it.

8

The earliest digital computers were programmed by wiring them up to perform some specific logic.

Pictured:

Harvard Mark I

Computer Programming, 0.X

Page 5: CS140 Lecture 09b: The Machinery of Computation: Computer

5

9

Computer Programming, 0.X

Later, instructions were programmed by flipping switches.

Pictured:

Digital Equipment PDP-8

Demo:

http://www.youtube.com/watch?v=DPioENtAHuY

10

Stored Program Computer

John von Neumann (a mathematician who worked on the atomic bomb)

– Described a computer architecture in which instructions are read from the memory space (RAM), just like data.

– This design enables programmability, by making it relatively easy to provide new instructions to the computer’s hardware.

Page 6: CS140 Lecture 09b: The Machinery of Computation: Computer

6

11

Stored Program Computer (Von Neumann Architecture)

Figure 5.1 The von Neumann architecture

12

Central Processing Unit

Central Processing Unit (CPU)

Refers to the combination of the Arithmetic/Logic Unit and the Control Unit.

The ALU performs basic arithmetic operations and logic operations. Examples:

– Arithmetic: addition, subtraction, multiplication

– Logical operations: AND, OR, NOT, XOR

Page 7: CS140 Lecture 09b: The Machinery of Computation: Computer

7

13

Central Processing Unit

Central Processing Unit (CPU)

Refers to the combination of the Arithmetic/Logic Unit and the Control Unit.

The control unit coordinates the flow of operations and data in the computer.

– coordinates the ALU and memory

– Keeps track of which instruction to do in a Program Counter (PC)

14

What did we do last time?

– Reviewed the full adder (the circuit which does binary number addition)

– Introduced some other circuits which are part of the CPU: the accumulator, the inverter

– Described the main memory as a location from which to read data into the adder, and to which data can be stored from the accumulator.

– Introduced the von Neumann architecture.

Page 8: CS140 Lecture 09b: The Machinery of Computation: Computer

8

15

An Abstract View of CPU Circuits

Unanswered Questions:

How to specify whether to add or subtract.

How to specify memory (RAM) addresses.

16

Stored Program Computer (Von Neumann Architecture)

Figure 5.1 The von Neumann architecture

Page 9: CS140 Lecture 09b: The Machinery of Computation: Computer

9

17

Control Processing Unit

Central Processing Unit (CPU)

Refers to the combination of the Arithmetic/Logic Unit and the Control Unit.

18

Arithmetic/Logic Unit

The ALU performs basic arithmetic operations and logic operations. Examples:

– Arithmetic: addition, subtraction, multiplication

– Logical operations: AND, OR, NOT, XOR

Most modern ALUs have a small amount of special storage units called registers

– The accumulator is the “main” register

Page 10: CS140 Lecture 09b: The Machinery of Computation: Computer

10

19

Arithmetic/Logic Unit

Recall how addition and subtraction happen (at the level of the CPU):

Addition: – take a value from memory

– send it to the adder

– add to the value in the accumulator

Subtraction: – take a value from memory,

– invert the bits, send to the adder, set carry-in to 1

– add to the value in the accumulator

20

Arithmetic/Logic Unit

The difference between addition and subtraction is slight; it can be controlled by sending a signal to the inverter and to the carry-in bit.

This signal is like flipping a switch to tell the circuits to behave a certain way.

– But instead of flipping the switch, we could just use a binary input to an AND gate…

Page 11: CS140 Lecture 09b: The Machinery of Computation: Computer

11

21

Arithmetic/Logic Unit

Given that: – instructions are carried out by sending data

through circuits

– circuits can be controlled by flipping switches….

A binary code can be used to control which operation the CPU will do at a given time.

22

Control Unit The control unit coordinates the flow of operations and data in the computer.

– coordinates the ALU and memory

Instruction register (IR)

Contains the binary code indicating which instruction is being executed.

Program counter (PC)

Contains the address (in memory) of the next instruction to be executed.

Page 12: CS140 Lecture 09b: The Machinery of Computation: Computer

12

23

Flow of Information

Bus A set of wires that connect all major components of the computer.

Figure 5.2 Data flow through a von Neumann architecture

24

The Computer Clock The computer has oscillator generates an endless rapid stream of off/on pulses (0,1,0,1…).

Each change from 0 to 1 is called a clock cycle.

– cycles per second is one measure of CPU speed

– measured in Hertz (MHz, GHz) – cycles per second

The clock cycle is used to coordinate the tasks of the various circuits.

Page 13: CS140 Lecture 09b: The Machinery of Computation: Computer

13

25

The Fetch-Execute Cycle

Figure 5.3 The Fetch-Execute Cycle

26

Fetch Execute Cycle

The previous diagram shows how it takes 4 steps to execute each instruction.

Is cycles per second a valid measure of CPU performance?

Page 14: CS140 Lecture 09b: The Machinery of Computation: Computer

14

27

Stored Program Computer

Data and instructions to manipulate the data are logically the same (binary code) and can be stored in the same place (RAM).

The machine language of a particular computer is the set of binary coded instructions built into its hardware.

28

Machine Language

Characteristics of machine language:

– Every processor type has its own set of specific machine instructions.

– The relationship between the processor type and the instructions it can carry out is completely integrated.

– Each machine-language instruction does only one very low-level task.

Page 15: CS140 Lecture 09b: The Machinery of Computation: Computer

15

29

Machine Language Example

A Virtual Computer is a hypothetical machine designed to contain the important features of a real computer that we want to illustrate.

Our examples will use the Pep/7 virtual computer designed by Stanley Warford that has 32 machine-language instructions. – http://www.rsu.edu/Faculty/PMacpherson/Programs/pep7.html

– http://www.rsu.edu/faculty/PMacpherson/Programs/tutorial.htm

30

Instruction Format

Figure 7.2 The Pep/7 instruction format

The machine language of a particular computer is the set of binary coded instructions built into its hardware.

This machine language has a 3-byte instruction format.

Page 16: CS140 Lecture 09b: The Machinery of Computation: Computer

16

31

Instruction Format

Operation Code Specifies which instruction is to be carried out. Register Specifier Specifies which register is to be used. Addressing-mode Specifier How to interpret the operand part of the instruction. – 00 for immediate (value given in operand) – 01 for direct (memory address given in operand)

32

Some Sample Instructions

Figure 7.3 Subset of Pep/7 instructions

Page 17: CS140 Lecture 09b: The Machinery of Computation: Computer

17

33

Sample Instructions

What does this instruction mean?

“Load immediate value 7 into the accumulator”

34

Sample Instructions

What does this instruction mean?

“Load value from address 31 into the accumulator”

Page 18: CS140 Lecture 09b: The Machinery of Computation: Computer

18

35

Sample Instructions

What does this instruction mean?

“Store value from accumulator into address 10”

36

Sample Instructions

What does this instruction mean?

“Add value from address 522 into the accumulator”

Page 19: CS140 Lecture 09b: The Machinery of Computation: Computer

19

37

Sample Instructions

What does this instruction mean?

“Read a character of input into address 10”

38

Sample Instructions

What does this instruction mean?

“Send a character from address 10 to output”

Page 20: CS140 Lecture 09b: The Machinery of Computation: Computer

20

39

Computer Programming

The CPU does some low-level tasks, and each one is specified as a machine-language instruction.

– How do we get the CPU to do stuff for us?

Computer programming is the process of analyzing a problem, designing a solution, and expressing that solution as a series of computer instructions.

Computer programs must be written in the language the computer understands.

40

Programming Strategy

Divide and Conquer!

Break up a large problem into smaller units and solve each smaller problem.

– Applies the concept of abstraction.

– The divide-and-conquer approach can be applied over and over again until each subtask is manageable.

Page 21: CS140 Lecture 09b: The Machinery of Computation: Computer

21

41

Machine Language Example

Hello, world! program

The classic “first” computer program is called “Hello World” It prints the words “Hello, world” to the screen.

How to write it in Machine Language for the Pep/7?

– Specify explicit tasks in unambiguous language.

– Divide and conquer: Keep refining these tasks until we arrive at a concrete set of instructions which map to machine language instructions.

42

Writing a Computer Program

Write "Hello, World!"

Write "Hello, World!"

Write "H"

Write "e"

Write "l"

Write "l"

Write "o"

Is this concrete yet?

Is this concrete yet?

Page 22: CS140 Lecture 09b: The Machinery of Computation: Computer

22

43

Writing a Computer Program

Write "H"

Write 0x48

Write "e"

Write 0x65

.

.

.

Write "o"

Write 0x6F

Is this concrete yet?

44

Writing a Computer Program

Page 23: CS140 Lecture 09b: The Machinery of Computation: Computer

23

45

Machine Language

Machine language is the set of binary coded instructions built into the hardware of a particular computer, and used directly by ALU/Control Unit.

– why are the instructions binary encoded?

– how does this encoding relate to the hardware?

46

Review: Machine Language

Operation Code Specifies which instruction is to be carried out. Register Specifier Specifies which register is to be used. Addressing-mode Specifier How to interpret the operand part of the instruction. – 00 for immediate (value given in operand) – 01 for direct (memory address given in operand)

Page 24: CS140 Lecture 09b: The Machinery of Computation: Computer

24

47

What the Computer Can Do

Modern computer processors can do the following types of basic tasks (CPU instructions):

– Add, subtract, multiply, divide, increment, decrement

– Logical AND, OR, XOR, NOT, and NEG operations

– Load data in from RAM, store data out to RAM

– Load data from/ send data to input/output

– Compare register contents for equal to Zero, Less than Zero, Negative

– Jump to another instruction (by address)

Note: by data, we mean one word (e.g. 32 bits)

48

The JUMP Instruction

Recall that the von Neumann computer stores data and instructions in the same RAM.

– The Program Counter holds the address of the next instruction to be executed. Usually, to find the next instruction, the PrC increments the stored address.

The JUMP instruction changes the address stored in the Program Counter programmatically.

– Conditionally: jump if accumulator equals 0

– Conditionally: jump if accumulator is less than 0

– Unconditionally: just jump

Page 25: CS140 Lecture 09b: The Machinery of Computation: Computer

25

49

The JUMP Instruction

The JUMP instruction enables decision making and repetition inside the CPU.

One could argue that JUMP is what makes a computer really programmable.

Examples (with some abstraction): – If a value is less than 0, then jump to instruction 7…

– If the quotient is 0, then end the program.

50

Page 26: CS140 Lecture 09b: The Machinery of Computation: Computer

26

51

Super Simple CPU

– Input: 1 word (16 bits)

– Output 1 word (16 bits)

– Registers: Program Counter

Accumulator

Temp

Instruction Register

– RAM: 16 words (16 bits each)

52

SSCPU Machine Language

Note: the Super Simple CPU has a different instruction format and opcode list than the Pep/7 CPU.

Why? What are the implications of this?

SSCPU Instruction Format: – Each instruction is 16 bits

– First 4 bits specify the operation code (opcode)

– Last 12 bits specify either a RAM address, or an immediate value (a.k.a. a literal number)

Page 27: CS140 Lecture 09b: The Machinery of Computation: Computer

27

53

SSCPU Machine Language

Some example instructions:

– 1111: stop the program

– 0001: add operand to accumulator

– 0010: subtract operand from accumulator

– 0011: load memory cell into accumulator

– 0101: store accumulator into memory cell

– 0110: input value into accumulator

– 0111: output value from accumulator

54

Partial Summary

– Even the modern computer is still a moronic number-crunching, data moving device.

– Algorithms describe the explicit steps the computer must follow to solve a problem.

– Pseudocode is a tool to help us express an algorithm, but it is not understood by the computer.

– Why would anyone want to write a program in machine language?

Page 28: CS140 Lecture 09b: The Machinery of Computation: Computer

28

55

Assembly Language

Assembly language

A language that uses mnemonic codes to represent machine-language instructions

Mnemonic code examples:

– LOADA means “load value into accumulator”

– ADDA means “add value into accumulator”

– STOREA means “store value from accumulator”

56

The Assembly Process

Assembler

A program that reads each of the instructions

in mnemonic form and translates it into the

machine-language equivalent.

Page 29: CS140 Lecture 09b: The Machinery of Computation: Computer

29

57

Pep/7 Assembly Language

Mode specifier: i = immediate, d = direct (memory)

58

Pep/7 Assembly Language

An assembly language can have pseudo operations which do not map directly to the underlying machine language operations.

Page 30: CS140 Lecture 09b: The Machinery of Computation: Computer

30

59

Example: Pseudo Code Program

Reading and adding three numbers

Set sum to 0

Read num1

Add num1 to sum

Read num2

Add num2 to sum

Read num3

Add num3 to sum

Write sum

60

Example: Assembly Program

Page 31: CS140 Lecture 09b: The Machinery of Computation: Computer

31

61

Partial Summary

– Assembly language: much easier to write/read than machine language!

– Why not just use assembly language instead of machine language?

– Who wrote the assembler program, and what language was it written in?

– Why not just write programs in pseudo code, and have a program translate that into machine language instructions?

62

Take-Away Points

– Von Neumann Architecture

– Arithmetic/Logic Unit

– Control Unit

– Machine Language

– Divide and Conquer strategy