Every kind of computer system requires one or more processors which carry out all the activity. From the earliest days of ENIAC and EDVAC to today’s laptops and tablets, most computers carry just one processor which is why it has come to be known as the CPU, which is short for Central Processing Unit.
To understand how a CPU works, it is useful to have an analogy. Imagine finding yourself alone in an IKEA store with the plan for a complicated piece of furniture. The plan has been cut up into individual steps, each on their own little piece of paper. How would you go about working?
You would likely proceed somewhat like this: Fetch the first piece of paper. Look at the instruction on it. Figure out what parts you need. Go to the warehouse and get the necessary parts. Put those parts together. Take the combined work product and put it somewhere. Move on to the second piece of paper and repeat until the piece of furniture is finished.
That in essence is how a CPU works. It fetches an instruction from memory, decodes what the instruction says, loads the necessary data, executes the instructions, and writes the results back to memory (more on what exactly memory is next Tuesday). It repeats that cycle until the execution of the program has finished. That’s it!
On most CPUs, an instruction consists of two parts: an opcode and an address. The opcode (short for operation code) tells the CPU what it is supposed to be doing. The address tells the CPU where the data for that operation can be found. The list of all available instructions on a particular CPU are known as the instruction set. The first CPU that I learned to program on was the 6502, which I first encountered on a Commodore PET and then in my Apple II. The 6502 instruction set had a total of 56 instructions. I am writing this on a MacBook with and Intel Core 2 Duo which has a little over of 200 different instructions.
One of the interesting things to note here is that there aren’t really a lot of instructions. You can think of these as the computer equivalent of chemical elements. There aren’t a lot of elements in the world (118 are known to us), but when combined they form molecules and the combination of molecules forms everything around us. The same is true for computers. The instructions can be combined into slightly larger chunks known as subroutines (the equivalent of molecules). Subroutines can then be combined into programs. At some later point in Tech Tuesdays we will discover that very few operations are truly required to build a general purpose computer. For now let’s just say that at varying times people have worked on reducing the number of different instructions, which has given rise to the acronym RISC (reduced instruction set computer) which you may have seen before.
As you might suspect based on last Tuesday’s “Of Bits and Bytes,” both opcodes and addresses are nothing other than sequences of bits. The CPU is built in a way so that a specific sequence of opcode bits stand for some particular operation to be carried out. In other words, just like ASCII is a mapping between letters and bits, the opcode is a mapping between operations and bits. Similarly, the address sequence of bits denotes a particular location in memory (a term I am using loosely here on purpose).
The complete sequence of fetching an instruction, decoding it, getting the data ready, executing the operation and moving the data back is known as a cycle. So how many cycles does a CPU process per second? For the 6502 in my Apple II, the answer was rather tame 1 million cycles per second. The answer for the Intel Core 2 Duo in my MacBook is mind boggling 2.4 billion (!) cycles per second. This measure is referred to as the clockspeed and is expressed these days in Giga Hertz (GHz), where Giga is the billion and Hertz means simply cycles per second. You may have heard people talk about “overclocking” - that refers to running a CPU at a higher clockspeed than originally intended.
There is a lot more to a CPU than covered in this first look. For instance, how does the CPU keep track of which instruction it is on? And where does it store intermediate work results? We will come back to these questions in upcoming Tech Tuesdays and as we do, we will keep revisiting the IKEA analogy from this post.