Bug Computer
A teaching computer with a 4-bit accumulator, 256 bytes of memory and jumps that are off by one.
Function call without parameters, 2012 · Turing complete: no · ●●○ moderate · esolangs.org
Tested with 5 known programs · Test method
Run it
The idea
The Bug Computer is a small machine invented in 2012 for teaching. The bug in its name is the program counter, the way simple teaching machines picture it, not a software bug. It has 256 bytes of memory, a 4-bit accumulator A, a carry flag and a stack of 4-bit values.
Read the story
Every instruction is one byte, an opcode digit and an operand digit. A program is written as its bytes in hex. It can change itself, as STO writes into the low digit of a byte and OPC into the high digit, the opcode.
The specification lists the reference interpreter's quirks. Every jump counts from the next instruction, so JMP +0 goes on as normal. The opcode FC was left unused, most probably for a random value.
The machine
| Byte | Does |
|---|---|
0n | LDA, set A to n |
1n 2n | Skip the next instruction if A is n, or is not |
3n | Decrement A and skip if it is then n |
4n 5n | Store A in the low digit of the byte n ahead or behind |
6n 7n | Store A in the high digit of the byte n ahead or behind |
8n 9n | Jump n forward or back |
An Bn | Jump n forward or back if A is 0 |
Cn Dn | Load A from the low digit of the byte n ahead or behind |
F0 FF | Halt |
F1 F6 F7 | Invert, increment, decrement A |
F2 F3 | Skip if the carry is set, or clear |
F4 F5 FE | Read a key into A, print A, print a line feed |
F8 F9 | Jump A forward or back |
FA FB | Push or pop A |
FC FD | Unused, no operation |
A digit above 9 prints as one of six symbols, space + : / - . for 10 to 15, so a program can show a time or a date. Reading takes the same symbols, the letters A to F, and Enter as 10.
Example
The cat prints each key until a 10 comes, a space or Enter.
The cat prints each key until a 10 comes, a space or Enter. At address 3, 94 jumps 4 back from address 4 and lands on the first instruction.
The adder keeps the two numbers just past its own code. It adds 1 to the first and takes 1 from the second until the second is 0. A is 4 bits, so 9 and 7 give 0.
Twelve, colon, thirty, from five loads and five prints.
Patterns it teaches
- Self-modifying code:
OPCwrites a new opcode into the program while it runs.
Further
See also
Educational
Write your own in Run Convert a program from Bug Computer
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: bug-computer.
Command line
shib run bug-computer -e 'F4 F5 1A 94 FF' --input '42
'shib manual bug-computerSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/bug-computer.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org