How A Computer Works

Melissa Mullen
simple CS
Published in
8 min readAug 29, 2020

--

The Hardware

Computers aren’t magic. They work because engineers of the 1900s exploited a basic physical property of the universe: electrons can move freely in metals, generating an electric current. They defined a simple alphabet with only two “letters” : 0 and 1. If no current was flowing, they would call that 0. If current was flowing, they would call that 1. This simple alphabet would become known as binary. To this day, every aspect of your computer is controlled using this alphabet, which simply indicates if trillions of tiny currents in your computer are flowing or not. These tiny currents flow through electronic devices called transistors (made from silicon-based metal-oxide-semiconductor material, often referred to as MOS technology, for all you chemists and physicists out there). The transistors are connected to each other on integrated circuits, or chips. Today, there are trillions of transistors on a single chip, and tens to hundreds of chips within a single computer.

In addition to defining the binary alphabet, engineers were also tasked with using it in an intelligent way to solve problems. They split up the problem solving steps and created hardware to handle each step. (Hardware is any physical part of a computer.)

  1. Input Device (e.g. keyboard) : receives problem (instruction) from user
  2. Memory : stores data and instructions
  3. Arithmetic Logic Unit (ALU) : performs mathematical operations on data
  4. Central Processing Unit (CPU) : tells ALU what mathematical operations to perform on data
  5. Output Device (e.g. screen) : displays solution to user

Each piece of hardware both operates and communicates with other components using binary. We will examine the most relevant components to understanding computer functionality: the CPU and Main Memory.

The Central Processing Unit (CPU)

The most complex of these components is the CPU, also called the processor. The processor is the “brain” of the computer. It is an integrated circuit in charge of the following tasks.

  • Incrementing the program counter, which is a special register that keeps track of which location in memory the next instruction should be read from. Registers are small pieces of quickly accessible memory located within the CPU.
  • Reading the instruction from the memory location indicated by the program counter and reading the data from memory required by the instruction.
  • Decoding the instruction into a set of commands for other hardware components. To perform the decoding, the CPU uses an Instruction Set Architecture (ISA) which must be defined specifically for each CPU model.
  • Providing the ALU with the necessary data and commands, and writing its output to a register.

Memory

Memory is also an integrated circuit. However, instead of using binary to facilitate tasking for other hardware components like the CPU, main memory uses binary to store data. It can be understood as an array of cells, each with a unique address, and each of which can hold one word. A word is a collection of a particular number of bits (0’s and 1’s). This number is known as the word width. Words widths on most systems are multiples of 8 bits, with modern word widths equal to either 32 or 64 bits. If the data or instruction that must be stored is larger than the word width, multiple adjacent memory cells can be used.

A computer’s main memory.

As an aside, it is important to understand that any information can be represented in binary. This is done by assigning each letter of the alphabet and character on the keyboard a corresponding binary number. That is exactly what ASCII is — a character encoding standard. Any character can be looked up in the ASCII table and is represented by a unique binary number. For example, capital F in binary is equal to 01000110.

Memory can be either volatile or non-volatile. Volatile memory requires power to store information. This means that once a computer is powered off, any volatile memory is lost. Main memory (the memory we refer to when we say the CPU “reads data from memory”), also called Random Access Memory (RAM), is volatile.

The memory hierarchy.

Computers also contain non-volatile memory, which does not require power to store information. A Hard Disk Drive (HDD) is the non-volatile data storage device that stores the files on your computer. It is often referred to as the disk. It takes more time for the CPU to access data from the disk than it does for the CPU to access data from RAM, or from the registers within the CPU. The disk is a magnetic storage device (unlike RAM, which uses MOS technology to store data).

The Software

