Category:Computer/zero Assembly

From Rosetta Code
This page is a stub. It needs more information! You can help Rosetta Code by filling it in!
Computer/zero Assembly
This programming language may be used to instruct a computer to perform a task.
See Also:

Listed below are all of the tasks on Rosetta Code which have been solved using Computer/zero Assembly.

Computer/zero is a simple instruction set architecture designed to accompany Programming degree zero, a short tutorial on computers and machine-level programming for the absolute beginner. It is deliberately minimal, offering only eight instructions, one general-purpose register, and 32 bytes of memory. As a result, it is not only easy to learn but also very easy to implement: the core interpreter for Computer/zero machine code comes to about 15 lines in a high-level language, making it quite feasible for beginners to implement and extend it for themselves. A partly graphical browser-based implementation (written in JavaScript) is available here.

Instruction Set

There are only 8 instructions to use. The top 3 bits represent the instruction, the bottom 5 are the operand. Operands represent absolute memory addresses only. Therefore, the only way to add or subtract a constant is to embed it in your program code (and doing so will consume an entire extra byte.) NOP and STP don't use their operands, so you can embed arbitrary data inside these instructions to save a little space.

  • NOP: 0b000xxxxx. This instruction does nothing.
  • LDA x: 0b001xxxxx. Loads the 8-bit value at memory address x into the accumulator.
  • STA x: 0b010xxxxx. Stores the 8-bit value in the accumulator into memory address x.
  • ADD x: 0b011xxxxx. Adds the value at memory address x to the value in the accumulator, and the result is stored in the accumulator.
  • SUB x: 0b100xxxxx. Subtracts the value at memory address x from the value in the accumulator. The result is stored in the accumulator.
  • BRZ x: 0b101xxxxx. Branch If Zero. If the accumulator contains the value 0, execution will jump to memory address x. Otherwise, this is the same as a NOP.
  • JMP x: 0b110xxxxx. Execution jumps to memory address x.
  • STP: 0b111xxxxx. Execution of code ends.


There are only 32 bytes of memory, and this includes your program code, leaving very little memory to work with. As such, there are no function calls or recursion, since both require a call stack and much more RAM than we have here.

Self-Modifying Code

With such little RAM available, the biggest tool we have at our disposal for programming is self-modifying code. The CPU makes no distinction between data and instructions, and as such we can store values into memory before that memory is executed, in order to change what the computer will do. Here's an example:

<lang 6502asm>LDA 4 STA 2 ADD 3 STP SUB 3</lang>

The above snippet of code never actually ADDs anything, since that instruction gets overwritten by SUB 3. And what is SUB 3 subtracting? It's subtracting the value of STP, which can be encoded as any number between 0xE0 and 0xFF. Specifically, the encoding of SUB 3 (i.e. 0b10000011) is decreased by some value between 0xE0 and 0xFF, and since all arithmetic on this CPU is modulo 256 (as is standard for nearly all 8-bit CPUs), there will be roll-over across the FF-00 boundary. Without a hardware carry flag, however, it's difficult to track this overflow.