Hardware

How Does a Computer Work? Hardware and Software Explained

A computer works by repeatedly fetching an instruction from memory, executing it on the processor, and storing the result, billions of times per second. Hardware does that work, and software (firmware, the operating system and your apps) supplies the instructions and decides which ones run.

The sections below follow the path from pressing the power button to a program running on screen. The details differ between a laptop, a phone and a server, but the structure is the same.

What are the main parts of a computer?

Every general-purpose computer has the same four kinds of hardware:

PartJobKeeps data without power?
CPU (processor)Executes instructions, one small step at a time, on several cores in parallelNo
RAM (memory)Holds the programs and data that are in use right nowNo
Storage (SSD, hard drive)Holds the operating system, apps and your files long termYes
Input and output devicesKeyboard, mouse, display, speakers, network adapter, and so onNot applicable

A motherboard connects them, and a power supply feeds them. A graphics processor (GPU) is a second kind of processor, specialised for doing the same calculation on many pieces of data at once. It is a separate chip on a graphics card, or built into the CPU package on many laptops.

To see what your own machine contains, follow how to check PC specs on Windows.

What does the CPU actually do?

The CPU does one thing: it executes instructions. An instruction is a very small operation, such as adding two numbers, copying a value from one place to another, comparing two values, or jumping to a different instruction. Everything a computer appears to do, from showing a web page to playing a video, is built out of long sequences of these small operations.

The cycle is the same for each one. The CPU fetches the next instruction from memory, decodes it to work out what it asks for, executes it, and writes back the result. Then it moves on to the next instruction. The processor's clock sets the pace, and a modern CPU completes this cycle for many instructions every nanosecond across its cores. A core is one independent processing unit, and a CPU with several cores can run several streams of instructions at the same time. Check how many cores you have if you want to know your own count.

The circuits inside work with two electrical states, written as 0 and 1. Instructions, numbers, text and images are all stored as patterns of these bits. Nothing in the hardware knows what a photo or a spreadsheet is. The meaning comes from the software that reads the bits.

Why does a computer need both RAM and storage?

Storage is large and keeps its contents when the power is off, but it is far slower to reach than memory. RAM is the reverse: the CPU can reach it quickly, but it is volatile, so everything in it disappears at shutdown. A program therefore lives on storage, is copied into RAM when you start it, and runs from there.

That is also why unsaved work vanishes in a crash. A document you have not saved exists only in RAM. Saving writes it to storage.

The operating system hides most of this from programs. Every memory access a program makes uses a virtual address, and the CPU translates it to a physical address using page tables that the operating system's kernel manages. The Linux kernel documentation describes the result as virtual memory abstracting "the details of physical memory from the application software". One practical effect is that two programs can use the same virtual address without touching each other's data.

The same documentation describes a second speed-up. When a file is read, the kernel keeps its data in a page cache in RAM "to avoid expensive disk access on the subsequent reads". Pages that have been changed are written back to the storage device later. The CPU also keeps a small cache of recent address translations, called the Translation Lookaside Buffer, for the same reason: memory is slow compared with the processor, so repeating work is expensive. The Linux page describes Linux specifically. Windows and macOS use the same general ideas with their own implementations.

What happens when you press the power button?

A computer cannot load an operating system from storage until something has set up the hardware enough to read it. That something is firmware, software stored on a chip on the motherboard. It runs before anything else.

Microsoft's boot documentation gives the sequence for a Windows device: firmware initializes the hardware, then hands control to a UEFI (Unified Extensible Firmware Interface) environment, which launches the Windows Boot Manager. The Boot Manager reads its boot configuration to decide what to run and then starts the main operating system. The page covers Windows 10 devices and notes that parts of it apply only to certain hardware, so treat it as an outline of the pattern rather than a description of every PC. Other operating systems use a different boot loader after the firmware but follow the same order.

  1. Power on. The power supply stabilises its voltages and the CPU starts executing firmware from a fixed location.
  2. Firmware. The firmware initialises the hardware and finds a bootable drive.
  3. Boot loader. A small program on that drive loads the operating system's kernel into RAM.
  4. Operating system. The kernel starts, loads drivers for the hardware, and starts background services and the login screen.

