The heart of a computer is the central processing unit or CPU. This device contains all the circuitry that the computer needs to manipulate data and execute instructions. The CPU is amazingly small given the immense amount of circuitry it contains. We have already seen that the circuits of a computer are made of gates. Gates, however are also made of another tiny component called a transistor, and a modern CPU has millions and millions of transistors in its circuitry. The image to the right [Intel 2000] shows just how compact a CPU can be. The CPU is a PentiumŪ III processor for mobile PCs.

The CPU is composed of five basic components: RAM, registers, buses, the ALU, and the Control Unit. Each of these components are pictured in the diagram below. The diagram shows a top view of a simple CPU with 16 bytes of RAM. To better understand the basic components of the CPU, we will consider each one in detail.

  • RAM: this component is created from combining latches with a decoder. The latches create circuitry that can remember while the decoder creates a way for individual memory locations to be selected.

  • Registers: these components are special memory locations that can be accessed very fast. Three registers are shown: the Instruction Register (IR), the Program Counter (PC), and the Accumulator.

  • Buses: these components are the information highway for the CPU. Buses are bundles of tiny wires that carry data between components. The three most important buses are the address, the data, and the control buses.

  • ALU: this component is the number cruncher of the CPU. The Arithmetic / Logic Unit performs all the mathematical calculations of the CPU. It is composed of complex circuitry similar to the adder presented in the previous lesson. The ALU, however, can add, subtract, multiply, divide, and perform a host of other calculations on binary numbers.

  • Control Unit: this component is responsible for directing the flow of instructions and data within the CPU. The Control Unit is actually built of many other selection circuits such as decoders and multiplexors. In the diagram above, the Decoder and the Multiplexor compose the Control Unit.

In order for a CPU to accomplish meaningful work, it must have two inputs: instructions and data. Instructions tell the CPU what actions need to be performed on the data. We have already seen how data is represented in the computer, but how do we represent instructions? The answer is that we represent instructions with binary codes just like data. In fact, the CPU makes no distinction about the whether it is storing instructions or data in RAM. This concept is called the stored-program concept. Brookshear [1997] explains:

"Early computing devices were not known for their flexibility, as the program that each device executed tended to be built into the control unit as a part of the machine...One approach used to gain flexibility in early electronic computers was to design the control units so they could be conveniently rewired. A breakthrough came with the realization that the program, just like data, can be coded and stored in main memory. If the control unit is designed to extract the program from memory, decode the instructions, and execute them, a computer's program can be changed merely by changing the contents of the computer's memory instead of rewiring the control unit. This stored-program concept has become the standard approach used today. To apply it, a machine is designed to recognize certain bit patterns as representing certain instructions. This collection of instructions along with the coding system is called the machine-language because it defines the means by which we communicate algorithms to the machine."

Thus both inputs to the CPU are stored in memory, and the CPU functions by following a cycle of fetching an instruction, decoding it, and executing it. This process is known as the fetch-decode-execute cycle. The cycle begins when an instruction is transferred from memory to the IR along the data bus. In the IR, the unique bit patterns that make up the machine-language are extracted and sent to the Decoder. This component is responsible for the second step of the cycle, that is, recognizing which operation the bit pattern represents and activating the correct circuitry to perform the operation. Sometimes this involves reading data from memory, storing data in memory, or activating the ALU to perform a mathematical operation. Once the operation is performed, the cycle begins again with the next instruction. The CPU always knows where to find the next instruction because the Program Counter holds the address of the current instruction. Each time an instruction is completed, the program counter is advanced by one memory location.

Each machine instruction is composed of two parts: the op-code and the operand. According to Brookshear [1997], "the bit pattern appearing in the op-code field indicates which of the elementary operations, such as STORE or JUMP, is requested by the instruction. The bit patterns found in the operand field field provide more detailed information about the operation specified by the op-code. For example, in the case of a STORE operation, the information in the operand field indicates which register contains the data to be stored and which memory cell is to receive the data." The image to the right shows the format of an instruction for our CPU. The first three bits represent the op-code and the final six bits represent the operand. The middle bit distinguishes between operands that are memory addresses and operands that are numbers. When the bit is set to '1', the operand represents a number. A simple set of machine instructions for our CPU are listed in the table below. Notice that all the op-codes are given an English mnemonic to simplify programming. Together these mnemonics are called an assembly language. Programs written in assembly language must be converted to their binary representation before the CPU can understand them. This usually done by another program called an assembler, hence the name.

Op-code Mnemonic Function Example
001 LOAD Load the value of the operand into the Accumulator
010 STORE Store the value of the Accumulator at the address specified by the operand STORE 8
011 ADD Add the value of the operand to the Accumulator ADD #5
100 SUB Subtract the value of the operand from the Accumulator SUB #1
101 EQUAL If the value of the operand equals the value of the Accumulator, skip the next instruction EQUAL #20
110 JUMP Jump to a specified instruction by setting the Program Counter to the value of the operand JUMP 6
111 HALT Stop execution HALT
A simple machine language

In the machine language above, notice that some of the operands include a # symbol. This symbol tells the CPU that the operand represents a number rather than a memory address. Thus, when the assembler translates an instruction with a # symbol, the resulting machine code will have a '1' in the position of the number bit. Also notice the central role that the Accumulator register plays. Nearly all the operations affect the value of this register since the Accumulator acts as a temporary memory location for storing calculations in progress. With our machine language defined, we are ready to take a look at some simple programs.

The first program is called Sum. This program adds the numbers stored in two memory locations. Mathematically, this program represents the formulas x = 2, y = 5, x + y = z where the variables x, y, and z correspond with the memory locations 13, 14, and 15 respectively. The instructions for the program are listed below. Read through the program, and then view the animation of this program by clicking the "View Animation" link.

# Machine code Assembly code Description
0 001 1 000010 LOAD   #2 Load the value 2 into the Accumulator
1 010 0 001101 STORE  13 Store the value of the Accumulator in memory location 13
2 001 1 000101 LOAD   #5 Load the value 5 into the Accumulator
3 010 0 001110 STORE  14 Store the value of the Accumulator in memory location 14
4 001 0 001101 LOAD   13 Load the value of memory location 13 into the Accumulator
5 011 0 001110 ADD    14 Add the value of memory location 14 to the Accumulator
6 010 0 001111 STORE  15 Store the value of the Accumulator in memory location 15
7 111 0 000000 HALT      Stop execution
Sum program   [view animation]

The second program is called Count. This program counts up to a number specified by the programmer in the first instruction. Notice that this program incorporates a loop construction by using the JUMP and EQUAL instructions. Every time the value in the Accumulator is incremented, the count is tested to see if it has reached the specified amount. Read through the program, and then view the animation of this program by clicking the "View Animation" link.

# Machine code Assembly code Description
0 001 1 000101 LOAD   #5 These two operations set the count value to five
1 010 0 001111 STORE  15
2 001 1 000000 LOAD   #0 Initialize the count to zero
3 101 0 001111 EQUAL  15 Test to see if count is complete; if yes, skip next instruction and go to instruction 5; if no, go to next instruction
4 110 1 000110 JUMP   #6 Set Program Counter to 6
5 111 0 000000 HALT     Stop execution
6 011 1 000001 ADD    #1 Increment the count in the Accumulator
7 110 1 000011 JUMP   #3 Set Program Count to 3
Count program   [view animation]