How Your Computer Works
Tags: Introduction, Computers, Computer Science

1. 1s and 0s

A computer is just like any other machine: a bunch of cleverly placed parts that, because of their placement, happen to perform an interesting task. For example, a car is technically a large hunk of metal, but because it is thoughtfully put together, it’s able to do more than just sit around and look pretty. Likewise, computers are a bunch of metal wires connected to a small piece of silicon, which is really another bunch of small wires. Once a computer is plugged in, the electricity that flows through these wires can be controlled in a way that does some interesting things. In computing disciplines, this controlled flow of electricity is represented with 1s and 0s, which represent the presence or absence of electric charge, respectively. Everything a computer does is founded on this single idea; numbers are represented in the binary counting system (which only needs two symbols, 1 and 0, to represent any number), and those numbers are used to represent everything from the text on this screen to the colors of the pixels that are displaying it.

2. CPU

The main component of a computer that “does things” with 1s and 0s is the Central Processing Unit (CPU). The CPU is capable of performing a limited number of simple instructions. Each instruction has a unique binary number that the CPU’s creator assigned to it in a document called an Instruction Set Architecture (ISA). When a CPU loads a certain binary instruction, the wires inside the CPU are designed to execute the corresponding task. As far as we are concerned, it’s not too important how these instructions get assigned to binary numbers; as long as we can all agree on a single, unique number for every instruction, things work out fine.

When your computer starts receiving power, the CPU starts running a very, very long list of these instructions, beginning from a predetermined starting place. The CPU continues to work its way through this list until it stops receiving power. You might be wondering if it’s possible to keep your computer on long enough for the CPU to reach the end of the instruction list. The answer is no, and here’s why:

CPU instructions can be broadly organized into three distinct categories:

1) Adding, subtracting, multiplying, and dividing binary numbers. (Arithmetic Operations)

2) Switching to an instruction that is not the very next one. (Control Flow)

3) Reading or writing binary numbers from a memory device. (Input/Output)

The instructions in category 2, control flow, allow us to change where the CPU looks for instructions in such a way that it runs through the instruction list indefinitely. For example, one way you could do this is add a single instruction at the very end of the list that tells the CPU to start over from the beginning of the list. This creates a very simple loop, which the CPU can run over and over again to infinity.

Another thing you might be thinking is: “given that a computer can access the internet and do calculus, the instructions you just listed sound too simple”. But consider this: the CPU is running billions of these instructions per second. For example, a modern phone processor is so fast, it’s able to calculate the individual color of every pixel on a 4K screen around 50–100 times per second, send those colors to the screen, and find the spare time to continuously figure out the physics of a game like Angry Birds. As for how adding numbers translates to complicated things like physics simulations, well, after some clever engineers got wires to do a couple basic things like add numbers, a lot more clever engineers devised ways to take those basic operations and get them to do more interesting things. If you want to learn more about how these crazy things are accomplished, consider studying computer science :).

3. Memory

A CPU has a limited number of wires designed to store numbers. A group of wires that store numbers are often called a “memory device” , but in the CPU, they have a special name: “register file”. The CPU uses the register file to keep track of binary numbers that the CPU needs in order to execute its instructions. However, this register file isn’t nearly large enough to store entire programs (read: very, very, very long lists of instructions). Rather, the register file is made to behave a lot like your refrigerator: it keeps a limited amount of important stuff close at hand.

So while the CPU is very good at calculating things, for various reasons it is designed to be not-so-great at remembering them. Instead, your computer stores programs and their data in a separate memory device, commonly known as DRAM (many people just call this RAM). The CPU can then access DRAM through a series of connections separately known as the system bus, the memory bus, and the I/O bridge. The inner workings of these connections are rather detailed. It is enough to know that these connections allow the CPU to ask DRAM for binary numbers, and for DRAM to send those numbers to the CPU.

DRAM does have one tricky caveat, though: when the computer is turned off, the information stored in DRAM is erased. Memory devices that behave in this way are called volatile. Memory volatility is a problem, because we like the ability to turn off our computers without triggering a digital armageddon. In order to solve this problem, another memory device called a hard disk drive (HDD), which is non-volatile, is used for no-power safekeeping.

The fact that we have HDDs may cause you to wonder why we even bother using DRAM, as well. We use DRAM because HDDs are very, very slow and DRAM is comparatively fast. When your computer boots, DRAM acts like a middleman between the HDD and the CPU. Put another way, binary information typically flows from the HDD to DRAM, and then from DRAM to the CPU.

