The
CPU's
function is to execute program instructions stored in memory. How does
the CPU execute a high-level language program? Consider the following C++
statement:
Y = S + T - R
The Hypothetical Machine or other computer cannot execute a program written in symbolic form such as C++ directly. Each C++ symbolic statement must be translated to the corresponding machine instructions that can be executed by that CPU, in this case for the Hypothetical Machine.
A C++ compiler can translate the Y = S + T - R into the Hypothetical Machine program code that follows. For the Hypothetical Machine, four machine instructions are translated from the single C++ statement. This is due to C++ having four operands (variables) Y, S, R, and T in one statement but each Hypothetical Machine instruction can have only one operand in memory.
How can the Hypothetical Machine do arithmetic operations with only one operand
instructions when most operations require three operations, for example
X=Y+Z. The Hypothetical Machine has an accumulator named A that,
as in a calculator, is always one of the operands. The following illustrates
how Y = S + T - R is implemented in Hypothetical Machine language.
| Operation | Hypothetical
Machine Mnemonic |
Comments |
| A = S | LDA S | Load A register with value of S |
| A = A + T | ADD T | Add to A register value of T |
| A = A - R | SUB R | Subtract from A register value of R |
| Y = A | STA Y | Store the A register value to Y |
The CPU executes machine instructions that are stored in memory. How does the CPU of the machine work? Suppose we had written the program instructions to perform Z = Y + 921. The machine instructions of the algorithm and data (variables Z and Y, and constant 921) would be stored in memory.
How would the program, algorithm and data, appear in the Hypothetical Machine memory?
| 01005
20004 02006 30007 00921 04800 00807 99000 |
PROGRAM Z=Y+921 - To better see how the program is represented in memory we need to know addresses of program instructions (ALGORITHM)
and variables (DATA) as below. The ALGORITHM is stored in memory addresses
000-003, and 007. The DATA (variables) are stored in memory addresses 004-006.
Address |
Instruction |
Mnemonic |
Comments |
000 001 002 003 004 005 006 007 |
OP ADDR
01 005
20 004
02 006
30 007
00921
04800
00807
99 000
|
LDA Y
ADD 921
STA Z
Jmp 007
921 00921
Y 04800
Z 00807
HLT
|
A = Y A = A + 921 Z = A GO TO Address 7 Constant 921 Variable Y Variable Z Halt Execution |
Fetch/Execute ALGORITHM:
How does the CPU execute a program? Simply stated, the CPU executes a program by repeatedly FETCH'ING one instruction from memory and EXECUTE'ING that instruction in an endless cycle called the FETCH/EXECUTE cycle. To execute a program, the Hypothetical Machine CPU implements this cycle in five steps as below.
| General Fetch/Execute | Hypothetical Machine Fetch/Execute |
| 0. Initialize CPU | PC = 0 |
| 1. Fetch instruction at PC | MAR = PC MDR = Memory[ MAR ] IR = MDR |
| 2. PC = next instruction address | PC = PC + 1 |
| 3. Decode instruction | OP = IR operation field Addr = IR address field |
| 4. Execute operation OP | |
| 5. Go to 1 |
CPU Architecture
PC 3 digits, holds address in range 000 to 999 of next instruction. IR 5 digits, holds instruction (2-digit operation and 3-digit operand). A, B 5 digits, holds numerical value in range -99999 to 99999. MAR 3 digits, holds address in range 000 to 999 of Memory index. MDR 5 digits, holds numerical value accessed to/from Memory[ MAR ]. Memory 5 digits, array indexed from 000 to 999, holds values -99999 to 99999 |
Fetch/Execute of Z = Y + 921 - A more detailed view of the Fetch/Execute of our above program:
0. Initialize CPU PC = 0 |
| 1. Fetch
instruction at
PC
Memory ADDRESS 000 = 01005 2. PC = PC + 1 PC = 1 3. Decode OP=01 ADDR=005 4. Execute OP A = Y = 4800 5. GO TO 1. |
1. Fetch instruction at PC Memory ADDRESS 001 = 20004 2. PC = PC + 1 PC = 2 3. Decode OP=20 ADDR=004 4. Execute OP A = A + 921 = 5721 5. GO TO 1. |
1. Fetch instruction at PC Memory ADDRESS 002 = 02006 2. PC = PC + 1 PC = 3 3. Decode OP=02 ADDR=006 4. Execute OP Z = A = 5721 5. GO TO 1. |
1. Fetch instruction at PC Memory ADDRESS 003 = 30007 2. PC = PC + 1 PC = 4 3. Decode OP=30 ADDR=007 4. Execute OP PC = 007 5. GO TO 1. |
1. Fetch instruction at PC Memory ADDRESS 007 = 99000 2. PC = PC + 1 PC = 8 3. Decode OP=99 ADDR=000 4. Execute OP HLT |
Instruction Cycles - Clock rate is often used by computer manufacturers as a raw measure of performance. The Hypothetical Machine operation is timed by a central clock. We have seen that the Fetch/Execute cycle consists of a series of several steps where each step takes one clock tick so the faster the clock ticks, the faster the machine executes. The LDA Y instruction of the Hypothetical Machine requires 7 total steps, 4 steps to fetch and 3 steps to execute. With a 1 Hz. clock (one tick per second), the LDA Y instruction would require 7 seconds. With a 1000 Hz. clock (1000 ticks per second), only 7/1000 seconds. With a 400 MHz. clock (400 million ticks per second), only 7/400,000,000 seconds. Note that not all instructions take the same number of steps to execute and modern CPUs can perform multiple steps simultaneously.
A more detailed (and accurate) examination of instruction execution is given below showing each data movement within the CPU registers. The Hypothetical Machine Simulator used in Homework 1 can be used to trace the Fetch/Execute cycle.
Example: LDA Y 01005 1. Fetch 1. MAR <- PC = 000 2. MDR <- (MAR) = (000) = 01005 3. IR <- MDR = OP = 01 ADDR = 005 2. 4. PC <- PC + 1 = 004 3. Execute 5. MAR <- ADDR = 005 6. MDR <- (MAR) = 04800 7. A <- MDR = 04800 |
Example: JMP 007 30007 1. Fetch 1. MAR <- PC = 003 2. MDR <- (MAR) = (003) = 30007 3. IR <- MDR = OP = 30 ADDR = 007 2. 4. PC <- PC + 1 = 004 3. Execute 5. PC <- ADDR = 007 |