Coding Essentials Guidebook for Developers: Basic Computer Architecture
This post is a free extract from Chapter 1 of the Coding Essentials Guidebook for Developers. If you get value out of it, support us by purchasing it here. Subscribe with your email address if you'd like to stay up to date with new content from Initial Commit.
Basic Computer Architecture
The way code gets executed depends on the hardware it runs on. Therefore, it is important to understand how computer architecture works at a basic level. This section is a quick primer to the key parts of a computer. It is by no means a comprehensive study of how computers work. It is intended to provide some essential background information that will help build a foundation for the later chapters in this book. Keep in mind that the details in this chapter describe how some of the inner parts of computers work. You probably won't need to directly interact with these directly while coding. Modern operating systems like Mac OS, Linux, and Windows handle the complex interactions with the components under the hood, and allow us as developers to focus on the code. That being said, a basic understanding of the underlying components will help clarify certain programming concepts going forward.
Circuit Boards, Microchips, and Transistors
If you open up the body of your laptop or desktop computer, you will surely see several flattish green rectangles with some complex-looking electronics stuck to them. Each one of these is called a circuit board and houses an internal part (or multiple parts) of your computer. On each circuit board, you will likely see several raised, black squares or rectangles. These are called microchips. A microchip is a self-contained set of circuitry that serves a specific electronic or computing purpose. For this reason, microchips are also referred to as integrated circuits.
If you look closely at a circuit board, you will notice small "tracks" etched into it. These are essentially built-in wires that allow electricity to flow between the different parts of the board. Here is an image of a typical circuit board:
Figure 1.1: Typical Circuit Board
Microchips rely on an electrical component called a transistor to function. A single microchip can contain millions or billions of tiny transistors embedded on it. The primary function of a transistor on a microchip is to act as an electrical switch. When the switch is off, we can say the transistor stores a value of 0. When the switch is on, we can say the transistor stores a value of 1. Later in this chapter, we will see how our data can be converted into zeros and ones that microchips in our computers can understand.
CPU - The Computer's Brain
Most modern computers have a microchip called the CPU, or Central Processing Unit. This can be thought of as the computer's brain. It performs the arithmetic, logic, and control operations that enable the computer to do things. CPUs have something called an instruction set. This is a list of commands (or instructions) that the CPU can execute. Examples of these instructions include copying data to a location the CPU can access, performing arithmetic operations (addition, subtraction, multiplication, and division) on numbers, comparing the size of two numbers, outputting the result of its operations, and more.
A CPU's instruction set is hard-coded on the chip via embedded circuitry. Each instruction is represented by a sequence of ones and zeros, also known as a binary value (more on binary later in this chapter). These instructions dictate all of the operations the computer can perform. Many instructions take one of more binary values as input, perform a calculation using those values, and return the result as output. Although each operation is very basic, many of them can be executed very quickly. The vast number of these basic instructions that can be processed in short periods of time is what enables computers to perform complex calculations, send emails, play games, and run our favorite programs.
It should be noted that the described operations take place at a very low level within the computer hardware and that today's operating systems rely on this basic concept. Users don't typically interact with CPU instructions or binary data directly but instead are presented with applications that talk to the lower level parts. This fundamental detail is how almost all modern computers, regardless of vendor, (Mac or PC), operate.
RAM - The Computer's Working Memory
Think of RAM, or Random Access Memory, as the information you hold in your mind when you're actively thinking about something. This could be called your working memory. When you fall asleep, the conscious processing of that memory or thought process is gone. Similarly, computer RAM is used to store information that is actively being used by the computer to do things while it is up and running.
Like the CPU, RAM lives on a microchip and works with data in a binary format. RAM chips contain millions of electronic switches that can each store a 0 or a 1. These switches need to be organized in some way so that the computer knows where to access the data it needs at any given time. This is accomplished by labelling each switch with an address that can be used to access the value of the switch. You can think of each RAM address as a pointer to a particular location in memory which stores either a 0 or a 1. When a program is launched on the computer, the CPU delegates a subset of RAM addresses for that program to use. This is called an address space. Running programs use these addresses to store the data and instructions that enable them to operate. RAM addresses are used to store user data like numbers and text characters, sequences of CPU instructions for the CPU to execute, information about input and output devices like keyboards and printers, and even other RAM addresses.
When a program is closed or shut down, the RAM it was using is released, or freed up for other programs to use. The memory location is marked as available, but the system doesn't actively go out and overwrite the contents. Therefore, the data stored in that program's RAM address space typically hangs around until it is overwritten by another program. This happens after the CPU delegates that same block of RAM (or part of it) to a different program, which eventually overwrites it. When the computer is restarted or turned off, all data stored in RAM is lost. For this reason, RAM is referred to as volatile memory which means that the data contents are lost when power is removed from the device.
Hard Drive - The Computer's Long-Term Memory
A hard drive can be equated with a human's long-term memory. A hard drive is an internal or external device that stores data that should persist even after the computer is turned off. Most computers ship with a built-in hard drive, but external drives can be added to expand storage capacity or to back-up data. Data on hard drives is also stored in binary format. Without hard drives, all of our data would be lost each time we turned off the computer. Hard drives provide the means of storing data for later use.
Data from the hard drive is loaded into RAM when programs are started and when running programs needs access stored data. When an executable program first starts, the binary instructions that describe how the programs works are loaded into RAM. While the program is running, it may pull data it needs into RAM for faster access. The CPU executes a read instruction which loads the requested data from the hard drive to a specified address space in RAM. When you save information to a hard drive, the CPU executes a write instruction which saves the specified data from RAM onto the hard drive.
To continue reading, purchase the Coding Essentials Guidebook for Developers here.
Recommended product: Coding Essentials Guidebook for Developers