Stored Programs A look at how programs are

  • Slides: 35
Download presentation
Stored Programs A look at how programs are executed CMSC 104 1

Stored Programs A look at how programs are executed CMSC 104 1

A Cast of Characters CPU: Central Processing Unit l Main Memory: l Secondary Storage:

A Cast of Characters CPU: Central Processing Unit l Main Memory: l Secondary Storage: Disk l Stored Program: l CMSC 104 2

The Central Processing Unit l CMSC 104 The CPU is the computer’s mechanism for

The Central Processing Unit l CMSC 104 The CPU is the computer’s mechanism for performing operations on data and coordinating the sequence of these operations. 3

Inside the CPU l The CPU consists of two main parts o The Arithmetic

Inside the CPU l The CPU consists of two main parts o The Arithmetic Logic Unit - contains circuitry that performs operations �Add, subtract, multiply, divide, compare … o The Control Unit - contains circuitry that coordinates the machine’s activities. �Read memory, write memory, fetch instruction CMSC 104 4

Registers Temporary memory inside the CPU l Used to hold partial results and CPU

Registers Temporary memory inside the CPU l Used to hold partial results and CPU control data. l Often specialized --- special registers for special functions l Very Fast --- “Register variables” l CMSC 104 5

Control Unit Keeps track of location of current instruction l Fetchs instructions from memory

Control Unit Keeps track of location of current instruction l Fetchs instructions from memory l Decodes instruction l Loads registers with data l Keeps track of free registers l Instructs ALU which operations are required. l CMSC 104 6

ALU jobs l Executes arithmetic and logic instructions from Control Unit. o “Add the

ALU jobs l Executes arithmetic and logic instructions from Control Unit. o “Add the contents of register A to register B and store the result in register B. ” o “Compare the contents of register A to the contents of the program counter. ” o “Add the contents of the program counter to the memory cell whose address is in register A. ” CMSC 104 7

CPU - Main Memory Diagram Registers Arithmetic Logic Unit Control Unit Central Processing Unit

CPU - Main Memory Diagram Registers Arithmetic Logic Unit Control Unit Central Processing Unit Bus Main Memory The number of registers varies from machine to machine The number is a multiple of 2, typically 16 or 32. CMSC 104 8

Machine Instructions : Example 1 Add Two Numbers Stored in Memory Situation Addend is

Machine Instructions : Example 1 Add Two Numbers Stored in Memory Situation Addend is in Memory Cell M 1 Augend is in Memory Cell M 2 Sum is to be stored in Memory Cell M 3 Registers R 1, R 2, and R 3 are free Arithmetic operations must be done “register to register” CMSC 104 9

Machine Instructions : Example 1 Add Two Numbers Stored in Memory 1) [M 1]

Machine Instructions : Example 1 Add Two Numbers Stored in Memory 1) [M 1] R 1 2) [M 2] R 2 3) Activate the addition circuitry with [R 1] and [R 2] and store the result in R 3 4) Store the result in memory. 5) Stop. CMSC 104 10

Machine Instructions: Example 2 1) LOAD a register with a value in memory 2)

Machine Instructions: Example 2 1) LOAD a register with a value in memory 2) LOAD another register with another value in memory 3) If this second value is zero, jump to step 6. 4) Divide the contents of the first register by the contents of the second register and put the result in the accumulator 5) STORE the contents of the accumulator in memory 6) Stop. CMSC 104 11

But Things Aren’t This Simple… CPUs Are Different l Different Registers o Accumulators: Must

But Things Aren’t This Simple… CPUs Are Different l Different Registers o Accumulators: Must hold addend and result o Indirect Registers: Use this a pointer to data. o Alternative register sets: Switch registers l Commands o Add Immediate: Add next byte(s) in instruction stream o Add Indirect: Add contents of address in stream CMSC 104 12

Type of Machine Instructions Data Transfer Instructions l Data Transfer: Instructions for copying data

