How Computers Work
There’s a reason why computers are so popular — the things we can do with them are cool. We can talk to our friends, read the news, watch funny videos, play games, shop for clothes, pay our bills, etc. It’s amazing that one machine can do so many useful things. It’s like the ultimate Swiss army knife.
My goal in this article is to give you a solid grasp of how these machines work. I’m going to try to do this in three stages:
- First I’m going to say that computers are analogous to a dumb intern. This analogy will give you a pretty low resolution image, but it will be enough to provide you with the big picture in a few minutes.
- Then I’m going to ask you to forget all of that and replace the intern analogy with a new one: a filing system. It will take a bit longer to describe computers using this analogy — maybe 15–30 minutes — but instead of a low resolution image, you’ll have a medium resolution one.
- Finally, I’m going to describe how computers really work. This will probably take 15–30 minutes to read, but a few hours to grasp.
The point of this approach is to constantly be focusing on the big picture. If I started off with high resolution pixels, it’d take a while before you have enough “jigsaw pieces” such that the big picture shines through:
I think that it’s much easier to work on a high resolution jigsaw puzzle once you’ve already completed a low resolution version of the same puzzle.
Alice: The computer must be very smart if it’s capable of doing such complicated things for us.
Bob: Actually no, computers are very stupid.
Alice: Huh? If they’re stupid, how could they do difficult and complicated things?
Bob: A computer is like a dumb intern — if you want it to do something useful, you have to take the time to train it. You have to give it very simple and thorough instructions. But once you train it, you can just kick back and let it do the tedious work for you.
Alice: Computers need to be trained? They need instructions? I know I’ve never really given my computer any instructions, other than clicking my mouse and typing on my keyboard.
Bob: Imagine that marketing director Megan trained intern Ivy on how to organize papers. Now, since Ivy has been trained by Megan, you can go to Ivy and have her organize your papers.
Similarly, software developers train your computer to do things for you. Once your computer is trained, you just have to use your mouse and keyboard to tell it what to do.
Note: programming = writing out instructions for computers.
Alice: What kinds of things are computers capable of doing?
Bob: Only very simple ones. They can’t print the word “adam” to the screen. They can’t even print the letter “a”. All they could do is turn pixels on and off.
Alice: How is a programmer supposed to sit there and write out such tedious, pixel-by-pixel instructions? I imagine that it’d take forever.
Bob: Because you can say things like: follow steps 22–734 to print the letter “a”. This way, you don’t have to always be “thinking in pixels”.
Alice: But if they have to go pixel-by-pixel, how am I able to watch videos at reasonable speeds?
Bob: Because computers are INSANELY fast. They also never rest, never disobey you, and never make mistakes. They may be stupid, but they’ve got some good qualities too.
Alice: I suppose that makes sense. This explains how you could get computers to display things for you. But how could you get them to think?
Bob: First, note that displaying things accounts for a lot of what computers are used for. YouTube, Instagram, email, social media, blogs, news, shopping, banking — they’re all mostly “dumb” tasks that just involve displaying things to you.
As for “smart” tasks, consider poker as an example. You could train a computer to play poker according to a series of instructions. For example:
"If you’re in middle position and are not facing a raise before the flop, fold with these hands, call with these, and raise with these."
Consider Amazon’s recommendations as another example — you could write out instructions like:
"When the user views 5 books from a particular author, recommend that they read the given author’s most popular 2 books."
All complicated tasks can be broken down into a series of simple instructions.
The takeaway of the Alice-Bob conversation was that a computer is like a dumb intern who you could train to do your tasks for you. It’s really stupid, so you’ll have to break things down and make your instructions very simple and thorough. But once you do, you can just kick back and let it do your work for you.
That analogy gave you a sense of the big picture in a short amount of time, but it also was pretty low resolution. Now let’s replace this low resolution picture with a medium resolution one. Let’s replace the intern analogy with a new analogy. Let’s compare a computer to a filing system. It may seem odd, but as you’ll soon see, the analogy is extremely fitting.
Suppose you’re the CEO of Widget Corp. It’s time for you to calculate your monthly expenses:
Office space: $30,000
Uh oh, you don’t know how much you need to pay your salesmen. Let’s figure out how much it is so you could finish your balance sheet.
Since you’re old school, you keep track of everything in your company with a filing system like this one:
There is a particular drawer that has all of the salesman cards. They look like this:
You have a filing clerk who works for you named Timmy. You can have him figure out how much commission you need to pay out. Here’s how Timmy would go about doing that:
Let me open up this drawer of all the salesman cards.
First up: John Doe.
- John did $25,000 in sales this month.
- Since his commission rate is 20%, he gets paid $5,000 this month.
Next up: Sally Jones.
- Sally did $30,000 in sales this month.
- Since her commission rate is 20%, she gets paid $6,000 this month.
- So far that’s $11,000 total that we need to pay out to our salesmen. Let me write that down on another card so I could keep track of it.
Ok, I’ve gone through all the cards and kept track of the running sum on my
totalcard. It says that we need to pay out $125,000 in commission.
Timmy didn’t know how to do this when you first hired him though. You had to tell him:
- What drawer the salesman cards are in.
- To multiply gross sales and commission rate to get the commission that needs to be paid out to that salesman.
- To use a separate card to keep track of the running total.
Before hiring him, Timmy knew how to read and write. And he knew basic arithmetic. But that’s really it. You had to train him to perform the tasks of interest to your company.
A computer is analogous to the filing system that I just described:
- The slots of memory in a computer (RAM) are analogous to Widget Corp’s drawers. The cards in the drawers are analogous to the data in the computers memory.
- The CPU of a computer is analogous to the filing clerk. It can do three things: 1) read what’s on a card, 2) perform a computation on what it reads, and 3) write something down on a card. That’s it. Keeping this in mind, you can train it to do useful things for you, like calculating total commission that needs to be paid out.
In the Widget Corp. analogy, we could give our filing clerk oral instructions. We can speak to him. And he could remember.
But we can’t speak to the “filing clerk” in computers. And it can’t remember. The only thing this “filing clerk” can do is read from cards, so we’ll have to write down the instructions for him on cards.
It would look something like this:
Ok Timmy, look at drawer #458 over here. Each card has an instruction for you. Just go through them one by one.
I’ll help you get started. We’ll do the first few together:
- The first instruction says to go to drawer #3326 (salesman drawer) and take out the card for salesman #1.
- The second instruction says to look up the amount of sales he/she did this month. For example, salesman #1, John Doe, did $25,000 in sales this month.
- The third instruction says to look up his/her commission rate. For example, John Doe’s commission rate is 20%.
- The fourth instruction says to multiply the amount of sales by the commission rate. So you’d multiply $25,000 by 20% to get $5,000.
- The fifth instruction says to write $5,000 down on a different card (the one that tracks the running total).
- The sixth instruction says to go to drawer #3326 (salesman drawer) and take out the card for salesman #2.
So on and so forth. Again, I wrote it all out for you in drawer #458 (instruction drawer). All you need to do is follow the instructions.
Does this make sense? Can I go meet my buddies at the golf course now?
There are actually different types of memory in a computer:
- The RAM is a type of memory that is analogous to the drawers in the filing system.
- Registers are another type of memory which would be analogous to the filing clerk keeping a few cards in his pockets.
- The hard drive is a type of memory that is actually a peripheral device. It’s analogous to a Widget Corp warehouse that holds rarely used cards.
As you could infer from the analogy, it’s a lot faster to take a card out of your pocket then it is to walk around Widget Corp opening up drawers. And it’s a lot faster to walk around Widget Corp than it is to drive over to your warehouse to get a card. So why don’t we just give the filing clerk a lot of pockets? Why don’t we use registers instead of RAM and hard drives?
Basically, because it’s expensive. Pockets are expensive. Also, there’s only so much room on the filing clerk’s clothing for pockets.
The same can be said for Widget Corp vs. a warehouse. With computer memory, there’s always a trade off between cost and speed.
So far we’ve seen that we can add up the amount of commission we have to pay out to the salesmen. In the Widget Corp analogy, the CEO could just ask the file clerk what that amount is. But in real computers, the “file clerk” can’t talk; all he can do is read cards, write on cards and compute.
That’s no problem, we could just have the “file clerk” write down the answer on a card for us. But in the real world, “cards” are microscopic transistors, and we can’t open up the computer and look at these transistors to get the result of a computers calculation.
As users of computers, we know the output ends up being displayed to us on a screen. But how does that happen?
(If the fact that there are wires coming out of cards bothers you, remember, the cards are just a medium resolution analogy for transistors.)
So if you wanted to have “Total commission: $125,000” written to the screen, you’d need a final instruction in drawer #458 saying:
Use the cards in drawer #1005 (pixel drawer) to turn on the right pixels.
Input works similarly to output. As you’re holding down the “a” key on the keyboard, it triggers electricity to be sent through a wire that turns on Card #1 in drawer #1004 (keyboard drawer):
Now, if you want to write a program that uses input from the user, you can have instructions that say something like:
If the user presses “y” for “yes”, follow instructions 2004–2011. If the user presses “n” for “no”, follow instructions 2012–2019.
To determine whether the user is pressing “y”, go to drawer #1004 (keyboard drawer) and look at card #25. To determine whether the user is pressing “n”, go to drawer #1004 (keyboard drawer) and look at card #14.
We talked about the screen, and about the keyboard. Neither of these are part of the computer itself. They have wires that connect to the computer, but they’re still peripheral devices.
The idea with peripheral devices is always the same: to have some sort of mapping between the state of the cards in the filing system, and the state of the peripheral device. Because of this mapping:
- You could write on certain cards to get output peripheral devices to do things for you.
- You could read from certain cards to determine the state of input peripheral devices.
With the screen, card #1 in drawer #1005 (pixel drawer) corresponds to the pixel in the top-left corner of the screen:
With the keyboard, we said that when “a” is being pressed, it corresponds with card #1 in drawer #1004 (keyboard drawer) being on:
Consider some other examples:
With a mouse, there are sensors that detect the x-y position of the mouse (using a little ball, or a laser). The mouse then chooses which wire to send a signal through based on what this x-y position is.
With a speaker, suppose that when card #6002️ in drawer #1008 (speaker drawer) is on, a sound with a frequency of 6002Hz is played. This happens because the speakers detect the signal from the wire coming out of card #6002 and convert it into the corresponding sound.
To be honest, I’m not sure how these peripheral devices really work. Computer science people just take it for granted that:
- They could write on certain cards to get the output peripheral devices to do stuff.
- They could read from certain cards to determine the state of an input peripheral device.
They don’t worry about how it actually happens — there are other people who deal with that.
A stethoscope is a computer?
It’s important to understand that a computer doesn’t have to look like this:
Here are some other types of computers:
Ultimately, a computer just looks like this:
It’s the peripherals and the instructions you give it that make it unique.
If you want to see CNN.com, you can’t just look inside your own filing system. You have to go out to CNN.com’s filing system and get the cards from them.
Think about it — the news changes every day, so you need to go out and get the updated version of the news. Also, there are over 1 billion websites. You wouldn’t have enough space to store them all in your filing system, so it makes more sense to just go out and get the cards when you want to use them, and then discard them when you’re done.
Slow internet connection is like a slow messenger (well, it could be because the messenger is slow, or there’s a long line at the server, or various other things). If you have a slow messenger and a fast filing clerk, it won’t do you any good because the filing clerk would have to sit around waiting for the messenger all the time.
But ultimately, everything with computers is crazy fast nowadays. When you buy a laptop and it says the processor is 2.7 GHz, that means that the file clerk could execute 2.7 BILLION instructions each second!
And when you have internet connection that is 45.8 Mbps, that means that the messenger boy brings you 45.8 MILLION cards to process each second!
File clerks weren’t always so fast though. Back when computers were new, the file clerks were still pretty slow, so computers would only be used to do, say, tedious arithmetic for you. And you often had to wait a few minutes/hours/days for it to finish its calculation. You’d enter the data, turn on the computer, head home for the night, come back the next morning, and see the result of the calculation.
Anyway, speed kills. File clerks haven’t really gotten any smarter over the years, just a lot faster. Their speed is the reason why computers can do cool things in reasonable amounts of time.
Remember, the computer can only turn individual pixels on and off. It does this by using the cards in drawer #1005 (pixel drawer):
Suppose you want to print the letter “a” to the screen. You could have a series of instructions in drawer #74302 (“a” drawer) that describe how to do this. They’d be telling you to use drawer #1005 to turn certain pixels on and off.
Writing them out is tedious, but once you have these instructions in drawer #74302, printing the letter “a” becomes a solved problem. When you want to print the letter “a”, you just have to say:
Go to drawer #74302 and follow the instructions there.
This saves programmers a HUGE amount of time. Furthermore, it makes programming much more manageable, because you no longer need to “think in pixels”.
Notice that there are some programmers who have to “think in pixels” — someone had to be the one to put the right cards in drawer #74302. This type of programming is considered to be low level. A higher level programmer just trusts that the other guy did his job, and uses drawer #74302 to print the letter “a”.
This is called abstraction. With abstraction, you know what will happen. In this case, you know that following the directions in drawer #74302 will print the letter “a”. But you don’t know how it happens. You don’t know exactly what pixels need to be turned on and off. However, you don’t care. As long as the job gets done, the details of how it gets done don’t matter to you.
Notice that there can be levels of abstraction. For example, there could be a drawer that has the instructions for printing out the word “hello”, and this drawer could be using the lower level drawers that describe how to print each letter.
Abstraction is what allows us to successfully program computers. Without abstraction, it’d take too long, and it’d be too difficult for us human programmers to keep track of everything and figure out what the right instructions are.
Abstractions are also what allow CEOs in the real world to do their jobs. Think about it — if a CEO of a major corporation had to “think in pixels”, he wouldn’t be able to do his job. But because he has access to abstractions, he can instruct his mid-level managers to get tasks done, who can instruct their employees to get things done, who can think in pixels, and do small tasks, like printing the letter “a”.
Let me elaborate (using words and letters as an analogy):
- The low level employees would be the ones who know how to print letters. They “think in pixels”.
- The mid-level managers would be the ones who know how to print words. They’d do this by instructing the low level guys to print certain letters. They have to consider what letters need to be printed, and in what order. These mid-level managers “think in letters”.
- The CEO gets to “think in words”. He figures out what words he wants to print, and then instructs his mid-level managers to get them printed out.
Note that, just like in a real corporation, the low level guys only have to know how to print letters. They don’t have to have the contextual understanding of what words these letters are a part of. And similarly, the mid-level manager only has to know how to print words. They don’t have to have the contextual understanding of what essay these words are a part of.
Example program: Microsoft Word
Here’s a very rough sketch of how Microsoft Word works:
Suppose you start a new document and call it “Midterm Paper”. Let’s say that you type the letter “a”. Two things happen:
1. “a” is printed to the screen.
2. “a” is recorded in your “Midterm Paper” file as the first character.
Microsoft Word is a program that basically says:
- Look at drawer #1004 (keyboard drawer) to see what key the user presses.
- When the user presses a key, use the alphabet drawer to a) look up how to print it to the screen, and b) record the letter on the next card in drawer #640 (the drawer we’re going to use to hold the contents of “Midterm Paper”).
Next time you open up the “Midterm Paper” file, the file clerk prints the letters that are in drawer #640.
Solitaire is a program that happens to have internal state. I’ll explain what that means. With Microsoft Word, the state consists of the characters that you type. All of this state ends up being displayed to you by the screen. With solitaire, some of the state is displayed to you, but some of it isn’t.
For example, consider the group of cards on the right in the picture below:
Suppose these are stored by the computer in drawer #610. Only the 4 of clubs is printed to the screen. The face down cards aren’t printed to the screen, but they still do exist in drawer #610.
These face down cards are sometimes used by the program. For example, if you place the 4 of clubs on the clubs pile, the program will remove it from drawer #610, and print the next card in drawer #610 to the screen, because the game of solitaire says that it should now be face up.
Not everything the program needs is always displayed to you on the screen.
1’s and 0's
As you probably could have guessed, there aren’t actually any drawers or cards inside your computer. And there certainly isn’t a miniature file clerk running around.
That purpose of the analogy was to give you a medium resolution view of what’s going on. Now let’s ditch that medium resolution bullshit and see how they really work!
Since we’re using such a high resolution now, it’ll take longer for the big picture to become visible, so you’ll have to be more patient. However, once you get the big picture, it’ll be the real thing!
The material in this section is more complicated. It won’t sink in immediately. There is a lot of wiring to keep track of. And at this point, you’re probably experiencing some reader fatigue.
I’d like to make it less complicated, but if I cut anything else out, the section wouldn’t give you a complete enough picture of how computers work.
- Give it an initial skim to get a big picture overview.
- Come back to it a few times to let it sink in.
- Take a class like Nand2Tetris for an even deeper understanding.
Let’s start off with the building blocks of a computer. The building blocks of computers are logic gates. Consider the OR gate as an example. If electricity flows through either of the two input wires, it’ll flow through the output wire as well:
This can be shown with a truth table. Row #1 says that when A is off and B is off, the output will also be off. More generally, each row tells us what the output is for a certain combination of inputs.
The question of how these logic gates are built is a question for electrical engineers, not computer science people:
Elementary logic gates
In addition to OR, the other main elementary logic gates are: AND, NAND, and NOT.
AND and NOT work as you’d expect. NAND means “not and”. Its outputs are the opposite of AND’s outputs. See the diagram below:
NAND is the building block of other gates
Really, NAND gates are the true building blocks because we use them to build the NOT, AND and OR gates:
If you want to prove to yourself that the above wiring works out, use the truth tables and think about how the electricity flows. The video below walks you through that process:
NAND gates are cheap to produce, so it’s efficient to use them as the building blocks of other gates, rather than “building the other gates from scratch”.
Abstraction vs. implementation
The difference between abstraction and implementation is very important to understand. We talked about abstraction earlier — with abstraction, you know what will happen without knowing how it will happen.
For example, on the left, we look at the interface of this three-way AND chip. We see that it has three inputs, and that the output is only on when all three inputs are also on. However, the picture on the left doesn’t show us how this ends up happening. It doesn’t show us the internal wiring.
On the other hand, the picture on the right does show us the internal wiring. It does show us the implementation. By looking at the picture on the right, we can understand how the output is produced.
Sometimes we care about the how. Sometimes we just care about the what.
In this article, I focus more on the how. I believe that too much detail is distracting and makes it harder to grasp the big picture.
However, I often show you the implementation anyway. There are two reasons for this:
- To show you that it’s not magic.
- In case you’re curious how it works under the hood.
Be careful not to lose the forest for the trees. If you’re curious, it’s probably best to take a quick glance, and then return to the implementation after you gave the article an solid first pass through.
Other elementary logic gates
The diagram below describes three other elementary logic gates:
Adding one-digit numbers
Let’s start to show how these gates could do something useful. Consider the Half Adder. It allows us to add two binary one-digit numbers. Let’s first view it as an abstraction:
Row #4 says that when we add
1+1, a one is carried, and a zero is placed in the ones column (the
sum output wire).
1+1=10. In the world we’re used to, when we do
5+5, we can’t fit ten into the ones column, so we have to carry a one to the next column. Similarly, in binary, when we do
1+1, two doesn’t “fit”, because each column can only fit a zero or one, so we have to carry a one.
Now let’s look at how it’s implemented:
Remember, you don’t really have to worry about the wiring. The main purpose of this is to show you that it’s not magic. That we could build something useful using only elementary logic gates as our building blocks. If you’d like to think about the wiring, that’s awesome, but be careful not to lose the forest for the trees.
Adding bigger numbers
A Full Adder lets us add three one-digit numbers.
Here is how it is implemented:
Or, if you want to open up the hood of the Half Adder:
Again, we see useful things being done with simple building blocks.
Adding even bigger numbers
Let’s add two three-digit numbers:
Here’s the implementation:
Here is a walkthrough of how the Three-digit Adder can be used to add 101 to 110:
Here we built a chip that adds three-digit numbers. But if we added a Full Adder on to the end, we’d have a chip that adds four-digit numbers. By continuing to add Full Adders, we could add numbers with as many digits as we want.
We can have multi-bit versions of elementary logic gates. For example, consider AND and NOT:
In computers, everything is done with 16 bits. For now, just accept it. Later, you’ll get a sense for why this is. (Actually, some computers do everything with 32 bits, some with 64, some older ones with 8.)
Here are the 16-bit versions of AND and NOT:
Recall that we had a Three-digit Adder. By continuing to add Full Adders to it, we could build a 16-bit Adder:
The ALU is a chip that takes two 16-bit numbers as inputs and can compute one of the following:
Here is what the chip looks like:
Here is how it works:
It takes two 16-bit inputs (
zxis on, it will set the
xinput to zero.
nxis on, it will send
xthrough a NOT16.
zyis on, it will set the
yinput to zero.
nyis on, it will send
ythrough a NOT16.
fis on, it will send
ythrough an 16-bit Adder. Otherwise it will send
ythrough an AND16.
nois on, it will send the output of step 5 through a NOT16.
- If the output is zero,
zrwill be turned on. If the output is negative,
ngwill be turned on.
Based on which of these are on, the ALU will end up performing different computations. For example, if
zy are all off, and
no are all on, the ALU will essentially be computing
y-x. Perhaps you’d like to take the time to prove this to yourself.
To sum it all up:
Hey, this ALU is starting to look like the file clerk!
- If the ALU were a file clerk, you would have to hand him a card, tell him what computation you want him to perform, and then he would tell you the result of the result of the computation.
- He wouldn’t be able to read directions off of cards yet.
- He wouldn’t be able to go out and read from a card in one of the drawers. You’d have to literally hand him anything you want him to look at.
- He wouldn’t be able to write on cards yet. He’d just be verbally telling you what the result of his computation is.
Memory is important
Let’s start building the drawers and cards of our filing system.
It’s pretty important that we have drawers, because as you can see, our filing clerk only knows how to add. If we want him to multiply
5 * 4, we have to tell him to do
5 + 5 + 5 + 5. More specifically:
- Instruction 1: Add 5 and 5. Store the result on the card called
- Instruction 2: Add 5 to
result. Store this new value on
- Instruction 3: Add 5 to
result. Store this new value on
The key point here is that we need to store the intermediate results and build off of them.
If computers were smarter, we could just tell them to multiply
5 * 4. But because they’re dumb, we have to break it down into a series of smaller tasks, and have them store the intermediate results so they could build off of them.
If computers weren’t able to store intermediate results, we wouldn’t be able to have them do complicated things for us.
A Bit is a chip that can store a one or a zero. It’s the building block of a computers memory.
To explain how it works, let’s start off with a chip called a DFF:
A computer works in cycles. Each cycle is sorta like a heart beat. Basically, the DFF outputs whatever the input was for the previous cycle.
We can use the DFF to build a Bit:
Here is how the Bit works:
- You can turn the
loadwire on to load the value of
inonto the chip.
- It will output whatever the chips value was at the previous cycle.
Consider a use case.
- You turn the
loadwire on and load a one onto the Bit. Then, at each cycle, it will output one. The value of one is being stored onto the chip!
- Now you’re ready to load a zero onto the chip. So you turn the
inwire off and the
loadwire on. Now a zero will be loaded onto the chip, and the chip will continue to output zero until you load a one onto it.
Video walkthrough of interface:
Video walkthrough of implementation:
To build a Register, you just line up 16 Bits and have them share the same
To build a Register, we combined a bunch of Bits. To build RAM8, we combine a bunch of registers. To build RAM64, we combine a bunch of RAM8’s. We can continue this process to build larger and larger memory:
Why is it called "random" access memory?
"Shift register memories
weresimpler and hence cheaper, but you had to shift the memory round in a circle to extract the particular bits you wanted. Random access memories, as the name suggested, allowed you to access any memory location you wanted at random. But they needed more pins and more circuitry, so were more expensive." - https://www.quora.com/Why-is-RAM-called-random-access-memory
In the file system analogy, the clerk could only deal with one card at a time. In real computers, the ALU can only deal with one Register at a time.
There are a bunch of Registers in the RAM64, so how does the ALU get in there and select a particular Register to operate on? Or, to use the file system analogy, how does the clerk open a drawer and select a specific card?
At a high level, the RAM chip has input wires called
address that “point to” a certain register. Suppose that Register 6 is what is being pointed to. Then:
- Register 6 has its output forwarded out of the larger RAMn chip.
ingets loaded onto Register 6.
Here is how it works at a lower level:
Let’s take a very brief look at the CPU, and then afterwards let’s take a few steps back, looking at where we are right now and how it aligns with our filing system analogy.
The CPU is basically just the ALU plus three registers. Think of a filing clerk that has three pockets, with each pocket holding one card.
With that said, the parallels between the filing system analogy and the real thing are becoming clear:
- The CPU is analogous to the filing clerk.
- A Register is analogous to a card. A Bit is (more loosely) analogous to a letter written on a card.
- RAM8 is analogous to a drawer of cards. RAM64 is analogous to a larger filing system composed of many drawers.
The instructions for the CPU are “written down” in RAM (or ROM, which is very similar to RAM). The CPU starts at instruction #1, and goes through them one by one, unless the instruction its reading tells it to jump to a different instruction.
Here is how that works. Inside the CPU, one of its “pockets” is a chip called Program Counter (PC):
PC is basically just a register. Its output “points” to a particular register in Instruction Memory. Recall from earlier that this “pointing” is what allows us to access a particular Register amongst the many registers in RAM/ROM, and that the “pointing” is being done with MUX and DMUX chips.
Whichever register that is being pointed to in Instruction Memory has its contents fed out of the chip, and eventually fed into the CPU to be used as an instruction. At this point, you can see that an instruction is just 16 wires that are either on or off! Sixteen ones and zeros!
To understand how the CPU uses this instruction, let’s take a closer look:
This is starting to get a bit messy, but bear with me.
- The file clerk has three “pockets” — A, D, and PC.
- The card in pocket A tells him which card in data memory to look at. Note that it gets outputted as
addressMpoints to a particular register in data memory, and that the contents of this register are fed into the CPU as
- Similarly, the card in pocket PC tells him which card in the instruction memory to look at. Note that it gets outputted as
pcpoints to a particular register in instruction memory, and that the contents of this register are fed into the CPU as
- The ⓒ icons may be confusing. Basically,
instructionis 16 wires and the wires get split up, fed through different elementary logic gates, and fed into different chips. In the diagram above, I show that it gets split up into
j, and that these components get fed back into different chips (which I labeled).
The 16-bit instruction tells the CPU:
- Whether to feed A or M to the ALU (
- What computation the ALU should perform —
y-x, etc (
- What cards the output of the ALU should write to. Register A (
d1)? D (
d2)? M (
d3)? All three? None?
- Whether or not to jump to the instruction in pocket A (
Here is a video walkthrough of how this all happens:
At this point, hopefully you have a solid grasp of how computers work. Still, there’s only so much you can grasp in an hour or two. I expect that:
- You’re still a bit overwhelmed and confused by all of this wiring. There’s a lot of stuff going on and it takes a good amount of exposure before things become less difficult.
- You don’t fully grasp how such large tasks could be broken down into such small instructions. Explaining the concept of abstraction can only get you so far. If you get some experience writing software — especially if you do so at different levels of abstraction — you’ll develop a much better sense of how a dumb machine can accomplish so much.
If you’d like to learn more, I’d highly recommend going through Nand2Tetris. It’s a free course that basically starts you off with a NAND gate and has you build your own computer (using software to simulate hardware)! Much of this article borrows from and is inspired by that course.
I’d also highly recommend reading But How Do It Know. I love iterating on my writing, so if there’s anything at all that’s confusing, please email me at firstname.lastname@example.org.