### Introduction to the computer: how a computer executes instructions

• Overview

• Topics discussed in this webnote:

 Structure and operation of the CPU Function of each component in the CPU Operation of the CPU (instruction execution cycle) Program flow The pointer concept Program counter is a pointer Default program flow is sequential Branch instruction can change the default program flow Types of instructions that a computer can execute Arithmetic and logic operations Data transfer operations between CPU and RAM memory Branch instructions that change program flow (branch instructions can be conditional)

• Structure of a CPU                 (best use board to teach this)

• Structure of a CPU:

Components in a CPU:

• General purpose registers:

 Register is a synonym for memory in computer science A general purpose register is a memory cell Each general purpose register has a unique name It is used to store (and recall) intermediate result of complex computations

• Arithmetic and Logic Unit (ALU):

 Is a complex electrical circuit that can perform Mathematical (+, -, ×, /) and logical operations (<, ≤ >, ≥, and, or) The output (result) of the computation (obtained by the ALU) is often stored in a general purpose register

• Instruction register (IR):

 Contains the current instruction being executed by the CPU The CPU will perform the operation indicated by the instruction code contained in the instruction register (E.g.: code 0 = "add", code 1 = "subtract", and so on)

• Program counter (PC):

 The program counter is a register (memory cell) ! This register contains the address (location in memory) of the next instruction after the CPU finishes executing the current instruction in the instruction register The value in the program counter will be increased after the CPU finishes executing one instruction

• Processor status register (PSR):

• This register contains the various information about the CPU
• Among the information contained in the PSR is:

 The result of a comparison operation

• When the CPU compares 2 numbers a and b, the outcome of the comparison is stored in the PSR

The outcome of a compare operation will allow the CPU to determine the following fact between a and b:

 equal not equal less than less than or equal greater than greater than or equal

• Operation of a CPU                 (best use board to teach this)

• The CPU performs the following sequence of operations repeatedly:

 After executing an instruction, the CPU obtains ("fetches") the (next) instruction at the address (location) given in the program counter The fetched instruction is stored in the instruction register in the CPU and the program counter is increased to point to the next instruction in the memory The fetched instruction (stored in the instruction register) is the executed (the CPU will do the operation indicated by the instruction code in the instruction register)

• Example:

• Supposed the CPU has just finished executing the instruction (instruction code 0) in the instruction register:

(Instruction code 0 in the instruction register means "add" and it has just been executed)

• The following figures illustrats the CPU's execution cycle (that goes on indefinitely):

1. The CPU first sends a request to retrieve (recall) the data stored at memory location given by the program counter (PC) (in the figure, the value of the PC = 1):

The CPU will cause the memory to retrieve (recall) the value stored at memory address (location) 1

2. In response, the RAM memory sends the value stored in memory location 1 (= the number 4) to the CPU which stores it in the instruction register (IR):

This number (4) will be interpretated as a code for a computer instruction.

The old instruction code (0) will be over-written by the new instruction code

3. The program counter is then increased:

Notice that the CPU is now ready to fetch the next instruction when the current instruction (stored in the instruction register) is processed.

4. The CPU will now execute the instruction in the Instruction Register (instruction code = 4)

When the CPU is finished, it will repeat these steps again (and again, until you turn the cmputer off)...

• Pointers: pointing to a location in memory

• Pointer:

• A pointer is "something" that points to somewhere

Examples:

3 pointers to different directions

• In Computer Science, a pointer always points to a location in memory

Examples:

Pointer to memory location 1 Pointer to memory location 4

• The computer does not have "pointing finger" that point to some location in memory

The pointer is represented in the computer by the value of the address (location) of the memory where the pointer is pointing at

Examples:

Pointer to memory location 1 Pointer to memory location 4
• Storing the value 1 in some register/memory cell will remember the memory location 1

• The value 1 in the register/memory cell is effectively pointing to the memory location 1
• Storing the value 4 in some register/memory cell will remember the memory location 4

• The value 4 in the register/memory cell is effectively pointing to the memory location 4

• Example of a pointer: the program counter (PC)

• The program counter (PC) is in fact a pointer

Example:

• When the program counter (PC) contains the value 1:

the program counter (PC) is in fact pointing to the memory location 1:

• Program flow

• Definition: program flow

 Program flow = the sequence of instructions from the program executed by the CPU

• Default program flow:

 After executing a non-branching (or non-jumping) instruction at memory location n, then next instruction that is executed is the instruction at memory location n+1

Example:

 All of the instructions in the figure are non-branching Therefore, the sequence of instructions executed by the CPU starting at memory location 0 is: 0 ("add") 4 ("compare") 1 ("subtract") And so on (in sequence)

• Program flow in program used by the mechanical piano

• Consider the mechanical piano:

• The program used by the mechanical piano is:

 stored as spikes on the rotating cylinder (in the center of the mechanical piano)

The cylinder can only rotate forward.

Therefore:

• There is only one kind of program flow possible in the mechanical piano:

 The default program flow

• The program flow in a mechanical piano is restricted by the physics

(The spikes on the cylinder are always accessed in the "rotational order")

• This physical restriction does not exist for the computer

• The computer can change (update) the program pointer to an arbitrary value

Changing the program counter (it contains the location of the next instruction) will change the program flow

• Therefore:

 The computer can change the default program flow !!!

• Branching: changing the program flow

• Branch instruction:

 A branch instruction can alter (change) the default program flow When the CPU executes a branch x instruction, the next instruction that will be executed by the CPU is the instruction at memory location x

Example:

 Notice there is a branch to location 4 instruction in the example. Therefore, the sequence of instructions executed by the CPU starting at memory location 0 is: 0 ("add") 64 ("branch to location 4") 56 ("negate" --- this is the instruction at memory location 4) 0 ("add") And so on (in sequence again)

• The types of instructions that a computer can execute

• The computer is a computing (reckoning) machine

• All existing computers (actually the CPU) execute the following 3 types of instructions:

• Arithmetic and logic operations

 + − × / AND            OR NOT

The result of an arithmetic and logic operation is often stored in a general purpose register

• Memory transfer operations

 Transfer the content from some specific memory location to a specific register (memory cell) in the CPU. and vice versa (transfer the content from some specific register (memory cell) in the CPU to some specific memory location).

• Branch operations

• A branch instruction will cause the CPU to branch (jump) to the specified location in memory

• After the jump has occured, the CPU will continue to execute the instructions in sequence, until another branch/jump instruction is encountered

• There are 2 kinds of branch operations:

 A unconditional branch instruction will always cause the CPU to jump to the target location A conditional branch instruction will only cause the CPU to jump to the target location when the specified condition is met