Bits & Code

How Do Computers Work? An Introduction to Computer Engineering

How Do Computers Work? An Introduction to Computer Engineering

Computers are everywhere: personal computers, smartphones, smartwatches, and even in the car and dishwasher.

To become a good programmer, it's crucial to know how computers work by understanding the building blocks of a computer.

The first electronic general-purpose computer was ENIAC (Electronic Numerical Integrator and Computer). It was reprogrammable, which made it possible to solve many numerical problems.

Technology has evolved much since the ENIAC. Computers are becoming more powerful. Furthermore, computers are becoming smaller. Only during the last ten years have we seen incredible evolvement of personal computers. I'm writing this on my Lenovo ThinkPad X230 from 2012, which would be considered "bulky" by today's standards.

Also, new types of devices are becoming popular. Tablets and smartwatches, to name a few. With the Internet of Things around the corner, computers are becoming more common.

In this tutorial, you'll learn what a computer is, how a computer works, as well as how to program a computer.

Table of Contents

  1. What Is a Computer?
  2. I/O Devices
  3. Memory
  4. Datapath
    4.1 Arithmethic Logic Unit
    4.2 Registers
    4.3 Putting the Datapath Together
  5. Controller
  6. Programming the Computer
  7. What Is an Operating System?

What Is a Computer?

A computer is simply a computing machine which performs arithmetic and logic operations. The components of a computer are

The datapath and controller can be combined into what is called the processor.

Let's take a closer look at these components.

I/O Devices

Examples of Input/Output devices are keyboards and screens.

Memory

The memory stores the program and all data. There are many different kinds of memories, for example, RAM (Random Access Memory), ROM (Read Only Memory), hard drive, and CD.

Datapath

The datapath performs arithmetic and logic operations.

A simple datapath consists of an Arithmetic Logic Unit (ALU), registers and busses.

Arithmetic Logic Unit

It will come as no surprise that the ALU performs all arithmetic and logic operations in the processor.

Arithmetic Logic Unit
Jim Lamberson / Wikimedia Commons

The ALU takes two data inputs (A and B), operation code input (opcode), and option input (status).

The opcode consists of N bits, meaning there are 2^N different functions. Every function performs a calculation. Some of the most common ALU operations are

It's possible to perform subtraction with some extra logic.

A subtraction can be expressed as addition since A - B = A + (-B) = A + B_1comp + 1 where B_1comp = bitwise not B.

Registers

Registers are memory units consisting of flip-flips.

Consider a NAND-latch.

The circuit "latches" the value created by the input signal and holds it until another signal changes it.

The latch is asynchronous, which means that its output changes as soon as its input changes.

Consider a D flip-flop derived from the NAND-latch.

The flip-flop is synchronous; its output only changes on the rising edge of the clock.

To make a N-bit register, we use N flip-flops.

Putting the Datapath Together

To transfer values between registers and the ALU, the components must be connected by a bus.

Since we can't put multiple values on the bus at the same time, we need to add an "output enable"-signal to each register. To make sure we don't overwrite a value stored in the register, we also add a "load enable"-signal.

Controller

The controller orchestrates the datapath, memory, and I/O devices by activating control signals according to the instruction of the program.

Programming the Computer

A computer runs a program, which is a set of instructions. The processor performs the instructions -- which are stored in the memory as machine code -- one at a time.

For example, a program that adds two integers in different registers together looks like this in MIPS machine code:

00000001010010110100100000100000

Pretty hard to read, right?

Machine code is not meant for humans to read -- it's the computer's language.

It's entirely possible to program a computer by writing machine code, but it's time-consuming. That's why low-level computer programming is done in a language that is called Assembly.

The code above looks like this in MIPS Assembly:

add t1, t2, t3

In assembly, every line of code is an instruction. The code above is an instruction to add the values in registers t2 and t3 together, and place the result in t1.

Let's take a look at a little longer assembly program...

Assembly listing for a Motorola 6800 8-bit microprocessor
Figure 1. An assembly language listing for a Motorola 6800 8-bit microprocessor. (Public domain)

Figure 1 shows an assembly language listing from a "monitor" program for a Motorola 6800 8-bit microprocessor.

Programming in assembly is necessary when performance is critical for an application, for example, in scientific computing.

However, sometimes abstraction is needed to solve a specific problem. Let me introduce C, a higher-level language.

Say that we want to add two integers and return the result. In C, it looks like this:

result = a + b

What would in assembly be a constant moving of values between registers is in C, just a simple expression. We've made an abstraction by saying, "We don't care in what registers the values are stored, we are just interested in computing the result."

To be able to run our C code on an computer, we need to compile it.

What is an Operating System?

More complex computer systems may use an operating system to manage the hardware and software resources. Some common operating systems in use today include Microsoft Windows, macOS and iOS by Apple, various Linux distributions, and Android.