Type of Machine Instructions Data Transfer Instructions l Data Transfer: Instructions for copying data (#s 1, 3, & 4 in example 1). o Copy the contents of one location on top of the contents of another. o The original location is undisturbed. The original data in the second location is destroyed. CMSC 104 13

Type of Machine Instructions Arithmetic/Logic Instructions l Arithmetic/Logic - Instructions that tell the control

Type of Machine Instructions Arithmetic/Logic Instructions l Arithmetic/Logic - Instructions that tell the control unit to request an activity within the ALU (Arithmetic operations or Logical operations) o Add [R 1] to [R 2] o Is [R 1] greater than [R 2]? o Is [R 1] equal to [R 2]? CMSC 104 14

Machine Instructions Control Instructions l Control Instructions: Instructions that direct the execution of a

Machine Instructions Control Instructions l Control Instructions: Instructions that direct the execution of a program rather than manipulate data. o Conditional Branch (Step 3 in example 2) branch to a different step if a certain condition is true. o Unconditional branches or jumps Example : Jump to step 25. CMSC 104 15

The Stored Program l Program stored in computer memory under computer control o Not

The Stored Program l Program stored in computer memory under computer control o Not “hardwired” or in “external memory” Idea credited to John Von Neumann (hence “Von Neumann architecture”) l Many programs can be stored in memory. l Execution begins by fetching the first instruction of the desired program. l CMSC 104 16

The Stored Program Logical Program Placement Start Program 1 Start Program 2 Start Program

The Stored Program Logical Program Placement Start Program 1 Start Program 2 Start Program 3 Start Program 4 CMSC 104 17

The Stored Program l Stored in “Machine Language” --patterns of 1’s and 0’s that

The Stored Program l Stored in “Machine Language” --patterns of 1’s and 0’s that encode specific CPU instructions. o “bit-patterns” trigger cpu switches l CMSC 104 “Human-readable” programs in C, Lisp, Java, or Assembly Language must be translated (“compiled”) into machine code for execution. 18

Machine Language Instructions l Each instruction has a known format o Instructions are unambiguous

Machine Language Instructions l Each instruction has a known format o Instructions are unambiguous l Many instruction have two parts o opcode --- specifies an operation, like LOAD, ADD, JUMP or STORE o operands --- provide more information about the instruction. Opcode Operand 1 Operand 2 “Load” CMSC 104 <Source. Memory Cell> <Target Register> 19

Program Execution l Instructions are read from memory and copied into the CPU one-at-a-time

Program Execution l Instructions are read from memory and copied into the CPU one-at-a-time Memory CPU CMSC 104 20

Program Execution l There are two special purpose registers used for program execution o

Program Execution l There are two special purpose registers used for program execution o Instruction register --- Contains the instruction currently being executed o Program counter --- Contains the memory address of the next instruction to be executed. CMSC 104 21

Program Execution Memory 7 Program Counter CMSC 104 Instruction Register 22

Program Execution Memory 7 Program Counter CMSC 104 Instruction Register 22

Program Execution l Control unit continuously repeats 3 -step cycle: o Fetch - gets

Program Execution l Control unit continuously repeats 3 -step cycle: o Fetch - gets the next instruction from memory o Decode - decodes the bit pattern in the instruction register o Execute - performs the action requested by the instruction in the instruction register. CMSC 104 23

Program vs Data Sven Wingquist. Self-Aligning Ball Bearing. 1929. Steel, 1 3/4" (4. 5

Program vs Data Sven Wingquist. Self-Aligning Ball Bearing. 1929. Steel, 1 3/4" (4. 5 cm) high, 8 1/2" (21. 5 cm) diameter. The Museum of Modern Art, New York. Gift of SKF Industries. Photograph © 1997 The Museum of Modern Art, New York. CMSC 104 24

Program -vs- Data Alvar Aalto. Vase. 1937. Amber cast glass, 5 5/8" (14. 3)

Program -vs- Data Alvar Aalto. Vase. 1937. Amber cast glass, 5 5/8" (14. 3) high. The Museum of Modern Art, New York. Gift of Artek-Pascoe. Photograph © 1997 The Museum of Modern Art, New York. CMSC 104 25

Program vs Data l l l CMSC 104 There is no difference between “programs”

Program vs Data l l l CMSC 104 There is no difference between “programs” and “data” “Machine-language Programs” are just “data” for the CPU. The CPU will attempt to execute ANY data in memory --- if it works, then the data must have been a “program” 26

Other ALU Operations Arithmetic Instructions (already mentioned) l Logic Instructions l o AND, OR

Other ALU Operations Arithmetic Instructions (already mentioned) l Logic Instructions l o AND, OR and EXCLUSIVE OR are the logic instructions. o They all take two operands and produce one result. CMSC 104 27

Logical Operations l l l CMSC 104 For all logical operations, “ 0” is

Logical Operations l l l CMSC 104 For all logical operations, “ 0” is considered “false”; “ 1” is “true” Truth tables can be constructed for each of the logical operations and the contents of these truth tables can be considered the definitions of the operations. With logical operations, unlike addition, each column’s result is independent of the results of its neighboring columns. 28

Truth Tables A truth table is a table of values for an operation. To

Truth Tables A truth table is a table of values for an operation. To construct a truth table for an operation, put all possible values of one variable across the top of the table and all possible values of a second variable down the left side of the table. l The upper left corner of the table is divided in two and should give the names of the variables being used. l CMSC 104 29

Truth Table for AND A CMSC 104 B 0 1 0 0 0 1

Truth Table for AND A CMSC 104 B 0 1 0 0 0 1 30

AND We can see from the truth table for AND that the only way

AND We can see from the truth table for AND that the only way we can get a result of true is if both operands are true. l So A AND B is true if and only if A is true and B is also true. l CMSC 104 31

Truth Table for OR A CMSC 104 B 0 1 0 0 1 1

Truth Table for OR A CMSC 104 B 0 1 0 0 1 1 32

OR We can see from the truth table for OR that the only way

OR We can see from the truth table for OR that the only way we can get a result of false is if both operands are false. l So A OR B is true, if A is true and B is also true, or if only A is true, or if only B is true. So either A or B needs to be true or both can be true. l CMSC 104 33

Truth Table for Exclusive OR A CMSC 104 B 0 1 0 0 1

Truth Table for Exclusive OR A CMSC 104 B 0 1 0 0 1 1 1 0 34

Exclusive OR “The Ice Cream or Cheesecake rule. ” a. k. a. XOR l

Exclusive OR “The Ice Cream or Cheesecake rule. ” a. k. a. XOR l “One or the other, but not both. ” l “A xor B” “A or B but not (A and B)” l o. A=B !(A xor B) is false o A != B A xor B Notation: “!<expression>” = “not <expression>” = “<expression is false>” CMSC 104 35