The CPU is the working part of the computer.
It runs your programs, makes changes to the contents of memory, and sends data to peripheral devices.
Thus, it causes the computer to produce the results you want.
The Simple CPU demonstrates how a computer works: what some very simple computer instructions look like and how they are combined to perform a calculation.
What The Simple CPU Does
The Simple CPU demonstration contains the CPU and a memory. These parts reside in an Excel spreadsheet so we can see them work.
For this demo, we will pay most attention to the contents of memory and to two registers in the CPU: the accumulator and the program counter.
The accumulator holds a single number that the CPU can add other numbers to.
The program counter holds the address of the computer program instruction that the CPU is executing.
When a program is running, the CPU uses the program counter to pick an instruction from memory and it executes that instruction. After completing that instruction, the CPU usually adds one to the program counter, which leads it to pick up the instruction following the previous one.
Downloading the Simple CPU
Click Here to download the Excel spreadsheet containing the Simple CPU.
The Simple CPU is embedded in an Excel “template” file. If you save it you will save a new, separate copy.
The Simple CPU file contains Excel Macros. If Excel asks you if macros should be enabled, say Yes.
Running the Simple CPU
To run the Simple CPU, do the following:
- Type your program into Column A. Storage locations are indicated by row numbers.
- Type Control-R (hold down the control key while typing the R key) to reset the accumulator and program counter.
- Type Control-W to step the Simple CPU through the program. You must type Control-W twice for each instruction. This is because the Simple CPU requires two separate memory cycles to execute each instruction. Real computers operate in a similar manner.
If you are interested in a more sophisticated CPU, see the Spreadsheet CPU.
Computer Program Instructions
The CPU can only execute instructions that it knows about. The Simple CPU only knows six instructions. They fall into two categories:
- Memory instructions – these instructions include the address of a memory location.
- Operating instructions – these instructions tell the CPU to do something that doesn’t involve memory.
Here are the memory instructions:
- Add – the CPU adds the contents of the indicated memory location to the contents of the accumulator
- Store – the CPU stores the contents of the accumulator into the indicated memory location
- Jump – the CPU updates the program counter so that the next instruction comes from the indicated memory location
Here are the operating instructions:
- Stop – the CPU stops running its program
- Invert – the CPU inverts the numeric sign of the value stored in the accumulator: negative numbers turn positive and vice versa.
- Clear – the CPU clears the accumulator so that it contains the value zero.
- Noop – the CPU does nothing and proceeds with the next instruction.
A Simple Program
For as long as this program runs, it doubles the number in storage location 5:
- Clear
- Add 5
- Store 5
- Jump 2
- 1
The program works like this:
- The first instruction clears the accumulator
- The second instruction adds the contents of location 5. Note that it contains “1”. The result of 1+0 is, of course, 1.
- The third instruction saves this result back into location 5. This doesn’t do anything significant at the moment, since location 5 already contains 1, but the instruction is important the next time around.
- The fourth instruction directs the CPU to continue the program with the instruction in location 2.
- When we do the Add the second time, it adds 1+1, yielding 2.
- When we do the Store the second time, we save the value 2 in location 6.
- Again, we jump back to location 2 and perform the Add again.
- This time, we are adding the 2 from location 5 to the 2 in the accumulator, yielding 4.
- And so on.
From now on, each time the Simple CPU executes instruction 2, it doubles the contents of the accumulator. Each time it executes instruction 3, it saves the doubled number. Each time it executes instruction 4, it loops, which repeats the process.
Note that the program will never try to treat location 5 as an instruction, since it always jumps back to 2 before reaching location 5.
Program Coding
The CPU doesn’t know how to read those typed instructions, but it knows how to interpret numbers. Therefore, we must encode the program in numeric form. The code works as shown in this table:
Instruction
|
Code
|
Example Text
|
Example Code
|
Add xx | 1xx | Add 23 | 123 |
Store xx | 2xx | Store 23 | 223 |
Jump xx | 3xx | Jump 5 | 305 |
Stop | 0 | Stop | 0 |
Invert | 1 | Invert | 1 |
Clear | 2 | Clear | 2 |
Noop | 3 | Noop | 3 |
The sample program given above requires that the following code be stored in memory:
- 2
- 105
- 205
- 302
- 1
Subtraction
Even though there isn’t a “subtract” instruction, you can still subtract one number from another. You just have to use additional instructions to do it.
To subtract the contents of one location from another, you must “add the negative.” To subtract the contents of location 10 from location 11, for example, put the contents of 10 into the accumulator, make it negative with the Invert instruction, and then add the contents of 11 to it. Here are the instructions:
- Clear
- Add 10
- Invert
- Add 11
Mistakes to Avoid
The instruction 101 does not necessarily add “1” to the accumulator. It adds the contents of location 1 to the accumulator. If location 1 happens to contain the value “1” then it adds 1 to the accumulator.
Do not confuse the memory locations containing instructions with locations containing data. In the first sample program, location 5 contains data and locations 1 through 4 contain instructions. The Add and Store instructions must point to data and the Jump instruction must point to another instruction.
The sample program includes a “loop” that makes the program run without ever stopping. If your program needs to stop, be sure to put a Stop instruction at the end. If it keeps going, it might treat your data as if it were instructions, and that could cause mistakes!
You must be logged in to post a comment.