Central processing unit

This article does not have any sources. You can help Wikipedia by finding good sources, and adding them.(February 2014)

A Pentium CPU inside a computer

A central processing unit (CPU) is an important part of almost every computer. The CPU sends signals to control the other parts of the computer, almost like how a brain controls a body.

The CPU is an electronic machine that works on a list of things to do, called 'instructions'. It reads the list of instructions and does (executes) each one in order. A list of instructions that a CPU can run is a computer program.

The speed that a CPU works at is measured in hertz (Hz). Modern processors often run so fast that gigahertz (GHz) is used instead, which means a billion cycles per second.

There are mainly two different types of CPUs used in modern desktop systems: 32-bit CPUs and 64-bit CPUs. The instructions in a 32-bit CPU are good at handling data that is 32 bits in size (most instructions "think" in 32 bits in a 32-bit CPU). Likewise, a 64-bit CPU is good at handling data that is 64 bits in size (and is often good at handling 32-bit data too). The size of data that a CPU handles best is often called the word size of the CPU. Many older CPUs from the 70s, 80s and early 90s (and some modern small CPUs) have an 8-bit or 16-bit word size.

When the CPU runs a computer program, it needs somewhere to store the data that the instructions operate on (the data that they read and write). This storage is called a register. A CPU usually has many registers. Registers must be very fast to access (to read and write). Therefore they are part of the CPU chip itself.

Storing all data in registers would usually make the CPU too complicated (and very expensive). Therefore, registers usually only store the data that the CPU is working on "right now". The rest of the data used by the program is stored outside the CPU in RAM (memory) in separate chips.

When the CPU wants to read or write some data in RAM, it outputs an address to that data. Each byte in RAM has an address. The size of addresses is often the same as the word size: A 32-bit CPU uses 32-bit addresses, etc. (However, smaller CPUs, like 8-bit CPUs, often have an address size that is larger than the word size. Otherwise the maximum program length would be too short.) Because the size of addresses is limited, the maximum amount of memory is also limited.

The 32-bit processor can usually only handle up to 4 GB of RAM. This is the number of different bytes that can be selected using a 32-bit address (each bit can have two values - 0 and 1 - and 232 bytes is 4 GB). A 64-bit processor might be able to handle up to 16 EB (16 exabytes, around 16 billion GB, or 16 billion billion bytes) of RAM.

The maximum amount of memory that programs can use also depends on the operating system used.

On modern computers, RAM is much slower than registers, so accessing RAM slows down programs. To speed up memory accesses, a faster type of memory called a cache is often put between the RAM and the main parts of the CPU. The cache is usually a part of the CPU chip itself, and is much more expensive per byte than RAM. The cache stores the same data as RAM, but is usually much smaller. Therefore all the data used by the program might not fit in the cache. The cache tries to store data that is likely to be used a lot. Examples include recently used data and data close in memory to recently used data.

Often it makes sense to have a "cache for the cache", just as it makes sense to have a cache for RAM. In multi-level caching, there are many caches, called the L1 cache, the L2 cache, and so on. The L1 cache is the fastest (and most expensive per byte) cache and is "closest" to the CPU. The L2 cache is one step away and is slower than the L1 cache, etc. The L1 cache can often be viewed as a cache for the L2 cache, etc.

Buses are the "wires" used by the CPU to communicate with RAM and other components in the computer. Almost all CPUs have at least a data bus - used to read and write data - and an address bus - used to output addresses.

An instruction set (also called an ISA - Instruction Set Architecture) is a language understood directly by a particular CPU. These languages are also called machine code or binary. They say how you tell the CPU to do different things, like loading data from memory into a register, or adding the values from two registers. Each instruction in an instruction set has an encoding, which is how the instruction is written as a sequence of bits.

Programs written in programming languages like C and C++ can't be run directly by the CPU. They must be translated into machine code before the CPU can run them. A compiler is a computer program that does this translation.

