Bug Computer

From Esolang
Jump to navigation Jump to search

The Bug Computer is a simple, educational computer architecture invented by User:Function call without parameters in 2012.

Naming

The Bug Computer, although it may seem reasonable, was not named after software bugs well-known by all of us, but rather the bug as a metaphor for the program counter originating from primitive educational computer architectures of similar complexity.

Architecture

The Bug Computer reference implementation utilizes a memory consisting of 256 bytes (although it could be extended arbitrarily in other implementations). It also has a (theoretically unbounded) stack which stores nybbles of data.

The system also has two registers, PC and A, and a flag, CF. PC is the program counter and is 8 bits (or different according to the memory size), A is the accumulator (4 bits) and CF is the carry flag.

Instruction set

The instruction set is the following:

Mnemonic Name Hex Function
LDA n Load 0n Sets A to the value of n.
SE n Skip if equal to 1n Skips the following instruction if A = n.
SNE n Skip if not equal to 2n Skips the following instruction if A ≠ n.
DSE n Decrement and skip if equal to 3n Decrements A (with wrap-around) and skips the following instruction if A = n.
STO +n Store 4n Stores the value of A into the lower 4 bits of the byte n cells forward in memory.
STO −n Store 5n Stores the value of A into the lower 4 bits of the byte n cells backward in memory.
OPC +n Modify opcode 6n Stores the value of A into the upper 4 bits of the byte n cells forward in memory.
OPC −n Modify opcode 7n Stores the value of A into the upper 4 bits of the byte n cells backward in memory.
JMP +n Jump 8n Jumps n bytes forward.
JMP −n Jump 9n Jumps n bytes backward.
JZ +n Jump if zero An Jumps n bytes forward if A = 0.
JZ −n Jump if zero Bn Jumps n bytes backward if A = 0.
RCL +n Recall Cn Recalls the lower 4 bits of the byte n cells forward in memory into A.
RCL −n Recall Dn Recalls the lower 4 bits of the byte n cells backward in memory into A.
HLT Halt F0 Halts execution of program.
NOT A Not F1 Negates the bits of A.
SC Skip if carry F2 Skips the following instruction if CF is set.
SNC Skip if no carry F3 Skips the following instruction if CF is clear.
INP A Input F4 Waits for user input until a suitable keystroke (see below) is received and stores it in A
OUT A Output F5 Outputs the value of A to the screen.
INC A Increment F6 Increments the value of A by one (with wrap-around).
DEC A Decrement F7 Decrements the value of A by one (with wrap-around).
JMP +A Jump (conditional) F8 Jumps forward the number of bytes which is stored in A.
JMP −A Jump (conditional) F9 Jumps backward the number of bytes which is stored in A.
PUSH A Push FA Pushes the value of A to the stack.
POP A Pop FB Pops a value from the stack and stores it in A.
none none FC This opcode was left unused (see below).
NOP No operation FD Does nothing.
OUT NL Output new-line FE Outputs a new-line to the screen.
BRK Break FF Stops program execution and quits interpreter.

Character set

The system does not use the standard hex alphabet (digits 0–9 and letters A–F) and instead defines a character set which is moderately useful in terms of user communication. In accordance with this, the reference interpreter accepts several different keystrokes for each of the values 10–15.

The characters are coded as per the following:

Value Reference symbol Acceptable keystrokes Notes
10 (Ah) Space A, Space, Enter
11 (Bh) ≡ or + B, =, +, *, # + is the preferred symbol; ≡ is for 7-segment displays
12 (Ch) : C, : (colon) Useful for displaying time
13 (Dh) / D, / Useful for displaying dates or fractions
14 (Eh) E, − (dash) Useful for negative numbers or as a separator
15 (Fh) . or , F, . (dot), , (comma) Decimal sign; should be displayed according to regional preference but . (dot) is preferred

Example program

Bytecode for a cat program on Bug Computer (in hex):

F4 F5 1A 94 FF

(terminates if a value of 10 (Ah) is received)


Bytecode for a program which adds two numbers and displays the result:

F4 4F F4 4E CC F6 4A CA F7 48 10 98 C4 F5 FF

Quirks and bugs

The (reference implementation of the) Bug Computer (although not named after them) has a number of quirks and what some may term “bugs”. I’ve also made a few strange decisions during design, especially because I was 4 years younger then and lacking any formal education on the subject. By the way, I’m now planning to release a similar but more reasonably-designed system which I may call the Feature Computer (pun intended).

For example, a bug is to be found in the jump routine in which jump addresses are off by one, a value of 0 (instead of 1) representing normal program flow.

Also a strange thing that I’ve reserved one opcode of arity 0, FC hex, most probably for an instruction which would have put a random value into A, allowing some nondeterminism in the code.