insature

18 August 2026 · note

Computer-Architecture

How to build a computer.

This note is based on a 50-hour course I gave in the summer of 2026 to a group of high-school students in Germany, which in turn is based on the excellent Nand2Tetris.

Hardware

If we strip everything non-essential away, a Computer is an object that does two things: it computes stuff, and it talks to storage (both for reading and writing). The computing is done in the CPU (Central Processing Unit), the storing in the RAM (Random Access Memory).1

Memory (which, next to the RAM, is also used in the CPU for storing temporary results) consists of billions of individual storage cells (bits) that can store, and potentially overwrite, one bit of information. We refer to a word of 16 bits as a register. An individual bit can be (for example) built out of logic gates, in the form of a D flip flop.

The CPU architecture is a design choice (the one by Nisan & Shocken is called Hack). The most important part in the CPU is the ALU (Arithmetic Logic Unit), which performs the actual computations. There are lots of knobs to twist and turn: to decide in every cycle what is computed, where the inputs are taken from, where the result is stored, where to go next in the program and so on. The pile of bits that selects all the right operations is the machine code of that instruction. A program is a collection of machine code instructions, which in the Hack architecture are stored in a piece of read-only memory (ROM).

In/Out

A quick note on interacting with the computer: we abstract display, keyboard etc. away through the concept of a memory map: all these peripherals just correspond to certain areas in the RAM. Writing to the right addresses has, for example, the effect of drawing onto the screen. Reading from the memory-mapped addresses could get the value of a pixel, the current key pressed or various other information from the peripherals. We delegate the implementation of the memory map to the hardware providers.

Software

The second task is to translate machine code into something human-readable (or, rather, vice-versa). In this project, the final destination was a Java-like programming language called Jack. The process is broken down into two steps: going from Jack to a virtual machine (VM) and going from the VM to binary code.

The virtual machine is based on a stack architecture. Parameters can be pushed from and popped into various memory segments (for example, static for static variables or this and that pointers inside of the heap for dynamically allocated variables and objects). In the following example, the first argument of a function contains the pointer to an object, whose third property is a number that should be changed to 42.

push ARGUMENT 0 // get object pointer from first argument
pop POINTER 1 // set the base address of the `THAT` segment
push CONSTANT 42 // `CONSTANT` is a virtual segment that can only be pushed from
pop THAT 2 // write into third property of object

The VM also allows for function definitions and calls. Whenever a function is called, it pushes a new stack frame on top of the stack (this contains the addresses of the previous ARGUMENT, LOCAL, THIS, THAT segments as well as the return address in the ROM). After all these commands are correctly translated into Assembly (which is, essentially, a shorthand notation for machine code) the VM provides a fully functional programming language.

Going from the virtual machine to Jack brings about all the amenities of modern programming languages: object orientation, loops, if-statements and so on. The Jack-to-VM compiler is split into a tokenizer, which parses an input file into a stream of valid language tokens, and a compilation engine, which makes sure the tokens constitute valid sentences within the programming language and subsequently writes the corresponding VM code.

Operating System

A special piece of software is the operating system. While in a real computer the OS is responsible for drivers, privileges and users, the running of multiple processes in parallel and more, the Hack OS provides only a small standard library of Jack functions. This includes drivers for interacting with keyboard and screen as well as essential maths commands, functionality for Arrays and Strings and basic memory management.

References

Footnotes

  1. We will ignore any other form of long-term storage for now.

Back to notes