Machine code is just a sequence of 0s and 1s, which makes it difficult for humans to read it. To make it more readable, machine code programs are usually written in assembly language. Assembly language uses text instead of 0s and 1s: You might write "LD A,0" to load the value 0 into register A for example. A program that translates assembly language into machine code is called an assembler.

Move a number from one place to another (for example, from one register to another, or between a register and memory).

Jump to another place in the instruction list.

Jump to another place in the instruction list, but only if some test is true (for example, only if one number is bigger than another).

Even very complicated programs can be made by combining many simple instructions like these. This is possible because each instruction takes a very short time to happen. Many CPUs today can do more than 1 billion (1,000,000,000) instructions in a single second. In general, the more a CPU can do in a given time, the faster it is. One way to measure a processor's speed is MIPS (Million Instructions Per Second). Flops (Floating-point operations per second) and CPU clock speed (usually measured in gigahertz) are also ways to measure how much work a processor can do in a certain time.

A CPU is built out of logic gates; it has no moving parts. The CPU of a computer is connected electronically to other parts of the computer, like the video card, or the BIOS. A computer program can control these peripherals by reading or writing numbers to special places in the computer's memory.

Each instruction executed by a CPU is usually done in many steps. For example, the steps to run an instruction "INC A" (increase the value stored in register A by one) on a simple CPU could be this:

Read the instruction from memory,

decode the instruction (figure out what the instruction does), and

add one to register A.

To run these steps, the CPU could have a separate module (separate electronics inside the CPU) that runs each step. The output from one module (for example, the bits of the instruction) would be the input to the next module (for example, the instruction decoder). A set of modules connected like this is called a pipeline. Pipelines can be found in many other places in electronics too, not just in CPUs.

Often it is possible to run some steps from different instructions at the same time, which makes the CPU faster. For example, we can read an instruction from memory at the same time that we decode another instruction, since those steps use different modules. This can be thought of as having many instructions "inside the pipeline" at once. In the best case, all of the modules are working on different instructions at once, but this is not always possible.

Modern CPUs often use a memory management unit (MMU). An MMU is a component that translates addresses from the CPU to (usually) different RAM addresses. When using an MMU, the addresses used in a program are (usually) not the "real" addresses where the data is stored. This is called virtual (the opposite of "real") memory. A few of the reasons why it is good to have an MMU are listed below:

An MMU can "hide" the memory of other programs from a program. This is done by not translating any addresses to the "hidden" addresses while the program is running. This is good because it means that programs can't read and modify the memory of other programs, which improves security and stability. (Programs can't "spy" on each other, or "step on each other's toes".)

Many MMUs can make some parts of memory non-writeable, non-readable, or non-executable (meaning code stored in that part of memory can't be run). This can be good for stability reasons and security reasons, as well as for other reasons.

MMUs allow different programs to have different "views" of memory. This is handy in many different situations. For example, it will always be possible to have the "main" code of a program at the same (virtual) address without colliding with other programs. It is also handy when there are many different pieces of code (from libraries) that are shared between programs.

MMUs allow code from libraries to appear at different addresses every time a program is run. This is good because not knowing where things are in memory often makes it harder for hackers to make programs do bad things. This is called address space randomization.

Advanced programs and operating systems can use tricks with MMUs to avoid having to copy data between different places in memory.

Some newer processors have multiple cores. This means that they have many processors built on to the same chip so that they can run many instructions at once.

While the individual cores might be slower than a single core processor, all the cores can work together to go faster. This means that the GHz might be lower, however the overall speed of the processor will be higher.

To make an analogy, think of traffic lanes;

One road having one lane, which takes up 50 cars in 10 minutes.

Another road with two lanes will take 100 cars in the same 10 minutes and if one of the lanes has a problems cars can go to the other lane.

In today's modern computers, some processors may have up to eight cores and are available to all consumers, like the AMD FX-8350.