![]() |
Simple CPU Demo |
Dr. Rick Smith
|
|
Dr. Smith Home | Research | Classes | Cryptosmith | QMCS Home | UST A-Z | UST Home
Simple CPU Home Page | Simple CPU Spreadsheet last update: |
||
CPU = Central Processing Unit
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.
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.
The CPU can only execute instructions that it knows about. The Simple CPU only knows six instructions. They fall into two categories:
Here are the memory instructions:
Here are the operating instructions:
For as long as this program runs, it doubles the number in storage location 5:
The program works like this:
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.
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:
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:
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!
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.
To run the Simple CPU, do the following:
This work by Rick Smith (resmith@stthomas.edu) is licensed under a Creative Commons License.