Mr. Martin's programming school

Computers, how do they work?

Bits and bytes and stuff »
You don't need to know how computers work in order to follow this course. If you already know that the answer to the Ultimate Question of Life, the Universe, and Everything is 42, you might skip this page. Well, you can skip it anyway, but if you later want to do some serious programming, it migth be good to have some knowledge on how the computers work, especially if you go into writing drivers, program in assembler or do embedded programming. Anyway, here's a ligth starter if you really do not know anything about how it works.

Today we take computers for granted and most people does not stop to reflect on how they actually work. It is not easy to explain the inner works of a computer without getting too technical for just anybody to understand without a lot of prior knowledge about digital electronics.

But I'll try another approach that will give you a gut feeling of how it works. Note, this is not a desctiption of how a computer works. Far from it. What it is, is an inspiration to trigger your fantasy to envision the kind of modus operandi that is going on inside a computer. Nothing more, and if you know the least of how computers work, you might as well skip this lesson. Or read on for fun.

Let's compare the microprocessor with the mechanical program of an old-style washing-machine. I do not assume you know how that works, but I'll give you a rough idea here.
Imagine a wheel with a notch. Actually, the weel probably has more than one notch. And there are more than one wheel in a mechanical program. Each wheel corresponds to one electrical componen in the washing-machine, e.g. a pump, a motor, a heater etc. A wheel with a notch There are many such wheels mounted on a common shaft. The shaft is turned slowly by an electrical motor that has been geared down to a very slow speed. One turn of the wheel corresponds to the entire washing program, or all washing programs if the washing-machine has more than one program. A number of wheels on a shaft
The notches are there to actuate so called micro-switches. In this case we have micro-switches that are in its 'off' position when its arm is in one of the notches. Just look at the images below:
In this position, when the end of the arm is in a notch, the switch is off. The electrical device that is supplied with power via this switch is not in operation. It has no power. Here, however, the arm of the switch is pushed up and the switch conveys current. The electrical device connected to the switch has power and is operating.
Add the following components:
Now, let us 'build' a washing-machine with these components. I know, it is over-simplified, but it is enough to give you an understanding of the principles I'm trying to explain.

There is one specifics in my model. The small motor, the clock, that turns the shaft is supplied via one of the micro-switches, but also via the level-switch which is supplying power when the washing-machine is full with water. So the shaft is turning either when the machine is full with water or when the micro-switch is on, and of corse also if both those conditions are true.

In the start position all switches are sunken into a notch each. The machine is fully off and nothing happens. Then the user turns the program knob one step forward. Some swithces will now be pushed over to its 'on' position and the machine starts.

This is the sequence per program step:

Note that the washing water and the rinsing water is regulated through the same valve. The water is fed into the machine via a small container where the user had already poured some washing-powder. All powder is flushed down into the machine the first time, thus we get washing water in step 2, while we get rinsing water in step 5.

Now, let's make a truth table of this. The columns below represent the switches and the rows are the steps.

Clock Valve Motor Pump Description
Off Off Off Off Step 1: Machine is off. Nothing happens.
Off On Off Off Step 2: Water is entering the machine. When full, the clock starts.
On Off On Off Step 3: The motor is turning the drum to wash the clothes while the clock slowly turns the shaft.
On Off On On Step 4: The pump is draining the machine while the drum still turns and the clock slowly turns the shaft.
Off On Off Off Step 5: The valve fills the machine with rinsing water.
On Off On Off Step 6: The motor turns the drum to rinse the clothes.
On Off On On Step 7: Rinsing is finished and the pump starts draining the machine again.
Off Off Off Off Step 1: The machine comes to a full stop.

The example uses mechanical switches. They turn on when not in a notch, and off when in a notch.
But that doesn't sound like something too usable in a computer. At least not nowadays. There actually was those days long ago when a computer used electromechanical components, but those days are, thanks heaven, long gone. Gone are also the generation that used vacuum tubes, and that is also good. Just imagine a lap-top that has no disk, some lamps for display, 32 bytes of RAM, flip-switches rather than a keyboard, and runs like 10 minutes on a fully charged car battery, is like 2 cubic metres in size and produces enough heat to cook dinner for three persons in those 10 minutes. And it is heavy.

In came the transistor, and made a revolution to all electronics. The name 'transistor' is short for 'trans-resistor', a transformation in resistance between input and output.
This is a symbol of the transistor. The part in the circle is the actual transistor. To the rigth of the transistor is a battery. The current from the battery is to run through the lamp. The lamp is the circle with a cross in it at the top. But the lamp is not lit because the transistor does not convey any current. The power is off. When a small current is fed in from the left the transistor 'opens' and lets the battery feed current through the lamp. And the lamp is lit. A small current fed in from the left, at the terminal called 'base' if you are interested, opens for a much larger current running from the top connector, called the 'collector' and out through the bottom, 'emitter' connector. That is why a transistor can do it's job as an amplifier.

And that is also why the transistor can be used as a switch. No more wheels with notches. Modern washing-machines are controlled by electrical signals. A small signal in can start a motor, a valve or whatever. And they can open other transistors. To make a transistor conductive, apply a voltage, via a resistor. Modern computers work with 2.8, 3.3, 5 or some other low voltage for the logics. Whatever voltage, we use to say that if a voltage is applied to a transistor, it is fed a '1', else a '0'. So the one's and zero's you might have heard about are actually voltage or no voltage on some wires and connections inside the computer.

Let us regard our washing sequence as a sequence of one's and zero's:

Clock valve Motor Pump Description
0 0 0 0 Step 1: Machine is off. Nothing happens.
0 1 0 0 Step 2: Water is entering the machine. When full, the clock starts.
1 0 1 0 Step 3: The motor is turning the drum to wash the clothes.
1 0 1 1 Step 4: The pump is draining the machine.
0 1 0 0 Step 5: The valve fills the machine with rinsing water.
1 0 1 0 Step 6: The motor turns the drum to rinse the clothes.
1 0 1 1 Step 7: Rinsing is finished and the pump starts draining the machine again.
0 0 0 0 Step 1: The machine comes to a full stop.

If we want, we can call the combinations of one's and zero's 'instructions' for the washing-machine:

0000 - No operation
0100 - Load with water
1010 - Wash
1011 - Clear out the washing-water
0100 - Load with rinse water
1010 - Rinse
1011 - Clear out the rinsing-water
0000 - No operation

As you might notice, wash and rinse has the same instruction code: 1010. That is because the same parts of the machine is used even if they are used for two different purpouses. The same is the case with codes 0100 and 1011. The codes are obviously more general than the use of them. Therefore we might construct a list of 'machine codes' giving them short names just to make it easier to remember what they do. Such codes are called 'mnemonics':

0000 - NOP
0100 - Load
1010 - Work
1011 - Clear

We now actually have a full list of instructions in mnemonics and machine code. If we had a computer program that could translate the mnemonics to machine code, a process called 'assembling', we could write the following program:


...and use the assembling program to translate that into machine code that the washing-machine understands. Then we would get the following list of machine codes:


Now it is just to put this into the modern washing-machine and make it execute the program. Then why did I remove the first NOP? Well that is because there would be a start button on the machine that the user pushes to get the program started.

I am fully aware that this is very, very over-simplifying, but the principle holds. A real-life washing-machine of course has a much more sofisticated program executed by a microcontroller (that is a kind of self-containing mini-computer in a chip that holds microprocessor, memory and other stuff) and buttons for program selection, a timer and a display.

Logic circuits

As I mentioned very shortly above, a transistor can also be used to control another transistor. For what purpous? Well, combining transistors in a smart way builds logics. Say that two different transistors both supply input to a third. If any or both of the two transistors are fed a '1', then the third transistor will also output a '1'. (Again I am simplifying, but the principle is 100% correct.) What will those three transistors constitute? An OR function of course. You have two inputs, and if either one or both are set to '1', the output is also a '1', else a '0'.

In a similar way AND, exclusive OR (XOR) and other logic functions are built. And then those logic functions are combined to make more complex logics and registers (small one-byte or a few-bytes memories inside the microprocessor). Even better, there are logic functions that combines two register to get the sum or difference between them, and now we have two of the four mathematical operations built into the microprocessor.

Now it gets complex, but do not worry. This is a work in progress where those involved creates one piece each, and nobody knows all. It is enough for you to understand what we have seen here in order to understand how the programs you are going to write are able to work in your computer.

What you need more to understand is that all periferals are controlled by signals. Just like the motors and valves of a washing-maching. They are just much more complex. When using a simple 7-segment digit display a value is presented as a combination of at least 4 bits. 4 bits can be combined into 16 different values, and we need 10 of them for the display. (A 7-segment display can show one digit.) A combination of simple logic functions, like AND, XOR and OR are combined to make the right segments of the display lit when a binary value from 0000, 0001, 0010... to 1001 is applied to it. Now it can display values like these:

A display like your computer screen is much more complex, but not more complicated. It is simply much more complex because it uses many more combined functions, not more complicated functions. However, it also needs more than just a value applied, as was the case with the 7-segment digit. For the display we have a complex grafics card, and in order to utilize it, we need some programming to make it work, a device driver.

You do not need to know how to command your display. This is so commonly done that it would be ridiculous if all programmers had to do that over and over again. No, programs for doing that are already written and inserted at different places. E.g. the program chip called BIOS (Basic Input and Output System) contains among lots of other things low-level programs that can e.g. write text on you screen. Then on top of that, Windows has standard functions for you to call in order to use the functions in BIOS. They are packed for you in an API (Application Programmers Interface).

So, in order to write the famous 'Hello world!' in a command prompt program, you simply write something like System.Console.WriteLine("Hello world!");

A compiler translates that into machine code that does not write 'Hello world!' on you command window, it rather translates it into machine code that in the end puts the string in some place in memory and calls some BIOS function, telling it where in memory the string is stored, and that it should be printed in the command window.

I think it is enough about that. Understanding the inner workings of a computer is not nessecary for the ability to write computer programs. What is nessecary is the knowledge of the program languages and the API:s you are going to use, and the ability to think up the logics you need to solve tasks at hand, and I will dedicate the rest of these cousses to help you with that.

So, as John Cleese used to say 'And now to something completely different'. You might benefit from reading some basics about bits and bytes or, if you have installed Visual Studio for the Desktop, you could skip that and go ahead and try the first lesson on Console programming.