Source: https://mokxi.com/learn/build-a-computer-from-nand-gates
Updated: 2026-09-28

Learn

# Build a computer from NAND gates

Written by the Mokxi team, updated September 28, 2026

Try it now, no account Sign up free or see every lesson

- 192 parts on the bench
- 25 boards running now
- 1.00x real time, on every board

Click A to count it up

Two number switches and a carry-in feed one ADDER8 chip made of 120 NAND gates.

The adder running above is 120 NAND gates and nothing else. Click the A or B switch while it runs and the display adds them. Nobody wired 120 gates by hand to make it: it was built one small chip at a time, each chip made of the ones before, the way this path teaches it.

Building a computer up from a single kind of gate is a well-known way to learn how computers work, and it is a good one, because nothing is left as magic. This is Mokxi’s own path through it: our lessons, our truth tables, our chip set and our own small CPU, the Kestrel. It runs in a browser tab, free, with no install and no account needed to start.

Every lesson is the same three moves. Build a circuit between switches and lamps. Test it against its truth table, on the real simulator, with every row marked. Package it into a chip with your name on it, and use that chip in the next lesson.

Want the step-by-step version? The lesson "Start the first lesson: the one gate" walks through this with checkpoints.
Open the lesson

## Why start from one gate

A NAND gate gives 0 only when both of its inputs are 1. That is the whole part. It turns out to be enough: tie its two inputs together and it becomes a NOT gate; put a NOT after it and it becomes AND; feed it two NOTs and it becomes OR. From those three you can build every circuit in a computer.

Starting there means every later chip is something you can open and follow all the way down. The adder above is two 4-bit adders, which are four full adders each, which are two half adders and an OR, which are XOR, AND and NOT, which are NAND gates. Select the adder in the editor and press Unpack to walk down a level at a time.

## Custom chips: build it once, use it everywhere

The part of Mokxi that makes this work is the chip. Build a circuit on the canvas with bit switches on its inputs and bit lamps on its outputs, press Make chip, and give it a name. The switches become the chip’s input pins and the lamps its output pins, named by their labels, and the circuit folds into one box you can place as many times as you like.

Chips go inside other chips, as deep as you need. A chip is saved with the project, travels with a share link, and can be kept in your blocks to use in any other project: signed in, your blocks follow you from one computer to another. To change a chip, press Unpack, fix the circuit, and make it again under the same name; every copy of it on the canvas updates.

When the circuit runs, the simulator opens every chip into the gates inside it. A chip costs nothing but its gates, so a chip made of chips simulates exactly as fast as the same gates wired flat.

## A truth table checks every step

Each lesson ends in a check that does not trust anybody’s eyes. Press Check and Mokxi builds its own copy of your circuit on the simulator, sets the inputs to every row of the table in turn, and reads the outputs. You get the whole table back with every row marked, and the first wrong row in a sentence: with A=0 and B=1 it should give Y=0, and it gives 1.

An output that reads unknown is told apart from a wrong one, because it usually means a wire that did not land. A table with up to ten input bits is tested in full; the 4-bit adder’s 512 rows all run. The 8-bit adder has 131,072 rows, so it gets every corner case and a fixed spread of the rest, the same rows every time.

Some steps say NAND gates only. The checker opens every chip in your circuit and looks, so a quiet AND gate borrowed from the parts panel is caught and named.

## The path, lesson by lesson

The one gate: meet NAND on two switches and a lamp. NOT from NAND: tie the inputs together and package your first chip. AND and OR: two chips, from NAND and your NOT. XOR from four NANDs, the heart of every adder.

Choosing with MUX and DEMUX: a select bit decides which signal goes where. Adding one bit: XOR and AND side by side make a half adder. Carrying: two half adders and an OR make a full adder. Four bits at once: four full adders in a ripple chain. An 8-bit adder: two 4-bit adders, 120 NAND gates in all.

Eight bits wide: an 8-bit MUX from eight of your MUX chips, adding one with constant bits, decoders from DEMUX chips, one bit of an ALU, and the 8-bit ALU that adds, subtracts, and does the logic jobs.

Memory: the set-reset latch from two NAND gates, the D latch, the flip-flop that moves only on the clock’s rising edge, a register bit and an 8-bit register, the program counter, eight bytes of RAM, and a register file of four registers. Their checks are sequences: the tester ticks the clock and checks what each step remembers.

The Kestrel: the control unit that decodes sixteen instructions, the CPU itself, tested by running a program on it, and your first computer: a step clock, a program ROM, your CPU and a terminal on one canvas, running a program you write.

About sixteen hours in all, in twenty-three lessons of twenty minutes to an hour. It suits a high school digital electronics unit, an intro computer architecture course, or anybody who has wondered what a processor is actually made of.

## The Kestrel, and your first computer

The path ends with the Kestrel, a small 8-bit CPU designed for it: four registers, eight bytes of RAM, a program counter and sixteen instructions, from LDI and ADD to JNZ. Every instruction takes one tick of the clock. It is about four thousand NAND gates, eight chips deep, and you build every one of those chips.

The last lesson puts it on a canvas with a step clock, a program ROM and a terminal. The ROM holds a program in Kestrel assembly, typed into its properties and assembled when you press Run. Run it at speed, or switch the clock to step and go one instruction a click, watching the program counter move and the terminal fill.

Then change the program. Print your name, count in the other direction, store a byte in RAM and read it back. It is a computer, and you built all of it from one kind of gate.

## Questions

Do I need to know electronics first?

No. The logic path uses ideal parts: switches, lamps and gates with no power supply or resistors to get wrong. If you can drag a wire from one dot to another you can start with the first lesson.

Is this the same as a university course on building a computer?

It follows the same well-known idea, from gates up to a computer, but the lessons, truth tables, chips and CPU here are Mokxi’s own. Nothing is copied from any book or course.

Can I use my own chips in other projects?

Yes. Check the box when you make a chip and it is kept in your blocks, which appear at the top of the part panel in every project. Signed in, they are saved to your account.

Can a teacher use this with a class?

Yes. Every lesson opens from a link with no sign-in, progress is kept per student, and each step is checked automatically. A class can hand in the finished adder, or the finished computer, like any other project.

How fast does a CPU made of gates run?

Fast enough to watch. The simulator runs every gate, so the Kestrel runs hundreds of instructions a second in a browser, and the step clock slows it to one instruction a click when you want to follow it.

Related

## Keep going

Logic Gates, and the Truth Table You Can Press Shifting a Byte In, One Edge at a Time The whole path, lesson by lesson The 8-bit adder project Your first computer, finished For teachers The project page, with the full sketch

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building Open the editor
