How Computers Work (Part 2): Hardware
Tags: computerscience, beginners, codenewbie
Published: 2020-10-30 13:56:00

## Intro 🛠️

In Part 1, we learned the basics of how computers store and process data.

In this part we'll look at the components that handle these operations: hardware. Specifically, the sort of hardware found in personal computers.

### Prerequisites

- [Part 1](https://dev.to/joshcarvel/how-computers-work-part-1-2o9l), or basic understanding of computing fundamentals such as binary, bits and logic gates

## The motherboard 👩

Inside a modern personal computer you will find many things, including:

- A power supply, batteries and cables to connect various parts
- Devices for cooling (since using electricity generates heat), usually a 'heat sink' and fan
- **Drives** (usually in desktop PCs) that accept discs or disks with stored data
- Internal storage (hard disk or SSD)
- (In laptops) built-in speakers

But most of the action takes place on the **motherboard,** i.e. the main circuit board of the computer, so that will be our focus here.

In early computers, different electrical components were just wired together in a complicated fashion. Two innovations changed this.

### Chips (integrated circuits)

The first is the **integrated circuit**, commonly known as a chip or microchip. This allows lots of tiny transistors to be connected on one small, flat piece (or 'chip') of silicon.

The type of chip seen today, which uses *photolithography* (using light to imprint circuit designs in silicon) was invented by Robert Noyce in 1959. In the early 1960s, an integrated circuit might hold around 5 transistors, but thanks to advances in science, the number of transistors that could fit doubled roughly every two years, known as 'Moore's law' after an engineer called Gordon Moore. 

In 1968, Moore and Noyce formed a company that became the world's largest chip manufacturer. Since it specialised in integrated electronics, they called it **Intel.** Today, Intel chips can hold *billions* of transistors.

### Printed circuit boards

Integrated circuits dramatically reduce the number of components needed, but the components still need to be wired together. **Printed circuit boards** **(PCBs)** helped with this. 

Wires (usually made from copper) that connect components are arranged in an intricate design for maximum efficiency, and placed on a board usually made of fibreglass (plastic reinforced with glass).

You can see the outline of these wires on a circuit board, but that's not all of them! The insulating material may have wires on *both* sides, and there may be multiple layers, like a sort of electrical lasagne, with connections being made vertically (via small holes) as well as horizontally on the surface.

Being able to put chips on PCBs revolutionised what computers could do. This is how we get to the modern motherboard.

### Motherboard components

The motherboard is a PCB which the main components of a computer are connected to. Here are some notable ones (not all of them):

- The **central processing unit (CPU)**, which connects to a socket on the motherboard and controls operations
- The **chipset,** which connects the CPU to the other components
- **RAM:** 'sticks' of memory (chips on a PCB) which connect into slots on the motherboard, and are the main place data is held while the computer runs
- **BIOS** chip, which stores the instructions the CPU uses to start up the computer
- **Ports,** which connect devices known as **peripherals** that provide some input (e.g. keyboard), output (e.g. monitor), or both (e.g. USB stick)
- **Expansion cards:** PCBs that connect to an expansion slot and enhance the functionality of the computer, for example a graphics card or sound card (used in desktop computers)

We'll focus on the CPU, because it's the most crucial to understand.

## The CPU 👑

Computer instructions need to be interpreted and carried out, i.e. *processed*. This mainly happens in the CPU. 

The CPU is a type of *processor*, of which there are others, such as the graphics processing unit (GPU). It's usually made using integrated circuits (usually a CPU is a single chip), in which case it's referred to as a *microprocessor*.

The CPU, quite simply, is in charge of the computer. It has a few main components which we'll talk about, though the actual implementation is dependent on the type of CPU chip.

### The control unit

The CPU is in charge of the computer, but the *control unit* is in charge of the CPU! It directs the processing of instructions. 

No matter how complicated computer instructions are, they always involve basic actions that the CPU can perform, such as:

- Getting data in and out of memory
- Mathematical and logical operations
- Inputting and outputting data from external devices such as a keyboard or monitor

Each of these instructions is given a code in binary, like how a particular video game action such as 'jump' is associated with a particular button. (Jump is actually a computer instruction too! We'll look at it shortly). 

This is an **opcode** (operation code). It is supplied along with the memory address of some data to do the operation on (called the **operand**).

These codes are not universal - it depends on the particular **instruction set architecture** (ISA) that the CPU chip has been built with, i.e. the way the circuits have been configured.

The most common series of ISAs was developed by Intel and is called **x86** architecture (because Intel produced a number of chips that used it whose name ended with 86).

#### RAM, caches & registers

To perform operations, the control unit works with **RAM**, a large bank of memory which is separate from the CPU but nearby on the motherboard, connected with wires. RAM connects to the motherboard via slots and you can add more to your computer. The most common type is called *dynamic RAM* (DRAM), and it's cheaper than the other main type, *static RAM* (SRAM).

The wires that transfer data on the motherboard are called **buses,** because they transport information. They are grouped together to carry bits, for example you might have 64 wires together to transport a 64-bit number.

The CPU is connected to RAM by an **address bus,** which the CPU uses to tell RAM the address of some data it wants to access. It is also connected by a **data bus,** through which data can be transferred back and forth.

For any given computer program, the data it wants to perform operations on and the instructions it wants the CPU to carry out are both stored in RAM. Each instruction contains an opcode and operand, together in a single binary number.

Sending data down wires is quick, but it still takes time, so location matters. For this reason, there is an area of memory *within* the CPU for temporarily holding values - the **registers.** Each register can hold a single value and like the buses, this is usually between 8 and 64 bits. 

The registers are used to hold data needed for operations, such as the current instruction. But there is also some memory kept for values that are not being used right now, but are likely to be needed soon: the **caches.** These are divided into 'levels' and there are often three of them. Level 1 cache, the closest and smallest, is usually on the CPU chip itself. The others are bigger but further away, though not as far as RAM.

#### The instruction cycle

Each instruction is *fetched* from RAM by the control unit, using the buses, one at a time. Each half of the instruction (opcode and operand) is read separately, so the control unit knows the action to perform, and it can get the data to perform it from RAM using the buses.

Once the control unit receives an instruction, it *decodes* it. This means it runs it through a circuit containing logic gates, which triggers binary values to be sent on a certain path of wires depending on what the binary opcode is. This is all just determined by the instruction set that has been hardwired into the chip.

Now the CPU is ready to *execute*, i.e. perform the operation. Again, this is a case of the CPU's circuits doing the job they have been wired to do - it's just the data they do it with that changes.

The whole cycle is known as the **instruction cycle** (also known as the fetch-decode-execute cycle or fetch-execute cycle). 

#### Optimisation

A simple CPU (as used in early digital computers) would wait to go through the whole cycle before moving on to the next instruction, but modern CPUs do multitasking, so when one instruction is being decoded, the next one is already being fetched, and so on.

In addition, since some operations take longer to perform, modern processors can split the circuitry that executes instructions into separate units specialised for different instructions (still on the same processor) and process them at the same time, allowing it to get more done quicker.

### The clock

The control unit directs the CPU, but it relies on the **clock** to function. 

The clock generates the consistent electrical pulses that are sent down the wires and received by transistors. It does this in the same way a digital watch keeps time - using a quartz crystal that vibrates at a fixed interval when electricity is applied.

The **clock rate** is the speed this happens, measured in *clock cycles per second*. A clock 'cycle' is *not* the same as the instruction cycle. 

In early computers, an instruction would actually take multiple clock cycles, because there are a few steps to carry out each instruction, each requiring a pulse of electricity. The process has been refined since then using optimisations such as those described above, so the opposite is now true - multiple instructions can be carried out per clock cycle.

This and many other factors affect how productive the computer actually is. But to keep things simple, the clock rate (also known as clock speed or frequency) is often used as a rough indication of how fast the computer can work. It's measured in *gigahertz*, i.e. *billion* cycles per second!

### The ALU

The component that makes operations possible is the **arithmetic and logic unit (ALU).** The ALU takes two pieces of data and returns one output. 

#### Maths

In Part 1 we looked at how logic gates are used with true and false values. Computers can do maths the same way, but we think of the values as 0 and 1 instead.

When it does maths, the ALU uses a type of logic gate called 'exclusive or', abbreviated to **XOR**. For the output to be 1, one of the inputs must be 1, and the other must be 0.

Here are the four possible variations and outcomes:

- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 0

We are essentially doing binary maths here! You may notice the sums all look correct except for the last one. But it's actually just the answer for the '1s' column - a remainder 1 is carried to the next column.

In this simple sum there would just be 0s in the '2s' column for both numbers being added. 0 + 0 = 0, plus the 1 remainder = 1 in the '2s' column. So the complete answer is 10, which is binary for two.

For this to work, computers have to be wired for the largest length of number they are expecting to deal with - they need the physical wires to represent each column, so the calculation can be performed. So a computer designed to deal with 8-bit numbers (like early computers), would represent the number 1 as 00000001.

Doing different types of calculations usually requires more logic gates, dealing with bigger numbers requires more wires, and there are some adjustments needed for negative numbers, but any mathematical operation can be achieved which this approach.

#### Logic

Let's look at an example of a logical operation now.

A common logical operation is for the control unit to 'jump' to a different instruction in the instruction list in RAM, *if* a certain condition is true. Imagine we have a simple 'guess the secret number' game, and the instructions tell the computer to keep prompting the user to guess again on the screen when a guess is made.

But, *if* the user's guess is *equal to* the secret number, the computer should jump to a different address in RAM to get an instruction for congratulating the user.

To do this, the ALU can subtract number A from number B, e.g. 5 - 5. If the result is 0, the numbers must be the same! (This would work the same way with words, which are just encoded as binary numbers).

In this case, we don't want the output of the calculation, we just want a true or false answer: is the result 0? ALUs have a wire specifically for this, along with some other useful indicator wires, which just output a 1 or 0: true or false. These are known as the *flags*.

So if the zero flag indicates true, the control unit can 'jump', by fetching a different instruction at the specified RAM address. This is just one example - all manner of logical instructions can be achieved using the ALU and its flags.

### Cores

For quite a while, computers did 'multitasking' in pretty much the same way you would - just switching back and forth between multiple tasks, and often completing each task slower than if you had spent all the time on it.

But if you had some other people to do the other tasks for you, you would get more done, and this is how modern CPUs tend to work. There are essentially multiple CPUs, known as **cores,** usually all on the same chip. Different sets of instructions can be carried out on different cores at the same time (as long as they don't depend on each other), allowing more work to be done quicker.

## Computer specifications 💻

We'll finish by looking at some key hardware specifications (specs). Hardware is a key factor in choosing a computer, especially if you are using the computer for programming.

Let's say you are choosing a laptop for typical programming, or you just want some solid performance. Here are three respectable specs to consider (as of time of writing):

 1\. <u>A mid-range CPU chip</u>

 CPU chips are named quite confusingly. Here is an example: the 'Intel Core i5-8250U'.

To find a respectable mid-range Intel chip, focus on three things:

- The brand: look for 'Intel Core', other brands are 'budget' chips
- The model number (i3, i5, i7 etc.): look for i5 or higher
- The 'generation': this number comes immediately after the dash following the model number, look for 7 or above

The suffix at the end is also informative, because it tells you what the processor is optimised for, e.g. the 'U' in the example above means it is optimised for saving battery power.

The 3 digits in between can be ignored as they are just for Intel's stock-keeping purposes, like a barcode. The naming convention is explained in more detail on [Intel's website](https://www.intel.ca/content/www/ca/en/processors/processor-numbers.html).

The main alternative to Intel is AMD, and the AMD Ryzen series competes with the Intel Core. The model numbers match, so AMD Ryzen 5 is roughly equivalent to i5, and there are fewer generations, so 2nd generation is fine. Here is some [further explanation](https://www.techconsumerguide.com/a-simple-guide-to-amd-ryzen-naming-scheme/) of their naming.

Though you don't need to, if you look in more detail at the specs of a chip, you will see the number of cores that it has (our example chip has 4) and the 'base' (usual) clock speed (1.60GHz) and a 'max' clock speed that it *might* be able to reach if it needs to (3.40GHz in our example).

While I have given a rough guide here, it is worthwhile doing further research on this, because exactly how you use your computer and what your priorities are will greatly affect the type of CPU you should look for.

 2\. <u>8GB RAM or more</u>

 RAM is a much simpler matter. You will want 8GB unless you are on a very tight budget, and 16GB if you are doing a lot of intensive work or gaming etc. You might need to stretch to 32GB but usually not.


 3\. <u>A solid-state drive (SSD)</u> 

We mentioned this in Part 1 of this series. These allow much faster access to the stored data, although you get less storage for the price.

Aside from these three specs, there are many other factors to consider. For the screen you want at least *full HD* resolution (1920x1080 pixels) and an *IPS* panel, not TN (the ones where the colours look weird when you change the screen angle). Then you have battery life, ports, weight, and more to consider.

## Conclusion

That concludes our look at hardware. In the final part of this series we'll look at the world of operating systems and software 😀. First, a brief recap:

- The key component of a computer is the motherboard, a printed circuit board containing the computer's main memory and components, including the CPU.
- The CPU is directed by the control unit, driven by the clock, and performs operations on data from RAM using the ALU.
- The type of CPU, amount and type of memory/storage and other factors have a big impact on the performance of the computer.

## Sources

I cross-reference my sources as much as possible. If you think some information in this article is incorrect, please leave a polite comment or message me with supporting evidence 🙂.

\* = particularly recommended for further study

- \* [Crash Course Computer Science: Episodes 5, 7 and 17](https://www.youtube.com/playlist?list=PLH2l6uzC4UEW0s7-KewFLBC1D0l6XRfye)

- \* [See How a CPU Works - In One Lesson](https://www.youtube.com/watch?v=cNN_tTXABUA)
- [How Motherboards Work - howstuffworks](https://computer.howstuffworks.com/motherboard.htm)
- [What Does the Inside of Your PC Look Like? - Lifewire](https://www.lifewire.com/tour-inside-a-desktop-pc-2624588)

- [Instructions Per Cycle – Gary explains - Android Authority](https://www.androidauthority.com/instructions-per-cycle-gary-explains-705127/)
- [Why You Can’t Use CPU Clock Speed to Compare Computer Performance - How-To Geek](https://www.howtogeek.com/177790/why-you-cant-use-cpu-clock-speed-to-compare-computer-performance/)
- [Computer hardware - Wikipedia](https://en.wikipedia.org/wiki/Computer_hardware)