Remember To Thank Your CPU

Jonathan Freed
5 min readMar 7, 2019

I became fascinated with computers when I fell down a rabbit hole one day and learned a bit about how they actually work. I was surprised by how little I knew and how much I’d taken computers for granted.

For example, did you know that when you type, your typing goes through several programs before it hits your editor? First, the kernel is what handles your hardware, so it’s the first to receive notice about the keypress. The keyboard sends in scancodes to the kernel, which then converts them to the actual letters, numbers, and symbols they represent. If you’re using a windowing system, it reads the keypress from the kernel and delivers it to whatever program is currently in focus on the display. Like so:

Keyboard -> Kernel -> Windowing system -> Application program

The kernel controls the flow of information between programs, acting as a program’s gate to the world around it. Every time data moves between processes the kernel controls the messaging, preventing programs from accidentally overwriting each other’s data and accessing files and devices that they don’t have permission to.

Von Neumann Architecture

Modern computer architecture is based on the Von Neumann architecture, named after its creator, John Von Neumann. The Von Neumann architecture divides the computer into two main parts — the CPU (Central Processing Unit) and the memory. This architecture is used in all modern computers, including personal computers, supercomputers, mainframes, and smartphones.

The computer views memory a lot like a post office with a room filled with PO Boxes. These boxes are similar to computer memory in that they’re each numbered sequences of fixed-size storage locations. For example, if you have 256 megabytes of computer memory, that means that your computer contains roughly 256 million fixed-size storage locations - or 256 million PO Boxes. Each location has a number and the same, fixed-length size. The difference between a PO Box and computer memory is that you can store all different kinds of things in a PO Box, but you can only store a single number in a computer memory storage location.

The computer’s memory is used for lots of different things. Really, everything that’s “stored” is stored in memory. Just a few things stored in your computer’s memory:

  • The location of your cursor on the screen.
  • The size of each window on the screen.
  • The shape of each letter of each font being used.
  • The layout of all of the controls on each window.
  • The graphics for all of the toolbar icons.
  • And on and on…

The Von Neumann architecture specifies that not only should computer data live in memory, but the programs that control the computer’s operation should live there, too. In a computer, there’s no difference between a program and a program’s data except how it’s used by the computer. They’re both stored and accessed the same way.

The CPU

Data needs to be accessed, manipulated, and moved, which is where the CPU comes in. The CPU takes in instructions from memory and executes them one at a time. This is what’s being referred to when people talk about the fetch-execute cycle.

The CPU accomplishes this with:

  • The Program Counter
  • The Instruction Decoder
  • The Data bus
  • General-purpose registers
  • Arithmetic and logic unit

The program counter tells the computer where to fetch the next instruction and holds the memory address of the next instruction to be executed. The CPU looks at the program counter and fetches whatever number is stored in memory at the specified location. Then comes the instruction decoder, which figures out what the instruction means. This includes what process needs to take place (addition, subtraction, multiplication, data movement, etc.) and what memory locations are going to be involved.

Then the computer uses the data bus to fetch the memory locations to be used in the calculation. The data bus is the actual wire that connects the CPU and memory. In addition to the memory on the outside of the processor, the processor itself has special, high-speed memory locations called registers— general registers and special-purpose registers.

General-purpose registers are where the main action happens. Addition, subtraction, multiplication, comparisons, and other operations generally use general-purpose registers for processing. However, most information is stored in the main memory, brought in to the registers for processing, and then put back into memory when the processing is completed. Special-purpose registers have very specific purposes regarding controlling or monitoring various aspects of the processor’s function.

After the CPU retrieves all of its needed data, it passes the data and decoded instruction to the arithmetic and logic unit. This is where the instruction is actually executed. After the results of the computation have been calculated, the results are sent through the data bus to the appropriate location in memory or in a register, as specified by the instruction.

Everything described above happens fast. Really fast. The speed at which the CPU can carry out instructions is called the clock speed. This is controlled by a clock. With every tick of the clock, the CPU fetches and executes one instruction. The clock speed is measured in cycles per second, and one cycle per second is known as 1 hertz. This means that a CPU with a clock speed of 2 gigahertz (GHz) can carry out two thousand million (or two billion) cycles per second.

Obviously, this is a very simplified explanation. Although the basic operation is the same, it’s complicated by the use of cache hierarchies, superscalar processors, pipelining, branch prediction, out-of-order execution, microcode translation, coprocessors, and other optimizations.

--

--