Skip to content

Instantly share code, notes, and snippets.

@trietptm
Forked from revmischa/how_computers_work.txt
Created December 8, 2019 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trietptm/2b87857f4bc3df98aa4aff5595abd2ac to your computer and use it in GitHub Desktop.
Save trietptm/2b87857f4bc3df98aa4aff5595abd2ac to your computer and use it in GitHub Desktop.
How computers work
How computers work
==================
Most people on the planet directly interact with computers on a daily
basis. They may be fooling around on their cell phone, writing a paper
for their homework, or playing video games. Despite the ubiquity
of digital computing devices, there is an astonishingly small number of
people who are familiar with the inner workings of these devices. Not
only that, but due to the fact most people think of computers in
extremely high-level terms (such as providing email functionality, or
viewing youtube), they are oblivious to the many layers of abstraction
involved and are not even aware of how much they don't know about what
they don't know.
I'm writing this document as a guide for everyone regardless of
technical background. I think it's in everyone's best interest to
increase understanding of the complex system known as a modern
personal computer, and it's in my personal interest as a software
developer to give everyone better insight into the fundamentals of
computer systems. All too often project managers have little idea of
engineering tasks they give their developers. It's hard to imagine
CEOs and managers of other engineering-heavy industries being as
oblivious to the basics of the technology that run their business, but
for some reason it's accepted in the software industry. Imagine if the
executives at a washing-machine design company didn't have the first
idea about the design of washing machines, electric motors or water
pumps, yet it's commonplace for the higher-ups at IT-based companies
to be depressingly uninformed about the core engine driving their
business. Part of this is probably due to a lack of helpful documents
that explain how computers work today from top to bottom in layman's
terms, which is what inspired me to write this.
Let's get started with the physical computer itself: The actual piece
of circuitry that does the computing is called the CPU, for central
processing unit. This is a device with a small set of instructions
that it knows how to perform, and it is the brain of the
machine. However, like a brain floating in a jar, a CPU is pretty
useless if it's not hooked up to any peripherals. Because of this
simple fact, the CPU will always come on a "motherboard", which is a
circuit board wiring the pins of the CPU to power and devices it can
perform input and output with, such as a keyboard, hard drive or
computer display.
The CPU itself is made up of staggering number of transistors, which
are the basic building blocks of digital logic. A transistor is a
simple piece of electronics which has two pins used to connect a circuit
and a third pin which controls if the circuit is open or closed. If
current is applied to the third pin then electricity will flow between
the other two pins, otherwise the circuit will be broken, enabling the
transistor to act as a simple on/off switch. By combining transistors
in clever ways, one can construct slightly more complex circuits which
correspond to basic logical operations, known as "AND", "OR", "XOR",
"NOT" and others. Almost all of the circuitry comprised of the
hundreds of billions of transistors in a CPU is built from these
simple logic gates.
The gates are hooked up into larger building blocks, such as adders or
multipliers for performing arithmetic, or sending and receiving
impulses from peripheral devices, or for storing and retrieving data
from random access memory (RAM) or local CPU storage
(registers). These components are controlled through a series of basic
instructions that a CPU understands, known as its instruction set. The
most common PC instruction set is known as the x86 (the "x" standing
for 186, 286, 386, and so on, which are models of Intel CPUs)
instruction set. It has functions like add, multiply, divide, load/store
data in RAM, compare two numbers, change the execution flow of a program,
basic logical operations (AND, OR, etc), send and receive signals on the
pins on the chip, and that is more or less it. Basically everything else
you would ever want to do can be implemented by means of these primitive
constructs.
When the CPU processes numbers, it can store them in a limited number
of local CPU registers, which are just fancy circuits capable of
holding a number of on/off positions which are interpreted as numbers
encoded in a base-2 numbering system (binary). Access to these is very
fast, so they're used for storing intermediary results of calculations
and such, but their capacity is very limited. RAM is used for storing
larger sets of data, and the hard disk is used when you would like to
have access to the data after the computer has been powered off and
back on again because the data doesn't vanish when it loses power.
When a CPU is running, it operates in a very simple loop which is as
follows:
- First, load the next instruction to execute from RAM. Just how does
it know where to look? It stores the current memory address in a
special register known as the Program Counter (PC), which is just a
big number that points to a location in RAM.
- After loading the instruction, the CPU decodes the instruction
(a sequence of numbers) to figure out what operation it should
perform. The operation code (opcode) definitions vary from
architecture to architecture, but they correspond to the
instruction set of the CPU executing the instructions.
- The CPU executes the opcode, and then advances the PC to point at
the next instruction, unless the opcode directs it to jump to
another instruction somewhere else, which it does by simply
modifying the contents of the PC.
- The loop is repeated.
This loop goes on usually a few billion times a second, and a number
of operations are performed simultaneously in stages to
execute several operations in parallel on a single CPU.
Modern chips may have several CPU cores on a single die, allowing the
computer to execute more than one distinct task at a time.
The CPU is not usually wired directly to peripheral i/o devices
but is connected to a Basic Input/Output System (BIOS), which is a
handy chip found on old motherboards that acts as a middleman between
things like disk access or keyboard input and the CPU. The presence of
a BIOS and a standard interface that is roughly the same across PC
BIOSes makes it much easier for the CPU to interact (in a rudimentary
fashion) with other components attached to the motherboard.
Some standard BIOS features on intel-compatible motherboards are
commands such as "read some bytes from the hard disk", "tell me what
keys the user pressed", or "display some text on the screen".
In modern computer systems, the BIOS is done away with in favor of
a new standard known as the Extensible Firmware Interface (EFI).
BIOS and EFI serve the same purpose however; they provide a standardized
mechanism for the system software to communicate with the hardware components
in the system, in order to do a handoff from lower-level stuff to a bootloader,
which is a specialized piece of software that contains instructions on how to
start your operating system.
When you press the power button on your computer, here's what happens
on a standard intel-based BIOS PC:
- The BIOS loads persistent settings such as what devices you would
like to boot from and the current date and time.
- The BIOS performs a Power-On Self-Test (POST), to make sure
everything is functioning as expected. If the POST fails, your
computer will make a series of annoying beeps and maybe flash some
lights.
- The BIOS scans your peripherals, and finds one to attempt to boot
up from.
- Once a suitable boot device is found, the BIOS loads the first 512
bytes of data on the drive, and looks for a marker signifying that
the 512 bytes contain code for a bootloader. If it is found, the
BIOS loads the bootloader into RAM and sets the PC to the first
instruction of the bootloader.
(I don't know what the procedure is for EFI, maybe someone more knowledgable
can describe that here)
After this process, the CPU is now executing the bootloader software
contained on your peripheral storage device. This software knows how
to find and load your operating system, which then takes over and does
the rest.
Most people rarely if ever interact with their bootloader, but it's a
very important little piece of software that makes your computer able
to load and run software of any size and capability. Without it, your
computer is a boring paperweight.
The operating system is what provides a common framework of handy
services and abstractions to software developers. It was quickly
learned that asking every programmer who wanted to write software to
learn how to interact with the myriad incompatible peripherals, each
with its own proprietary interface was unreasonable and undesirable,
so the OS does all the hard work of interacting with the BIOS, CPU,
peripherals (through device drivers) and doling out segments of RAM
and resources to applications. This frees up the programmer to
concentrate on writing cool software to let you look at pictures of
cats on the internet.
I hope that was informative. For more articles and resources, please
check out: https://github.com/revmischa/learn-software-engineering/wiki/Background
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment