How does a computer CPU work ?

BACKGROUND: How a computer ‘does things’

Firstly here’s a quick and simple backgrounder as to how a computer works. A computer does things by basically executing sets of instructions. These sets of instructions are called programsExample program instructions to continually accept 2 user number inputs, Add them & print it out :-
10 READ : Input #1
20 READ : Input #2
30 ADD: Input#1 + Input#2
40 PRINT : Result
50 GOTO : Line 10
. Programs run on top of the machines’ Operating System, like Microsoft Windows. It is the Operating System that loads a program and runs the instructions contained in it. The Operating Sytem acts like a ‘middle-man’ interacting between the program and the computers hardware, like the mouse or screen or memory or disk. The mouse might be moved by the user and the Operating System will then tell the running program what happened in case it wants to do anything about it. Likewise when a program wants to say send something to the printer attached to the PC it will tell the Opearing System it wants to talk to the printer.

A program like Microsoft Word takes inputs like your mouse clicks and executes the appropriate set of instructions. : load a file; save a file; format text etc. In the course of doing this many low level operations are performed : move a number from memory location X to location Y; get the value from memory at location A; get the value from memory location B; add them together and store it in memory location C. This all happens extremely quickly and seamlessly from your point of view. It’s within this maelstrom of low level activity that we want to look at what the CPU is and what it is doing that enables all this to happen.

See panel below for a list of the computers key components

So … What is a CPU?

The Central Processing Unit, or CPU, is considered to be the brain of a computer. Without this vital component, the computer wouldn’t be able do any of the functions it is capable of doing. Basically, it is a chip that handles the moving and processing of data and the execution of programming instructions. It’s responsible for receiving and processing the input from the keyboard, the mouse, the web-cam and any other number of peripheral devices. All hardware components and programs installed on the system must go through the CPU before their function can be carried out.

1. The Control Unit

The control unit of the CPU contains circuitry that directs the entire computer system to carry out, or execute, stored program instructions. However the control unit does not execute program instructions; rather, it directs other parts of the system to do so. The control unit communicates with both the arithmetic/logic unit (ALU) and memory.

2. The Arithmetic/Logic Unit (ALU)

The arithmetic/logic unit (ALU) contains the electronic circuitry that executes all arithmetic and logical operations.

The ALU can perform the 4 basic kinds of arithmetic operations: addition, subtraction, multiplication, and division. And, as its name implies, the arithmetic/logic unit also performs logical operations.

Logical operations are basically comparisons that test for certain conditions. If the condition is met True is returned otherwise False. These are also know as boolean values :

Equal-to condition : Compares 2 numbers, or pieces of text to see if they are the same.

Less-than condition : Compares 2 numbers, or pieces of text to see if one is greater than the other.

Greater-than condition : Compares 2 numbers, or pieces of text to see if one is greater than than the other.

The above might seem trivial but it is at the core of how a computer program is able to tell what to do. For instance are there are unfilled seats on an airplane has a credit card exceeded it’s credit limit, is the mouse hovering over a link in a web page etc etc.

3. Registers (Temporary Storage Areas)

Registers are temporary storage areas for program instructions or data. They are not a part of memory; rather they are special additional storage locations that offer the advantage of speed. Registers work under the direction of the control unit to accept, hold, and transfer instructions or data and perform arithmetic or logical comparisons at high speed. The control unit uses a data storage register the way a store owner uses a cash register-as a temporary, convenient place to store what is used in transactions.

Registers are usually assign special roles and come in 4 ‘flavours’:

Accumulator register – which collects the result of computations.

Address register – keeps track of where a given instruction or piece of data is stored in memory.

Storage register – temporarily holds data taken from or about to be sent to memory.

General register – used for several miscellaneous other functions.

4. Memory and Storage

This is not technically part of the CPU. It is used by it to hold data and instructions for processing (outside of the Registers). Although closely associated with the CPU, memory is separate from it. It’s only being mentioned here as it is an essential componant to CPU operation acting as a ‘reserve tank’ of data and instructions waiting to be accessed by it.

If more than one program is running at once (often the case on large computers and sometimes on small computers), a single program can not lay exclusive claim to memory. There may not be room in memory to hold the processed data in which case it is then stored to disk.

For speed of access, quickest to slowest, it’s : Registers, RAM, hard disk.

PUTTING IT ALL TOGETHER : How does it all work?

All CPUs use the same basic set of operations when running a program instructions:

  1. Fetch
  2. Decode
  3. Execute
  4. Writeback
1. Fetch

The control unit fetches (gets) the instruction from memory. The location in program memory is determined by a program counter (PC), which stores a number that identifies the current position in the program. In other words, the program counter keeps track of the CPU’s place in the current program. After an instruction is fetched, the PC is incremented by the length of the instruction word in terms of memory units. This can be thought of like a finger advancing to the next instruction in a list of instructions (the program). Often the instruction to be fetched must be retrieved from relatively slow memory (RAM), causing the CPU to stall while waiting for the instruction to be returned. This issue is largely addressed in modern processors by caches and pipeline architectures. The instruction that the CPU fetches from memory is used to determine what the CPU is to do.

2. Decode

The control unit then decodes the instruction (decides what it means). The instruction is broken up into parts that have significance to other portions of the CPU. The way in which the numerical instruction value is interpreted is defined by the CPU’s instruction set architecture (ISA).Often, one group of numbers in the instruction, called the opcode, indicates which operation to perform. The remaining parts of the number usually provide information, ie that data, required for that instruction. The control unit then passes this information to the Arithmetic Logic Unit (ALU)

3. Execute

The ALU is given control and performs the actual operation on the data. If, for instance, an addition operation was requested, an arithmetic logic unit (ALU) will be connected to a set of inputs and a set of outputs. The inputs provide the numbers to be added, and the outputs will contain the final sum. The ALU contains the circuitry to perform simple arithmetic and logical operations on the inputs (like addition and bitwise operations).

4. Writeback

In the final step, writeback, the ALU simply “writes back” the results of the execute step to some form of memory. Very often the results are written to some internal CPU register for quick access by subsequent instructions. In other cases results may be written to slower, but cheaper and larger, main memory (RAM). Some types of instructions manipulate the program counter rather than directly produce result data. These are generally called “jumps” and facilitate behavior like loops, conditional program execution (through the use of a conditional jump), and functions in programs. Many instructions will also change the state of digits in a “flags” register. These flags can be used to influence how a program behaves, since they often indicate the outcome of various operations. For example, one type of “compare” instruction considers two values and sets a number in the flags register according to which one is greater. This flag could then be used by a later jump instruction to determine program flow.

…and repeat it all again…

After the execution of the instruction and writeback of the resulting data, the entire process repeats, with the next instruction in the program. If the completed instruction was a jump, the program counter will be modified to contain the address of the instruction that was jumped to, and program execution continues normally. In more complex CPUs, multiple instructions can be fetched, decoded, and executed simultaneously.


Background : Key computer components

Broadly speaking the main components of a computer are :

CPU : The brains of the machine.

Memory : Data storage for the running computer (also called RAM or Random Access Memory)

Disk Drive : Storage of data. Unlike RAM data stored on this device will survive when the PC is switched off. ie a permanent storage.

Author: Robert

Share This Post On

Pin It on Pinterest

%d bloggers like this: