0.0 Computers
Programming is the art of telling computers to do what you want them to do. To accomplish this, it's very useful to have some basic understanding of what computers are and how they work.
There are many different types of computers. You might be reading this on a desktop, laptop, tablet or phone. These are all examples of computers. Other computers are used in cars, airplanes and even in your microwave. This website is also served by a computer (server) somewhere on the internet.
What all computers have in common is that they are machines that process data. Computers come in many shapes and sizes, but in order to do their job of processing data they all have the same basic parts in common:
Components of a computer
First, there needs to be a way for the original data to enter the computer and for the processed data to leave it. This is done through input and output devices (i/o devices). Examples of input devices are keyboards, mice, touchscreens and microphones. Examples of output devices are monitors, printers and speakers.
The actual processing of the data is done by the Central Processing Unit (CPU). The CPU is the brain of the computer. It can do calculations and other data transformations.
The CPU is connected to the main memory of the computer. This is where the CPU stores the data it is currently working on. The main memory is also called RAM (Random Access Memory). The data in RAM is lost when the computer is turned off.
The computer also has mass storage. This is where the computer stores data for a longer time. Examples of mass storage are hard drives and SD cards. The data in mass storage is not lost when the computer is turned off. Mass storage can also function as i/o devices from which input data can be read and where output data can be stored.
The CPU, main memory and mass storage are connected to each other through a set of buses. Buses are like the roads that connect the different parts of the computer. The data travels from one part to another over these buses.
Inside the CPU is a small set of very fast storage locations called registers. Registers temporarily hold the values the CPU is currently working on, such as numbers being added together or the memory address of the next instruction to fetch. Because registers live inside the CPU, they can be read from and written to much faster than main memory.
All computer memory (mass storage, main memory and registers) is made up of bits. A bit is a single unit of information that can be either a 0 or a 1. Groups of bits can represent different kinds of data depending on the context they are interpreted in. We will see many examples of this later on, but for now just think of bits as the building blocks of all data in a computer.
Instructions and machine code
For the CPU to do its job, it needs instructions to follow. Programs consist of a list of instructions Programs are usually loaded from mass storage into main memory before they are run. The CPU reads the instructions from the main memory and executes them. Each instruction tells the CPU to execute a specific operation, where to get the input data of the operation and where to put the output data. The instructions are written in a language called machine code. Machine code is a relatively simple language that the CPU can understand. It is made up of 1s and 0s. Each instruction is a sequence of 1s and 0s that tells the CPU to do something.
The following simple instruction will write the value 1 into register 2:
00000101 00000010 00000001
Here, the first part 00000101 represents a »load number« operation, the second part 00000010 represents register 2, and the third part 00000001 is the value to load into that register. After this instruction runs, register 2 holds the value 1.
The following instruction will write the value 4 into register 3:
00000101 00000011 00000100
Now we add both numbers and put the result into register 1:
00000110 00000001 00000010 00000011
Here, the first part 00000110 represents an »add« operation, the second part 00000001 represents register 1 (the destination), the third part 00000010 represents register 2 (the first operand), and the fourth part 00000011 represents register 3 (the second operand). After this instruction runs, register 1 will hold the value 5 (1 + 4).
Now we use a different instruction to add the content of register 1 to the number 7 and write the result back into register 1:
00000111 00000001 00000001 00000111
Here, the first part 00000111 represents an »add number« operation, the second part 00000001 represents register 1 (the destination), the third part 00000001 represents the first operand (the content of register 1), and the fourth part 00000111 is the number 7 (the second operand). After this instruction runs, register 1 will hold the value 12 (5 + 7).
Here, we can see that bit sequences can represent different things depending on the context. In the first instruction, the bit sequence 00000001 represented a value (the number 1), while in the third instruction, the same bit sequence represented a memory location (register 1). In the fourth instruction, the sequence 00000111 represented the operation »add number« and the number 7. Each type of CPU has different rules (instruction sets) on how to interpret bit sequences as instructions: it defines which sequences represent which operations, how many operands each operation has and how the operands are represented.
Binary numbers
One common way to interpret bit sequences is as binary numbers. Binary numbers are numbers represented in base 2. This means that each digit in a binary number can be either 0 or 1. This is different from the decimal system (base 10) that we use in everyday life, where each digit can be any number from 0 to 9. The same way the decimal number 1251 = 1Ă—103 + 2Ă—102 + 5Ă—101 + 1Ă—100 = 1Ă—1000 + 2Ă—100 + 5Ă—10 + 1Ă—1, the binary number 1011 = 1Ă—23 + 0Ă—22 + 1Ă—21 + 1Ă—20 = 1Ă—8 + 0Ă—4 + 1Ă—2 + 1Ă—1 = 11 in decimal. In the instructions above, the binary numbers 00000001, 00000100 and 00000111 represent the decimal numbers 1, 4 and 7 respectively.
Excercise
Convert the following 8-bit binary numbers to decimal numbers. We broke them into groups of two nibbles (4-bit sections) to make them easier to read:
Programming languages
Because different types of CPUs have different instruction sets, a program that runs on one type of computer might not run on another type of computer. This is why you need to install a different version of a program on your phone than on your laptop.
To make it easier to write programs that run on different types of computers, people have invented programming languages. Programming languages are also easier for humans to understand than machine code. Instead of using cryptic number combinations to represent instructions, programming languages use words and symbols that are more familiar to us. The instructions in a programming language are translated into machine code by a program called a compiler. The compiler takes the instructions you wrote and translates them into machine code that the CPU can understand. Often the same code in a programming language can be compiled to different types of machine code, making it possible to run the same program on different types of computers.