We no longer offer ftp downloads. If there is a file you need referenced here, please contact me by email and I will get it to you.
The CPU (Central Processing Unit) is the heart of any computer, but the operating system is the brain. Unfortunately, understanding exactly how these things really work can be difficult, because it's fairly hard to "play" with the operating system that you are actually using. You can do quite a bit with sophisticated debuggers, but eventually you run into confusion and difficulty. And, as you try more complex tasks, you run the risk of interfering with the real machine's operating system. Finally, modern CPU's are very complex, and that complexity can make it more difficult to understand basic concepts.
I therefore thought it would be interesting to emulate CPU's and other hardware in software. By doing that, we can present simple ideas and build on that understanding. We'll start very simply, and build from there, eventually demonstrating more advanced concepts like virtual memory, disk filesystems, and even multitasking.
Our first CPU is very simple. The code to run it is a simple Perl script: ftp://aplawrence.com/pub/yourcpu/cpu1.pl We have no input and output; we have to preload everything we need into our programs. The earliest computers weren't much different: front panel switches and lights to show register contents.
You don't need to understand this script at all as long as it works on your system. If it doesn't work, it's probably either (Unix/Linux) that the first line needs to be changed to match where Perl is found on your machine ("which perl") or (Windows) that you haven't associated the ".pl" extension with Perl, or you haven't installed Perl at all.
This CPU reads its instructions from a file called "memory1" in your current directory. It only understands three instructions, which are the letters "L" for LOAD, "A" for ADD, and "H" for HALT. You get these instructions into memory simply by editing "memory1" with a text editor. Then just run cpu1.pl to see the results. For example, we might create memory1 to contain:
You can do that with "vi". If you are using Windows, it will probably be easier if you modify "cpu1.pl" to read from "memory1.txt" and use Notepad to create the file. Our program doesn't do much: it loads "1" into register A, "2" into register B, adds B to A, and finally adds A to B. Pretty weak, but not so very many years ago a machine that could do just that was pretty amazing stuff.
If you create the file, run "./cpu1.pl", and type "g" after the first display and you will get:
REGISTERS A: 0 B: 0 C: D: IP: 0 CMD: LOAD A 1 ================================= REGISTERS A: 1 B: 0 C: D: IP: 3 CMD: LOAD B 2 ================================= REGISTERS A: 1 B: 2 C: D: IP: 6 CMD: ADD A B ================================= REGISTERS A: 3 B: 2 C: D: IP: 9 CMD: ADD B B ================================= REGISTERS A: 3 B: 4 C: D: IP: 12 CMD: HALT =================================
If you just press ENTER instead of "g" you will "single-step" through the code.
Our simple CPU ignores instructions it doesn't understand, and will stop automatically when it reaches the end of its memory (the end of "memory1"). So, you can "run" this CPU on arbitrary text. It won't necessarily do anything useful of course.
So what is this teaching us? First, all CPU's use registers, which are simply storage on the CPU chip itself. The advantage of registers (on real CPU's) is that manipulation of data in registers is very fast - much faster than memory. The "IP" is the Instruction Pointer: the address in memory that it is reading instructions from. This is simply the number of bytes it has read into our "memory1" file: The 6th character of our test file is A followed by AB.
Our simple CPU can't handle numbers greater than 9, at least on the LOAD command. And it only shows you four registers: A through D. So, even if it were real hardware, it wouldn't be very useful. Yet, the very first CPU's didn't have much more power than this.
To get more power, we need more instructions, and we need an easier way to write programs.
Our second CPU is a little more powerful: ftp://aplawrence.com/pub/osconcepts/cpu2.pl. You'll also need ftp://aplawrence.com/pub/osconcepts/asm2.pl. Again, you don't need to understand either of these. The "asm2.pl" is an assembler for our cpu2.pl processor; it will create "machine language" instructions in a file called "memory2". Our first program will be very simple. Put this text into a file called "prog1":
LOAD A 1 LOAD B 2 LOAD C 2 LOAD D 1 HALT
If you are using a Linux or Unix system, run the commands
./asm2.pl prog1 && ./cpu2.pl
If not, run asm2.pl and cpu2.pl separately.
You should get:
REGISTERS A: 0 B: 0 C: D: AB: 0 0 CD: 0 0 ABCD: 0 0 FLAGS: IP: 0 CMD: LOAD A 1 ================================= REGISTERS A: 1 B: 0 C: D: AB: 256 1 CD: 0 0 ABCD: 16777216 1 FLAGS: IP: 3 CMD: LOAD B 2 ================================= REGISTERS A: 1 B: 2 C: D: AB: 258 513 CD: 0 0 ABCD: 16908288 513 FLAGS: IP: 6 CMD: LOAD C 2 ================================= REGISTERS A: 1 B: 2 C: 2 D: AB: 258 513 CD: 512 2 ABCD: 16908800 131585 FLAGS: IP: 9 CMD: LOAD D 1 ================================= REGISTERS A: 1 B: 2 C: 2 D: 1 AB: 258 513 CD: 513 258 ABCD: 16908801 16908801 FLAGS: IP: 12 CMD: ADD A D ================================= REGISTERS A: 2 B: 2 C: 2 D: 1 AB: 514 514 CD: 513 258 ABCD: 33686017 16908802 FLAGS: IP: 15 CMD: HALT =================================
Of you like, you can see what the assembler put into memory by examining "memory2". On Linux/Unix, "od -cx memory2" is a good way to see it. Or just enter "D" while the cpu is running. You'll notice that we've tried to use ASCII characters where possible; for example LOAD A will end up in memory as LA. No real cpu would be so wasteful: a single byte would likely encode the instruction and the register it referenced. That is done for efficiency in hardware and assemblers. We have no need of efficiency, and it's helpful to have memory dumps a little easier to decode.
We still have some limitations with this version. First, our registers can't hold any more than 255 - an 8 bit number. In this little "prog1" we didn't use anything that large anyway.
Fact is, CPU registers are always going to be limited as to how big a number they can hold. On modern CPU's, that's going to be more than 8 bits, and might even be 64 bits on the latest hardware, but it will always be limited. And while 64 bits is a very large number, your programming may need to work with bigger numbers. You'll certainly need to deal with more than 255, so our cpu2.pl is a rather useless processor, right?
Well, maybe. You may have noticed these lines in the output:
AB: 514 514 CD: 513 258 ABCD: 33686017 16908802
Those are what the registers would represent if you strung them together. This is binary math (I know, your head hurts already), but lets not worry about how that works just now. Just accept that if register A is "1" and register B is "2", then A and B together are either 513 or 258, depending on how your machine looks at numbers.
Huh? Which is it? Well, as I said, it depends. If your computer uses "little-endian" numbers (Intel for example), it's 258, but on a Mac (normally big-endian), it's 513. For our purposes here, it really doesn't matter: we could write programs to add up larger numbers either way simply by deciding which way we want to look at the numbers. To write such a program we need some new instructions. Save this as "prog2" and run "./asm2.pl prog2 && ./cpu2.pl".
LOAD A 0 LOAD B 0 LOAD C 1 LOAD D 5 LOAD E 253 LOAD F 0 LOOP: ADDREGS A E JUMPNOTZERO CHECK: ADDREGS B C CHECK: SUBTRACTREGS D C JUMPNOTZERO LOOP: HALT JUMP LOOP:
This program will multiply 253 by 5 and the result will be in AB.
Note that we have the ability to set labels in our code and to jump to the address represented by the label. We also introduce the concept of "flags", which are simply a special kind of register that will be set when a condition becomes true. For the moment, we have only two flags: OV, which is set when a register rolls over from its maximamum value of 255, and ZERO, which is set when the result of an add or subtract is zero. We then need a JUMPNOTZERO instruction to make use of the flags (in this simple CPU, "ZERO" is set for the OV condition also).
If you step through this (or just hit G ), you'll see that the value of A increments. When it overflows at 255, it becomes 0 but we then add 1 to B, and continue. We do that 5 times and stop.
What would happen if we didn't stop after 5? Of course B would keep on incrementing and eventually it would overflow too. That wouldn't take all that long: our emulated CPU is very slow, but it wouldn't take but a very few minutes. On a real Pentium class machine, executing similar instructions would overflow a 32 bit register in a very few seconds, but when we start using 64 bit registers we get an entirely different animal. For example, Pentium CPU's have a 64 bit register called the TSC (Time Stamp Counter) that counts processor clock cycles since reset (or since it was programmatically zeroed). Consider that a 1 Gigahertz cpu increments that register 1 billion times per second: start it from 0, wait 1 second and its value is 1 billion. Of course 64 bits can hold values up to 16 billion billion , so it will take 16 billion seconds to overflow (actually it's 4GB * 4GB so it's a bit longer, but 16 billion is a nice round figure) That's 500 years or so; a bit of a wait. That's one of the reasons we designed this fake cpu with arbitrarily small registers: it's easier to see overflows happen.
With very little more effort, we could actually use this to multiply numbers.
This multiplies 253 by 5 and the result is in AB.
Our simple CPU has a very limited repertoire. We don't have a lot of power in this cpu, yet we can actually (with enough effort) do almost any programming task with just these simple instructions. A hundred years ago that would have been more than incredible, and even 40 years ago we didn't demand a lot more than that. I'll continue with this series as I have the time; we'll add simulations of virtual memory, a protected memory mode, i/o, primitive task scheduling and more.
Got something to add? Send me email.
More Articles by Tony Lawrence © 2010-10-30 Tony Lawrence