Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This is almost embarrassing ask...I have a degree in Computer Science (and a second one in progress). I've worked as a full-time .NET Developer for nearly five years. I generally seem competent at what I do.

But I Don't Know How Computers Work!

Please, bare with me for a second. A quick Google of 'How a Computer Works' will yield lots and lots of results, but I struggled to find one that really answered what I'm looking for. I realize this is a huge, huge question, so really, if you can just give me some keywords or some direction.

I know there are components....the power supply, the motherboard, ram, CPU, etc...and I get the 'general idea' of what they do. But I really don't understand how you go from a line of code like Console.Readline() in .NET (or Java or C++) and have it actually do stuff.

Sure, I'm vaguely aware of MSIL (in the case of .NET), and that some magic happens with the JIT compiler and it turns into native code (I think). I'm told Java is similar, and C++ cuts out the middle step.

I've done some mainframe assembly, it was a few years back now. I remember there were some instructions and some CPU registers, and I wrote code....and then some magic happened....and my program would work (or crash). From what I understand, an 'Emulator' would simulate what happens when you call an instruction and it would update the CPU registers; but what makes those instructions work the way they do?

Does this turn into an Electronics question and not a 'Computer' question? I'm guessing there isn't any practical reason for me to understand this, but I feel like I should be able to.

(Yes, this is what happens when you spend a day with a small child. It takes them about 10 minutes and five iterations of asking 'Why?' for you to realize how much you don't know)

share

locked by maple_shaft Jul 22 '13 at 18:36

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

closed as not a real question by Walter, Aaronaught, Mark Trapp Jun 5 '11 at 17:48

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
See en.wikipedia.org/wiki/Instruction_cycle –  rwong Jun 5 '11 at 4:54
20  
I'd recommend Code by Charles Petzold –  Glenn Nelson Jun 5 '11 at 5:02
    
It is not an Emulator. It is a bunch of real semiconductor circuits with hundreds (thousands) of input and output wires, and billions of logic gates. The Emulator was used for instructional purpose only. –  rwong Jun 5 '11 at 5:15
1  
I think the title should be changed to be more specific/informative. This is actually a pretty useful question, but at first glance it looks totally ridiculous, like what the small child originally asked. Change it to something like "how do high-level software commands connect to low-level hardware responses?" –  jhocking Jun 5 '11 at 13:23
2  
Hi Rob, as worded right now, this is hopelessly broad and has quickly turned into a book recommendation question and extended discussion: both of which we don't want here. If you can tighten up your focus and ask something specific about a problem you're currently facing, feel free to ask about that. –  user8 Jun 5 '11 at 17:53

13 Answers 13

up vote 140 down vote accepted

I will start from the lowest level that might be relevant (I can start from even lower level, but they are probably way too irrelevant), starting from Atom, to Electricity, to Transistors, to Logic Gates, to Integrated Circuits (Chip/CPU), and finishes at Assembly (I'd assume you are familiar with the higher levels).

In the Beginning

Atom

Atom is a structure composed of electrons, protons, and neutron (which themselves are composed of elementary particles). The most interesting part of the atom for computers and electronics are the electrons because electron are mobile (i.e. it can move around relatively easily, unlike protons and neutrons which are more difficult to move) and they can free-float by itself without being held inside an atom.

Usually, each atoms has equal number of protons and electrons, we call this "neutral" state. As it happens, it is possible for an atom to lose or gain extra electrons. Atoms in this unbalanced state are said to be "positively charged" atom (more proton than electrons) and "negatively charged" atom (more electron than proton) respectively.

Electrons are unconstructible and indestructible (not so in quantum mechanics, but that's irrelevant for our purpose); so if an atom loses an electron, some other atom nearby had to receive the extra electrons or the electron had to released into a free floating electron, conversely since electron is unconstructible, to gain extra electron, an atom had to sap it off nearby atoms or from a free floating electron. The mechanics of electrons is such that if there is a negatively-charged atom near a positively-charged atom, then some electrons will migrate until both atoms have the same charge.

Electricity

Electricity is just a flow of electrons from an area with very high number of negatively-charged atoms to an area with very high number of positively-charged atoms. Certain chemical reactions can create a situation where we have one nodes with lots of negatively-charged atoms (called "anode"), and another node with lots of positively-charged atoms (called "cathode"). If we connect two oppositely charged nodes with a wire, masses of electrons will flow from the anode to cathode, and this flow is what we call "electric current".

Not all wires can transmit electrons equally easily, electrons flows much easily in "conducting" materials than in "resistant" materials. A "conducting" material have low electrical resistance (e.g. copper wires in cables) and a "resistant" material have high electrical resistance (e.g. rubber cable insulation). Some interesting materials are called semi-conductors (e.g. silicons), because they can alter their resistance easily, under certain conditions a semiconductor might act as a conductor and at other conditions it might turn into a resistor.

Electricity always prefers to flow through the material with least resistance, so if a cathode and anode are connected with two wires, one having very high resistance and the other with very low resistance, the majority of electrons will flow through the low resistance cable and nearly none flows through the high resistance material.

The Middle Age

Switches and Transistors

Switches/Flip-Flops are like your regular light switches, a switch can be placed between two pieces of wire to cut off and/or restore electricity flow. Transistors works exactly the same as a light switch, except that instead of physically connecting and disconnecting wires, a transistor connects/disconnects electricity flow by altering its resistance depending on whether there is electricity in the base node, and, as you might have already guessed/know, transistors are made from semiconductors because we can alter semiconductor to become either a resistor or a conductor to connect or disconnect electric currents.

One common type of transistor, the NPN Bipolar Junction Transistor (BJT), has three nodes: "base", "collector", and "emitter". In an NPN BJT, electricity can flow from the "emitter" node to the "collector" node only when the "base" node is charged. When the base node is not charged, practically no electron can flow through and when the base node is charged, then electrons can flow between the emitter and the collector.

The behavior of a transistor

(I highly suggest you read through this before continuing, as it can explain better than me with interactive graphics)

Let's say we have a transistor connected to an electric source at its base and collector, and then we wire up an Output cable near its collector (see Figure 3 in http://www.spsu.edu/cs/faculty/bbrown/web_lectures/transistors/).

When we apply electricity to neither base nor collector, then no electricity can flow at all since there is no electricity to talk about:

B   C  |  E   O
0   0  |  0   0

When we apply electricity to the collector but not the base, electricity cannot flow to the emitter since the base becomes a high resistance material, so the electricity escapes to the Output wire:

B   C  |  E   O
0   1  |  0   1

When we apply electricity to the base but not the collector, also no electricity can flow since there is no charge difference between the collector and the emitter:

B   C  |  E   O
1   0  |  0   0

When we apply electricity to both base and collector, we get electricity flowing through the transistor, but since the transistor now has lower resistance than the Output wire, nearly no electricity flows through the Output wire:

B   C  |  E   O
1   1  |  1   O

Logic Gates

When we connect the emitter of one transistor (E1) to the collector of another transistor (C2) and then we connect an output near the base of the first transistor (O) (see Figure 4 in http://www.spsu.edu/cs/faculty/bbrown/web_lectures/transistors/), then something interesting happens. Let's also say we always apply electricity to the collector of the first transistor (C1) and so we only play around with the the base nodes of the transistors (B1,B2):

B1   B2   C1   E1/C2  |  E2   O
----------------------+----------
0    0    1    0      |  0    1
0    1    1    0      |  0    1
1    0    1    0      |  0    1
1    1    1    1      |  1    0

Let's summarize the table so we only see B1, B2, and O:

B1   B2  |  O
---------+-----
0    0   |  1
0    1   |  1
1    0   |  1
1    1   |  0

Lo and behold, if you're familiar with Boolean Logic and/or Logic Gates, you should notice that this is precisely the NAND gate. And if you're familiar with Boolean Logic and/or Logic Gates you might also know that a NAND (as well as NOR) is functionally complete, i.e. using NAND only, you can construct all the other logic gates and the rest of the truth tables. In other word, you can design a whole computer chip using NAND gates alone.

In fact, most CPUs are (or is it used to be?) designed using NAND only since it is cheaper to manufacture than using a combination of NAND, NOR, AND, OR, etc.

Deriving the other boolean operators from NAND

I would not describe how to make all boolean operators, only the NOT and the AND gate, you can find the rest somewhere else.

Given a NAND operator, then we can construct a NOT gate:

Given one input B
O = NAND(B, B)
Output O

Given a NAND and NOT operator, then we can construct an AND gate:

Given two inputs B1, B2
C = NAND(B1, B2)
O = NOT(C) // or NAND(C,C)
Output O

We can construct other logic gates in a similar way. Since NAND gate is functionally complete, it is also possible to construct logic gates with more than 2 inputs and more than 1 output, I'm not going to discuss how to construct such logic gates here.

Enlightenment Age

Building a Turing Machine from Boolean Gates

A CPU are just a more complicated version of a Turing Machine. The CPU registers are the Turing Machine's internal state, and the RAM is a Turing Machine's tape.

A Turing Machine (CPU) can do three things:

  • read a 0 or 1 from the tape (read a cell of memory from RAM)
  • change its internal state (change its registers)
  • move left or right (read multiple position from the RAM)
  • write a 0 or 1 to the tape (write to a cell of memory to RAM)

For our purpose, we're building Wolfram's 2-state 3-symbol Turing Machine using combinatorial logic (modern CPUs would use microcode, but they're more complex than is necessary for our purpose).

The state table of the Wolfram's (2,3) Turing Machine is as follow:

    A       B
0   P1,R,B  P2,L,A
1   P2,L,A  P2,R,B
2   P1,L,A  P0,R,A

We want to reencode the state table above as a truth table:

Let I1,I2 be the input from the tape reader (0 = (0,0), 1 = (0,1), 2 = (1,0))
Let O1,O2 be the tape writer (symbol encoding same as I1,I2)
Let M be connected to the machine's motor (0 = move left, 1 = move right)
Let R be the machine's internal state (A = 0, B = 1)
(R(t) is the machine's internal state at timestep t, R(t+1) at timestep t+1)
(Note that we used two input and two outputs since this is a 3-symbol Turing machine.)

      R  0          1
I1,I2
(0,0)    (0,1),1,1  (1,0),0,0
(0,1)    (1,0),0,0  (1,0),1,1
(1,0)    (0,1),0,0  (0,0),1,0

The truth table for the state table above:

I1  I2  R(t) | O1  O2  M   R(t+1)
-------------+--------------------
0   0   0    | 0   1   1   1
0   0   1    | 1   0   0   0
0   1   0    | 1   0   0   0
0   1   1    | 1   0   1   1
1   0   0    | 0   1   0   0
1   0   1    | 0   0   1   0

I'm not really going to construct such a logic gate (I'm not sure how to draw it in SE and it's probably going to be quite huge), but since we know that NAND gate is functionally complete, then we have a way to find a series of NAND gates that will implement this truth table.

An important property of Turing Machine is that it is possible to emulate a stored-program computer using a Turing machine that only have a fixed states table. Therefore, any Universal Turing Machine can read its program from the Tape (RAM) instead of having to have its instruction hardcoded into the internal state table. In other word, our (2,3) Turing Machine can read its instructions from I1,I2 pins (as software) instead of being hardcoded in the logic gate implementation (as hardware).

Microcodes

Due to the increasing complexity of modern CPUs, it becomes prohibitively difficult to use combinatorial logic alone to design a whole CPU. Modern CPU is usually designed as an interpreter of microcodes instruction; a microcode is a small program embedded in the CPU that is used by the CPU to interpret the actual machine code. This microcode interpreter itself are generally designed using combinatorial logic.

Register, Cache, and RAM

We have forgotten something above. How do we remember something? How do we implement the tape and RAM? The answer is in an electronic component called Capacitor. A capacitor is like a rechargeable battery, if a capacitor is charged it will retain extra electrons and it can also return electrons to the circuitry.

To write to a capacitor, we fill the capacitor with electron (write 1) or drain all the electrons in the capacitor until it's empty (write 0). To read the value of a capacitor, we try to discharge it. If, when we try to discharge, there is no electricity flowing, then the capacitor is empty (read 0), but if we detect electricity, then the capacitor must be charged (read 1). You might notice that reading a capacitor drains its electron store, modern RAMs have the circuitry to periodically recharge capacitor so they can retain their memory as long as there is electricity.

There are multiple types of capacitors used in a CPU, the CPU registers and the higher level CPU caches are made using very high-speed "capacitors" that is actually built from transistors (therefore there is almost no "lag" to read/write from them), these are called static RAM (SRAM); while the main memory RAM is made using lower power, but slower and much cheaper capacitors, these are called Dynamic RAM (DRAM).

Clock

A very important component of a CPU is the clock. A clock is a component that "ticks" regularly to synchronize processing. A clock typically contains a quartz or other materials with well-known and relatively constant oscillation period, and the clock circuitry maintain and measures this oscillation to maintain its sense of time.

CPU operations are done between clock ticks and read/writes are done in the ticks to ensure that all components move synchronously and not trample into each other while in intermediate states. In our (2,3) Turing Machine, between clock ticks electricity passes through the logic gates to calculate the output from the input (I1, I2, R(t)); and in the clock ticks, the tape writer will write O1,O2 to the tape, the motor will move depending on the value of M, and the internal register is written from the value of R(t+1), then the tape reader will read the current tape and put charge into I1,I2 and the internal register is reread back to R(t).

Talking with Peripherals

Note how the (2,3) Turing Machine interfaces with its motor. That is a very simplified view of how a CPU may interface with an arbitrary hardware. Arbitrary hardware can listen or write to a specific wire for inputs/outputs. In the case for the (2,3) Turing Machine, its interface with the motor is just a single wire that instructs the motor to turn clockwise or counterclockwise.

What is left unsaid in this machine is that the Motor had to have another "clock" that runs in synchrony with the Machine's internal "clock" to know when to start and stop running, so this is an example of a synchronous data transmission. The other commonly used alternative, asynchronous transmission uses another wire, called the interrupt line, to communicate synchronization points between the CPU and the asynchronous device.

Digital Age

Machine code and Assembly

Assembly language is a human readable mnemonic for machine codes. In the simplest case, there is a one-to-one mapping between assembly to machine code; although in modern assembly languages some instructions may map to multiple opcodes.

Programming Language

We all are familiar with this aren't we?


Phew, finally finished, I typed all this in just 4 hours, so I'm sure there is a mistake somewhere (I'm primarily a programmer, not electric engineer nor physicists, so there might be several things that is blatantly wrong). Please if you found a mistake, don't hesitate to give a @yell or fix it yourself if you have the rep or create a complementary answer.

share
37  
"awesome" doesn't do it justice. This answer is positively heroic. –  njd Jun 5 '11 at 20:46
4  
+1 for the effort :) –  back2dos Jun 5 '11 at 20:53
1  
Isn't the atoms part incorrect? Generally they will ionize to form charged atoms rather than stay neutral in order to have a full outer subshell. –  alternative Jun 5 '11 at 21:27
3  
+1, +1000 if I could. What is truly astonishing to me, and why I love this field, is that the great breadth of information in this answer doesn't even barely begin to scratch the surface of the amazing depth of the technology that makes a modern PC work. –  qes Jun 5 '11 at 21:40
3  
+1 though I feel I should note that modern hardware isn't exactly done via NAND gates. There are gates, yes, but they're really quite complex and only (usually) approximate NAND logic; the constraints are pretty much physical. The physical gates are first assembled into logic modules, which could be a classic NAND but are usually rather more (e.g., flip-flop or half-adder). The exact set of gates allowed depends on the logic style being used and the constraints of the fabrication plant; not every fab can build everything. (Aaargh! I'm starting to remember details! Help!) –  Donal Fellows Jun 6 '11 at 10:45

From Nand to Tetris In 12 Steps

I think this will be aboslutely perfect for you:

From Nands to Tetris In 12 Steps

share
    
thanks for the link, watching it now. Is there a central index of these talks, or did you just happen to see this one on a blog somewhere? –  jhocking Jun 5 '11 at 13:29
    
I'm not sure, this particular came up as I was surfing one day –  Darknight Jun 5 '11 at 14:07
    
He missed a 13th step - silicon wafers. –  Job Jun 5 '11 at 14:49
    
thanks for posting it! This is basically the summary of the syllabus of the course that the author of the talks in the video lectures at IDC in Israel, and is based on his book - I posted a link to the book in my answer. –  littleadv Jun 5 '11 at 19:05
    
+1 Nice, thanks –  Anthony Aug 20 '12 at 8:36

If you've done assembly, then there are really only two or three layers left to understand:

  • Logic gates, which is how logic is implemented via moving electrons - here it does become an electronics quesion
  • CPU and system design, which is how logic gates are composed to form a CPU and connected with RAM and peripherals. Modern CPUs are extremely complex, but for your desire to understand how things basically work, it should be sufficient to look at a classic simple CPU like the Z80.
  • Microcode, which is how assembly instructions are interpreted and turned into hardware-level actions of registers and logic circuits.

The last one (Microcode) is what made it "click" for me, because it filled the gap between electronics and code.

share
    
Maybe it's microcode that I'm missing. I've taken classes in logic gates and CPU/system design, programmed in assembly, and learned all about binary commands (and done well in all those classes), but I still couldn't tell you how the heck it all fits together. I'll have to research microcode. –  Casey Patton Jun 5 '11 at 11:14
    
@Casey: quite possible that that's indeed what you're missing. For me, learning about (and writing some) microcode was exactly where I though: OK, now I understand how computers work. Sure, they've become so complex that you can still easily run into situations that seem impossible to understand, but I am quite convinced that any such situation can be understood if you're competent, persistent and invest enough time. –  Michael Borgwardt Jun 5 '11 at 11:21

Example for a CS undergraduate course syllabus that explains precisely what you asked about can be found here (IDC.AC.IL course CS101). It's based on this MIT Press book: "The Elements of Computing Systems: Building a Modern Computer from First Principles".

share
1  
+1 for mentioning "The Elements of Computing Systems". It's a great book, and should precisely answer the question of "How do computers work?". –  Cedric Jun 5 '11 at 17:06
    
would you mind explaining more on what these resources do and why do you recommend these as answering the question asked? "Link-only answers" are not quite welcome at Stack Exchange –  gnat Jul 22 '13 at 18:04

To fully answer this question would take an entire book. Luckily someone has already written that book. It's called Code: The Hidden Language of Computer Hardware and Software by Charles Petzold. It's a highly informative and very entertaining read.

share
1  
+1, just Ctrl-F for "Petzold" brings the answer I was pretty sure was there ;) –  mlvljr Jun 7 '11 at 21:16

I highly reccomend Code by Charles Petzold. The book is both a history lesson and a technical overview of how to build a computer. Starting with explaining simple telegraph switches the book demonstrates how transistors work, then to logic gates, programmable computer, to more complex stuff. Its also very well written and could probably be grasped by anyone with enough curiosity.

share

It's going to be way too difficult (and long!) to list everything that you may need to know to gain a sufficient understanding of what you need to know. A famous book that actually answers all these questions is from Andrew Tanenbaum: Structured Computer Organization.

This book actually takes you from the physical computer on your desk all the way down to logic gates and boolean algebra, then shows an example architecture to guide you through how everything actually happens in such a system.

(One note: it's very expensive since it's ~800 pages. It's probably good just to get a second hand version or an older edition. The concepts didn't change.)

share

Well it takes a lot of balls to say that and ask i guess.

Pretty much the code is reduced further and further down to more complicated lower level code. Down to Assembly level code with push and move registers.. etc...

Then the hardware takes this code, and acts on it. Most times the hardware will actually have their own instructions about how to do things. So there might be a simple instruction such as a PUSH where a register (memory location) gets a value such as 1 or 2 or whatever..

It's definitely a computer question. And also a programming one. Some programmers program the hardware that will take your code and make it do something albeit at a very low level. It's also an electronics question.

share

There are devices.

Then there are device drivers which interact with these devices. Part written in C, part in assembly typically.

The OS interacts with application software at one end and device drivers at the other to communicate with the actual hardware.

If you are really interested why not do a Linux kernel hacking to learn more?

share
    
Thank you for your answer and suggestion. That sounds like a great idea! –  Rob P. Jun 5 '11 at 5:19

At the core of things this is an electronics question, though the basics of this should have been covered in the survey course for any CS degree. All hardware acts based on gates that are programmed into it at the component level. These are the most basic of logical operations: NOT, AND, OR, XOR, NAND, NOR. Each gate has a specific function:

The NOT gate takes one input value and produces one output value, it gets a 0 or 1 and outputs the opposite.

The AND gate takes two input values and produces one output value, it gets any combination of 0 and 1 and outputs 0 for any combination except two ones, for which it outputs a 1.

The OR gate works much like the AND gate, but will produce a 1 for every combination of 0 and 1 it gets except two zeros, for which it outputs a 0.

The XOR gate is again similar to both the AND and OR gates, but it will produce a 0 when both inputs are the same, and a 1 when both inputs are different.

The NAND gate is the logical opposite of the AND gate and the NOR gate is the logical opposite of the OR gate.

In other words, at the hardware level, it all comes down to the most basic of binary logical expressions. Everything else is just a transition from a higher level of programming to a lower one until it reaches this last layer.

share
    
+1 The next level is how to make a sequential behaviour with these logic gates. The key component is the flip-flop. Assemble logic gates to form an ALU, some flip-flop for registers, a clock, and you get a CPU. –  mouviciel Jun 5 '11 at 7:49
    
@mouviciel You forgot the multiplexers. –  starblue Jun 6 '11 at 19:50

For the part on transforming a program in an high level language in machine instructions, any compiler book should fill the bill. For instance the dragon book.

For the part on "how instructions are executed?" Computer Organization and Design: The Hardware/Software Interface should fill the bill.

share

Although I find it staggering that someone can complete a course in CS without understanding the hardware, I suppose it's entirely possibly that a course could concentrate only on the theory as a branch of mathematics, rather than the engineering and implementation details. The venerable SICP lectures (as delivered in the 1980s) seemed to be like this.

On my CS course, two decades ago, an earlier edition of Computers: from Logic to Architecture was required reading in the first year.
Something like this should fill in the gaps.

Alternatively, MIT's Open Courseware should have something that will help.

share

Well, despite being an expert Z80, 6502, 6809, 68K and 80x86 assembler programmer (or at least I was 20 years ago) I can frankly say I know zilch about digital logic, transistors or electrons. So don't feel so bad!

share

Not the answer you're looking for? Browse other questions tagged or ask your own question.