29
1 1 John Magee 30 July 2013 Some material copyright Jones and Bartlett CS101 Lecture 26: Low Level Programming 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?

Overview/Questions€¦ · 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?

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Overview/Questions€¦ · 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?

1

1

John Magee 30 July 2013

Some material copyright Jones and Bartlett

CS101 Lecture 26:

Low Level Programming

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: Overview/Questions€¦ · 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?

2

3

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.

4

An Abstract View of CPU Circuits

Unanswered Questions:

How to specify whether to add or subtract.

How to specify memory (RAM) addresses.

Page 3: Overview/Questions€¦ · 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?

3

5

Stored Program Computer (Von Neumann Architecture)

Figure 5.1 The von Neumann architecture

6

Control Processing Unit

Central Processing Unit (CPU)

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

Page 4: Overview/Questions€¦ · 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?

4

7

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

8

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

Page 5: Overview/Questions€¦ · 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?

5

9

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…

10

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.

Page 6: Overview/Questions€¦ · 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?

6

11

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.

12

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

Page 7: Overview/Questions€¦ · 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?

7

13

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.

14

The Fetch-Execute Cycle

Figure 5.3 The Fetch-Execute Cycle

Page 8: Overview/Questions€¦ · 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?

8

15

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?

16

Stored Program Computer (Von Neumann Architecture)

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.

Page 9: Overview/Questions€¦ · 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?

9

17

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.

18

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.

– Note: different than the Super Simple CPU.

– http://www.rsu.edu/Faculty/PMacpherson/Programs/pep7.html

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

Page 10: Overview/Questions€¦ · 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?

10

19

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.

20

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)

Page 11: Overview/Questions€¦ · 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?

11

21

Some Sample Instructions

Figure 7.3 Subset of Pep/7 instructions

22

Sample Instructions

What does this instruction mean?

“Load immediate value 7 into the accumulator”

Page 12: Overview/Questions€¦ · 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?

12

23

Sample Instructions

What does this instruction mean?

“Load value from address 31 into the accumulator”

24

Sample Instructions

What does this instruction mean?

“Store value from accumulator into address 10”

Page 13: Overview/Questions€¦ · 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?

13

25

Sample Instructions

What does this instruction mean?

“Add value from address 522 into the accumulator”

26

Sample Instructions

What does this instruction mean?

“Read a character of input into address 10”

Page 14: Overview/Questions€¦ · 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?

14

27

Sample Instructions

What does this instruction mean?

“Send a character from address 10 to output”

28

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.

Page 15: Overview/Questions€¦ · 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?

15

29

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.

30

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.

Page 16: Overview/Questions€¦ · 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?

16

31

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?

32

Writing a Computer Program

Write "H"

Write 0x48

Write "e"

Write 0x65

.

.

.

Write "o"

Write 0x6F

Is this concrete yet?

Page 17: Overview/Questions€¦ · 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?

17

33

Writing a Computer Program

34

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?

Page 18: Overview/Questions€¦ · 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?

18

35

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)

36

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)

Page 19: Overview/Questions€¦ · 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?

19

37

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

38

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.

Page 20: Overview/Questions€¦ · 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?

20

39

Lab 09

Learning Objective

To deepen your understanding of the central processor unit, the fetch-execute cycle, and the stored program concept.

In addition, you will use pseudo code to express an algorithm, and translate the algorithm into machine-language instructions which the CPU can execute.

40

Page 21: Overview/Questions€¦ · 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?

21

41

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)

http://www.cs.bu.edu/courses/cs101/labs/ECS_2e/Applets/APPLETS/CPU/applet_frame.htm

42

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 22: Overview/Questions€¦ · 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?

22

43

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

44

Lab Exercise

You will use a simple, simulated CPU to: – observe the CPU’s fetch-execute cycle in action

– become acquainted with machine language programming and instructions

– experiment with programming in a machine language

You will write a pseudo code algorithm, and a machine language program to do addition of 2 numeric inputs.

Page 23: Overview/Questions€¦ · 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?

23

45

Pseudo code

Pseudo code is a way of expressing algorithms that uses a mixture of English phrases and indention to make the steps in the solution explicit.

– There are no grammar rules in pseudocode.

– Pseudocode is not case sensitive.

Analogy: pseudo code is like an outline for writing a paper -- it contains all of the main ideas, but is not tied to the structure of grammar, etc.

46

Algorithms

Algorithm

A set of unambiguous instructions for solving a problem or subproblem in a finite amount of time, and using a finite amount of data.

For a given set of inputs, the algorithm will always produce the same outputs.

Page 24: Overview/Questions€¦ · 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?

24

47

Following an Algorithm

Grandpa Ron’s Chocolate Kahlua Mudslides Ingredient List: ½ cup vodka ½ cup Kahlua ½ cup Bailey’s Irish Cream ¼ cup Hershey’s chocolate syrup 1 blender full of ice

Process:

Fill blender with ice.

Pours other ingredients onto ice. Blend until smooth consistency. Pour into glasses and enjoy.

48

Following an Algorithm

Algorithm for preparing Chocolate Mudslides:

If not all ingredients are present

Go to store and buy missing ingredients

Fill blender with ice

Pour other ingredients into the blender

While (not smooth)

Turn on blender

Wait 10 seconds

Turn off blender

Test consistency with wooden spoon

Pour into glasses and serve

Page 25: Overview/Questions€¦ · 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?

25

49

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?

50

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”

Page 26: Overview/Questions€¦ · 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?

26

51

The Assembly Process

Assembler

A program that reads each of the instructions

in mnemonic form and translates it into the

machine-language equivalent.

52

Pep/7 Assembly Language

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

Page 27: Overview/Questions€¦ · 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?

27

53

Pep/7 Assembly Language

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

54

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

Page 28: Overview/Questions€¦ · 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?

28

55

Example: Assembly Program

56

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?

Page 29: Overview/Questions€¦ · 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?

29

57

Take-Away Points

– Von Neumann Architecture

– Arithmetic/Logic Unit

– Control Unit

– Machine Language

– Divide and Conquer strategy

58

Student To Dos

– Readings:

Reed ch 7, pp 128-134

Reed ch 6

– Homework on course schedule.

– Wednesday: Meet in CS teaching lab.