Nibble puzzles

Sandbox

Nibble is the language of a tiny machine. It has one register, AX, which holds 0, 1, 2 or 3, and a program is exactly six instructions. A run starts with AX set to the input. It accepts when zero finds AX at 0, and it rejects when decr finds AX at 0 or when it runs off the end. Each puzzle names a set of inputs: write a program that accepts exactly those and rejects the rest. The first four puzzles teach you Nibble, one opcode at a time.

How does a program recognize a "language"?

Here a language is simply a set of inputs: some of the numbers 0, 1, 2 and 3. There are 16 such sets, from none of them to all four. A program recognizes a language when it accepts exactly the inputs in the set and rejects every other input.

To find out which language a program recognizes, the machine runs it four times, once for each input. Each run starts with AX holding that input and goes down the six lines in order. A run ends in one of three ways:

  • zero finds AX at 0: the run accepts;
  • decr finds AX at 0: the run rejects;
  • the run gets past line 6 without either: it rejects.

Four runs give four verdicts, and the inputs that were accepted are the program's language. For example, take the program decr, zero (the rest nada):

  • input 0: decr finds AX at 0, so it rejects;
  • input 1: decr makes AX 0, then zero finds 0, so it accepts;
  • input 2: decr makes AX 1, zero lets it pass, and the run falls off the end: reject;
  • input 3: AX becomes 2, and the same: reject.

Only 1 is accepted, so decr, zero recognizes the language {1}, which is puzzle 2. The trace under each puzzle shows exactly these four runs, one column per input.

Every one of the 4,096 possible programs recognizes exactly one of the 16 languages, and every language has at least one program; a puzzle asks you to find one. This is how computer science talks about problems: a problem is a set of inputs, and a machine solves it by saying yes to exactly those. The book about this playground follows that idea to the Cook–Levin theorem.

Loading the machine...