4. Booting

When the computer first begins receiving power, the CPU starts up and looks for its first instruction at the predetermined starting place. It then begins executing the instruction at that address. When the computer is first reset, rather than pointing to a location in DRAM, this special address points to a small memory chip that stores a program called BIOS (Basic Input Output System). BIOS is a small program designed to search for the next program, known as the bootstrapper, for the CPU to execute. It does this by searching the hard disk drive for eligible programs, asking the user to pick a contender, then loading the selected program into DRAM and showing the CPU the first instruction. Many computers only have one “eligible program” installed, so BIOS takes the liberty of making this decision for you.

The bootstrapper is essentially BIOS on steroids. BIOS was designed to load very small programs onto a computer’s DRAM. The bootstrapper, on the other hand, is designed to load the operating system (OS; think Windows or MacOS), a program that manages all the other programs you’ll end up running. After the bootstrapper is done loading the OS into memory, it tells the CPU to run the OS. After some housekeeping we won’t delve into, the OS will be up and running, and your computer will be ready to run its first “real” program.

5. Processes

When I first began learning about computers, I thought it was odd that a single processor that’s already occupied with the operating system could run another program, much less the many programs necessary to get to the login screen, at the same time. Everything from calculating pixel colors to managing components to receiving data over the internet to processing the data received over the internet to playing cat videos has to have its own, separate program, which needs the full “attention” of a CPU. Given this information, even fancy new processors that have 2,4 or even more CPUs seem suspect.

It turns out that the OS and computer hardware are able to work together to make the illusion that many programs are being executed at once. In reality, at any given point in time, the majority of programs aren’t getting executed at all. Instead, they are executed concurrently, meaning that they take turns using the processor. To illustrate, suppose that you are running Chrome and Spotify on your computer. Both Chrome and Spotify are treated by the OS as processes, which means that they are actually copies of the programs’ instruction code that the CPU has loaded into DRAM from the HDD. The OS chooses one of these processes to execute, and sends its starting instruction address to the CPU. Every few milliseconds, a hardware (read: physically built-in) timer sends the CPU a signal that time is up for that process. The CPU then gives control back to the OS (whose address is tucked away under a digital pillow). The OS then rinses and repeats. Because the processor is so fast and the delay between execution periods so small, this ends up appearing to you, the user, as if both Spotify and Chrome are working at the same time, even though they really aren’t.

6. Virtual Memory

Programs don’t just share the same CPU, either. They also share the same memory. This means that the OS has to deal with the tricky business of setting up a single DRAM device so that every process is able to store its data and code on the system. As with CPUs, the hardware and software closely cooperate to make the illusion that each program has complete control over the computer’s memory unit. This abstraction is called Virtual Memory (VM). It’s called virtual memory because a program may “think” it is accessing a certain part of the computer’s memory, but really the data at that address could be just about anywhere in DRAM, the disk drive, or even nowhere at all.

In the CPU, there is a special module called the Memory Management Unit (MMU). Every time the CPU executes a memory read or write instruction, the address of that load instruction is sent to this unit. Inside the MMU, the address is split up in two parts. The first part references a “page” of memory, and is called the Virtual Page Index (VPI). “Pages” are just equal-sized chunks of a memory device that computer architects decided to use to make the computer easier to organize. The second part of the address is called the Virtual Page Offset (VPO). The computer uses this part to figure out how far into a specified “chunk” the requested data is. Because DRAM and the hard disk drive are cut up into these chunks, processes that are just getting started by the OS are first cut up into chuck-sized pieces. The starting addresses of each of these pages are then loaded into a special register file called a Page Table (PT). The MMU uses the PT to translate the VPI into an actual address that contains the data. Whew, that was a mouthful. Each time the OS switches processes, it switches the page table that the MMU uses so it can correctly translate the virtual addresses of that specific process. That way, every process can think it has full control of the computer, and the computer can happily manage memory for several running processes at once.

7. Conclusion

That’s it! There is a lot of additional circuitry and algorithmic thinking that has gone into making all of these parts work together, and it is pretty messy! But the main idea behind all this computer stuff remains the same, and the knowledge that’s been outlined in this paper will likely apply to your personal devices for the next while. In the foreseeable future, it is also plausible that your personal device will become a quantum computer, or a bunch of very strange wires that can represent 0s, 1s, and 01s. But more on that at another time…