If a machine fails at one of these stages, the symptom tells you which. Nothing at all points to power; a firmware screen but no operating system points to storage or boot settings. The article on a computer that won't turn on walks through the first stage.

What does the operating system do?

The operating system sits between programs and hardware. Three of its jobs explain most of what you see on screen.

  • Running several programs at once. Microsoft defines a process as "an executing program" and a thread as "the basic unit to which the operating system allocates processor time". The scheduler gives each runnable thread a turn on a core, switching so quickly that dozens of programs appear to run together, even on a CPU with few cores.
  • Managing memory. It gives each process its own virtual address space and decides which pages sit in RAM, as described above.
  • Talking to devices. Drivers translate between the operating system's generic requests ("read this file", "draw this window") and the specific commands each drive, display or network card understands. Programs never talk to the hardware directly, which is why an app written once runs on thousands of different machines.

The operating system also enforces limits. One program cannot read another's memory or write straight to a drive unless the kernel allows it.

How does a program you click on actually run?

Take opening a text editor as an example. You double-click its icon, and the operating system follows these steps:

  1. It finds the program's file on storage.
  2. It creates a new process and gives it a virtual address space.
  3. It copies the program's code and starting data into RAM.
  4. It starts a thread, and the scheduler gives that thread time on a CPU core.
  5. The CPU executes the program's instructions. When the program needs something from the system, such as opening a file or drawing a window, it asks the operating system, which uses the right driver to talk to the hardware.

Pressing a key follows the reverse path. The keyboard signals the CPU, the operating system's driver reads the key, and the system passes it to the program that has focus. The program updates its text, asks the system to redraw, and the graphics hardware produces the new image for the display.

Where does software come from?

People write software in programming languages that humans can read. A compiler translates the source code into the processor's instructions ahead of time, or an interpreter translates it while the program runs. Either way, the CPU only ever executes its own instruction set. This is why a program built for one type of processor, such as x86-64, does not run on another, such as ARM, unless it is rebuilt or translated.

The layers stack like this, from the bottom up: hardware, firmware, the operating system, then your apps. Each layer uses only the one below it, which lets engineers change one layer (a new graphics card, a new version of an app) without rewriting the others.

If you want to see it for yourself

The Task Manager on Windows (Ctrl+Shift+Esc) and Activity Monitor on macOS show the ideas above live: the list of processes, how much RAM each uses, and how busy the CPU is. Open one, start a large program, and watch memory use rise as its code and data are loaded into RAM.

Frequently asked questions

What are the main parts of a computer?
The main parts are the processor (CPU), which executes instructions; memory (RAM), which holds the programs and data in use right now; storage (SSD or hard drive), which keeps files when the power is off; and input and output devices such as a keyboard, display and network adapter. Firmware and an operating system sit on top of this hardware and make it usable.
What is the difference between hardware and software?
Hardware is the physical equipment: the processor, memory chips, drives, circuit boards and cables. Software is the instructions and data that the hardware runs, such as the operating system, apps and the files they open. Software cannot do anything without hardware to run on, and hardware does nothing useful without software.
Does a computer only understand 0s and 1s?
Yes, in the sense that the processor's circuits work with two voltage states, written as 0 and 1. Programs are stored as patterns of those bits. Programmers rarely write them directly: they write in a programming language, and a compiler or interpreter translates it into instructions the processor can execute.
Why does a computer lose unsaved work when the power goes off?
RAM is volatile memory, so it only keeps its contents while powered. An open document that has not been saved exists only in RAM. Saving writes it to storage, which keeps its contents without power.

Sources