Skip to content

Computer architecture

~12 min

In short

Computer architecture explains how a processor, its memory, and its instructions fit together. You do not need to design a chip to benefit from it. Knowing how memory is organised and how instructions run helps you read performance problems, understand why some code is slow, and make sense of what your operating system does.

What the subject is about

A computer is built from simple parts. Logic gates combine bits (0 and 1) into arithmetic and decisions. The arithmetic logic unit (ALU) does the calculations. Registers hold a few values close to the processor. Main memory holds much more, but it is slower to reach. Caches sit between the two and keep recently used data close to the processor.

A program becomes a sequence of machine instructions. The processor repeatedly fetches an instruction, decodes it, and executes it. Modern processors overlap these steps in a pipeline and guess which way a branch will go. These details explain why two programs with the same logic can differ in speed by a large factor.

The subject also covers how numbers are represented in binary, including negative numbers and floating-point values. Many surprising results in programming come from these representations.

What to learn first

  1. Binary and hexadecimal numbers, and converting between them.
  2. Two's complement for negative integers.
  3. Logic gates (AND, OR, NOT) and how they combine into adders.
  4. The fetch, decode, and execute cycle.
  5. Registers, the program counter, and memory addresses.
  6. Assembly-level view of a simple loop and a function call.
  7. The memory hierarchy: registers, caches, main memory, disk.
  8. Why cache hits and misses change the speed of code.
  9. Pipelining at the level of the idea, not the circuit details.
  10. Floating-point representation and why 0.1 + 0.2 is not exactly 0.3.

The Python course does not teach assembly, but ../../python/01-getting-started/04-variables-and-types.md covers the types that these representations support. For the performance side, the complexity notes in ../../python/06-codeforces-patterns/02-complexity-and-pypy.md are a useful companion.

What you can skip at first

  • Circuit design with transistors. It is part of electrical engineering courses and is not needed for software work at the start.
  • Writing large programs in assembly. Read a small amount so you understand what the compiler produces.
  • Vendor-specific instruction sets and their extensions.
  • Detailed cache-coherence protocols for multi-core systems. Learn that caches must agree, and return later for the rules.

How it connects to the other subjects

The memory hierarchy is the reason data structure choice matters beyond Big-O. A contiguous array is often faster to scan than a linked list with the same number of items, because of how caches load memory. See data structures and algorithms.

Operating systems use the hardware's privilege levels, interrupts, and virtual memory. You cannot understand a system call without knowing what the processor does when it is invoked. See operating systems.

Databases depend on disk and memory speed. Buffer pools and page sizes make more sense once you know the hierarchy. See databases.

Free resources

  • MIT 6.004, Computation Structures (https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/). Lectures and materials from MIT OpenCourseWare that start from logic gates and build up to processors. It suits someone who wants the full hardware path.
  • The Elements of Computing Systems, nand2tetris (https://www.nand2tetris.org/). You build a computer from NAND gates upward, one project at a time. It suits a hands-on learner who wants to see every layer work.
  • Build an 8-bit computer, Ben Eater (https://eater.net/8bit). A build-along series that wires a simple computer from logic chips on a breadboard. It suits someone who learns best by seeing the physical parts work.
  • Computer Systems: A Programmer's Perspective, Bryant and O'Hallaron (https://csapp.cs.cmu.edu/). The companion site for a standard textbook on how programs meet the machine. It suits a student who codes in C or wants to understand performance deeply.

Practice

  1. Convert 183 to binary and to hexadecimal by hand. Check your answers with Python's bin and hex functions.
  2. Show, with a small calculation, how an 8-bit two's complement number represents -5. Then check it with int operations in Python.
  3. Write a short Python program that prints 0.1 + 0.2 == 0.3 and explain the result in two sentences.
  4. Write two loops that sum a large two-dimensional list, one going row by row and one column by column. Time both and explain the difference using the idea of memory layout.
  5. Draw the fetch, decode, and execute steps for the instruction ADD R1, R2 on a single sheet of paper, labelling where each value is stored.

Sources

  • MIT OpenCourseWare, 6.004 Computation Structures, https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/
  • nand2tetris, The Elements of Computing Systems, https://www.nand2tetris.org/
  • Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, companion site, https://csapp.cs.cmu.edu/
  • Ben Eater, Build an 8-bit computer, https://eater.net/8bit