Software is the collection of instructions and data (written in binary) that tell the computer (more specifically, the CPU) what to do. Software can be divided into two categories: system software and application software. System software is software designed to be a platform for application software. It is responsible for facilitating communication between device drivers and application software. Device drivers are software that allow a computer to give an external piece of hardware (printers, external hard drives, etc.) commands without needing to know precise details about it. Application software is software designed for humans. This includes Google Chrome, Microsoft Word, and even the terminal.

The Operating System (OS)

The Operating System is system software. It is the key to our ability as humans to communicate with a machine. It is the final link between difficult-to-understand computer language and actual information and visual cues that make sense to average, non-genius humans.

The kernel is the main computer program (a set of instructions for the CPU) in an operating system. It is the “brain” of the OS. The kernel is one of the first programs loaded into main memory when a computer boots, and it stays it memory for the duration of use, until the computer is powered down. It is in charge of the following tasks.

  • Translating and forwarding commands from peripherals (Input/Output devices) to device drivers.
  • Creating processes. A process is an instance of a running computer program. The kernel ensures that processes have seemly exclusive use of the CPU and main memory via context switching and virtual memory. Context switching is the saving of the current state of the process (set of instructions, value of the program counter) in memory so that processes can take turns using the CPU (since CPUs are only capable of running one set of instructions at a time). It’s like pausing a TV show on one channel, switching channels to watch another show, then switching back to finish the original TV show (where you are the kernel and the channels are the processes).
Virtual memory mapping to physical memory via the MMU.

Virtual memory is the mapping of virtual memory addresses to physical memory addresses. Virtual addresses are a set of numbers, just like physical addresses, except that they do not refer to a specific physical cell. Rather, they refer to a physical address (the number). A piece of hardware called the Memory Mapping Unit (MMU) located in the CPU is responsible for translating virtual addresses into physical addresses.

This means that when the OS launches a process and prompts the CPU to retrieve data from a specific address in memory, that address is a virtual memory address. The MMU then translates that virtual address into a physical address so that the CPU knows what memory cell the data is physically located in. But why are virtual addresses and the MMU necessary? Aren’t they just adding an unnecessary middleman? Yes, if the MMU only mapped to physical addresses in RAM. However, the MMU also maps to physical addresses on the disk. This gives the CPU the illusion of having more RAM. The CPU is, in effect, using RAM as a cache, storing as much data there as possible, and storing any overflow data on the disk. This is because RAM is physically closer to the CPU than the disk, so data stored on it can be accessed more quickly. So why not just make RAM storage bigger? RAM is more expensive than disk storage. Building a computer is a balancing act, where both efficiency and affordability must be optimized.

  • Listen for system calls from processes. System calls are the interface between processes and the operating system. Processes can invoke them to request services from the kernel. The request is in machine code, instructions the CPU understands. Note: Although it is possible to write machine code, most engineers use assembly code to give instructions to an assembler, which is a program that translates instructions into machine code (also called “object code”). Even more engineers use the language higher level languages like C, which a compiler program can translate into assembly code.

Clearly, the kernel is an important component of the operating system, with lots of responsibilities. But there are more components with equally important responsibilities, albeit fewer. Other components include:

  • Networking
  • Security
  • User Interface

Networking is the transmission of information from one computer to another. This topic deserves it’s own article! For now, just know that the operating system handles communication with any hardware external to the computer.

Security is the protection of the computer system from theft or damage. The operating system is capable of distinguishing between “privileged” and “non-privileged” users via authentication, where the user provides a username and password.

The User Interface (UI) (in the context of an operating system) is the terminal. The terminal allows humans to communicate directly with the kernel via commands. Every operating system has its own set of (bash) commands. The most common bash language is Linux bash, written for the Linux OS. The Mac OS is Linux-based, which is why Linux bash commands word on a Mac OS command line. The Windows OS has its own set of bash commands, which are less widely used.

Final Thoughts

There is a lot of information packed into this article. And yet, I actually left out a lot of the details! Computers are extremely complex, but it is possible to understand how they work. This article is an attempt to help you do that, on a high level.

--

--