Title: Introduction to computer: executes instructions
1Introduction to computer executes instructions
2Overview
- Topics discussed in this webnote
- Structure and operation of the CPU
- Program flow
- Types of instructions that a computer can execute
3Overview
- Topics discussed in this webnote
- Structure and operation of the CPU
- Program flow
- Types of instructions that a computer can execute
4Structure of a CPU
5Components of a CPU
- General purpose registers
- Arithmetic and Logic Unit (ALU)
- 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
- Is a complex electrical circuit that can perform
Mathematical (, -, , /) and logical operations
(lt, gt, , and, or) - The output (result) of the computation (obtained
by the ALU) is often stored in a general purpose
register
6Structure of a CPU (cont.)
- Instruction register (IR)
- Program counter (PC)
-
- 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
- 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
7Structure of a CPU (cont.)
- Processor status register (PSR)
- This register contains the various information
about the CPU - Among the information contained in the PSR is
- 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
- The result of a comparison operation
- equal
- not equal
- less than
- less than or equal
- greater than
- greater than or equal
8Operation of a CPU
- 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)
9Operation of a CPU (cont.)
- 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)
10Operation of a CPU (cont.)
- The following figures illustrates the CPU's
execution cycle (that goes on indefinitely) - The CPU will cause the memory to retrieve
(recall) the value stored at memory address
(location) 1
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)
11Operation of a CPU (cont.)
- 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 interpreted as a code
for a computer instruction. - The old instruction code (0) will be
over-written by the new instruction code
- 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)
12Operation of a CPU (cont.)
- 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.
13Operation of a CPU (cont.)
- 4. The CPU will now execute the instruction in
the program counter (instruction code 4) - When the CPU is finished, it will repeat these
steps again (and again, until you turn the
computer off)...
14Pointers pointing to a location in memory
- A pointer is "something" that points to
somewhere Examples
3 pointers to different directions
15Pointers pointing to a location in memory (cont.)
- In Computer Science, a pointer always points to a
location in memory - Examples
Pointer to memory location 1
Pointer to memory location 4
16Pointers pointing to a location in memory (cont.)
- 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
17Pointers pointing to a location in memory (cont.)
Pointer to memory location 1
Pointer to memory location 4
- 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
- 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
18Example 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
19Overview
- Logical (functional) view of a computer
- Program flow
- Types of instructions that a computer can
execute
20Program flow
- Definition program flow
- Default program flow
- Program flow the sequence of instructions from
the program executed by the CPU
- 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 n1
21Program flow (cont.)
- 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 - 1. 0 ("add")
- 2. 4 ("compare")
- 3. 1 ("subtract")
- 4. And so on (in sequence)
22Branching changing the program flow
- 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
23Branching changing the program flow (cont.)
- 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 - 1. 0 ("add")
- 2. 64 ("branch to location 4")
- 3. 56 ("negate" --- this is the instruction at
memory location 4) - 4. 0 ("add")
- 5. And so on (in sequence again)
24Overview
- Logical (functional) view of a computer
- Program flow
- Types of instructions that a computer can
execute
25The 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 The result
of an arithmetic and logic operation is often
stored in a general purpose register
26The types of instructions that a computer can
execute (cont.)
- 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).
27The types of instructions that a computer can
execute (cont.)
- A branch instruction will cause the CPU to
branch (jump) to the specified location in memory
- After the jump has occurred, 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