esolang

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

Expected: 2012

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

ByteDoes
0nLDA, set A to n
1n 2nSkip the next instruction if A is n, or is not
3nDecrement A and skip if it is then n
4n 5nStore A in the low digit of the byte n ahead or behind
6n 7nStore A in the high digit of the byte n ahead or behind
8n 9nJump n forward or back
An BnJump n forward or back if A is 0
Cn DnLoad A from the low digit of the byte n ahead or behind
F0 FFHalt
F1 F6 F7Invert, increment, decrement A
F2 F3Skip if the carry is set, or clear
F4 F5 FERead a key into A, print A, print a line feed
F8 F9Jump A forward or back
FA FBPush or pop A
FC FDUnused, 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.

Expected: 7
Expected: 0

Twelve, colon, thirty, from five loads and five prints.

Expected: 12:30

Patterns it teaches

Further

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-computer

Source

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