Stored Programs A look at how programs are executed CMSC 104 1 The Central Processing Unit The CPU is the computer’s mechanism for performing operations on data and coordinating the sequence of these operations. The CPU consists of two main parts o The Arithmetic Logic Unit - contains circuitry that performs operations o The Control Unit - contains circuitry that coordinates the machine’s activities. CMSC 104 2 Registers CMSC 104 Since the CPU is a separate device from main memory, it needs to have special memory cells to use as a temporary storage place to hold the data it will be working with. These memory cells within the CPU are called registers. 3 Control Unit & ALU jobs The control unit is responsible for placing the data that’s needed in the registers, telling the ALU which registers are being used, and to turn on the correct circuitry in the ALU for the desired operation. The ALU uses the data in the indicated registers to do the operation and places the result in the indicated register. CMSC 104 4 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 5 Machine Instructions : Example 1 Add Two Numbers Stored in Memory 1) Get one value and put it in a register. 2) Get the other value and put it in a register. 3) Activate the addition circuitry with the registers used in steps 1 and 2 as inputs and another register designated to hold the result. 4) Store the result in memory. 5) Stop. CMSC 104 6 Machine Instructions Example 2 Division 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 7 3 Categories of Machine Instructions Data Transfer - Instructions that move data around (#s 1, 3, & 4 in example 1). o Data isn’t really moved, which implies that the original location is left vacant. o Data is actually copied from one place to another. The original is undisturbed. Arithmetic/Logic - Instructions that tell the control unit to request an activity within the ALU (Arithmetic operations or Logical operations) CMSC 104 8 3 Categories of Machine Instructions (continued) Control - 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 9 The Stored Program The idea that a program can be stored in main memory is generally credited to John Von Neumann. In fact, modern computers are said to have Von Neumann architecture. Many programs can be stored in memory. To execute the program we want its first instruction is fetched from memory. CMSC 104 10 The Stored Program Remember that whether the program is written in a high-level language, like C, or in assembly language, it is transformed into machine language before the computer can use it. Machine language is comprised of a series of individual instructions that are coded into 1’s and 0’s. CMSC 104 11 Machine Language Instructions Each machine language instruction is made up of two parts o The opcode - a bit pattern (of 1’s and 0’s) that is a code for an operation, like LOAD, ADD, JUMP or STORE o The operands - bit patterns that provide more information about the instruction. CMSC 104 •Example: If the opcode was for LOAD, the the operands would have to say what memory location was to be read and the register to place the value in. 12 Program Execution A computer follows a program stored in its memory by getting the instructions from memory and putting a copy of them in the control unit as they are needed. There are two special purpose registers used for program execution o Instruction register o Program counter CMSC 104 13 Instruction Register & Program Counter The Instruction Register (IR) contains the instruction that is currently being executed. The Program Counter (PC) contains the address of the next instruction in memory. CMSC 104 14 Program Execution The control unit performs its job by continually repeating what is called the machine cycle. The machine cycle consists of 3 steps CMSC 104 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. 15 Program vs Data CMSC 104 Many programs can be stored in the computer’s memory We can start a program running by putting its starting address in the program counter. Both data and programs are stored in memory, and remembering that they are both just a collection of 1’s and 0’s, if the address of data is put into the program counter, the machine will attempt to execute data. The likelihood is that it won’t get very far. 16 Other ALU Operations Arithmetic Instructions (already mentioned) Logic Instructions o AND, OR and EXCLUSIVE OR are the logic instructions. o They all take two operands and produce one result. CMSC 104 17 Logical Operations For all logical operations, we consider a 1 to be true and a 0 to be false. 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 CMSC 104 the results of its neighboring columns. 18 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. The upper left corner of the table is divided in two and should give the names of the variables being used. CMSC 104 19 Truth Table for AND A CMSC 104 B 0 1 0 0 0 1 0 1 20 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. So A AND B is true if and only if A is true and B is also true. CMSC 104 21 Truth Table for OR A CMSC 104 B 0 1 0 0 1 1 1 1 22 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. 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. CMSC 104 23