esolang

miscmisc2

Seven instructions in a row of integers. The program sits in memory, and its first cell says where to run next.

Transoptimal · Turing complete: unknown · ●●○ moderate · esolangs.org

Tested with 5 known programs · Test method

Run it

Expected: Hello world!

The idea

Transoptimal made miscmisc2 to be a low-level language that is not too awkward to use and still has a small set of instructions. Memory is a row of integers from cell 0 onwards. The program is written into it from cell 0, and cell 0 itself is the pointer P. So the first number of a program says where it starts, and a jump is a write to cell 0.

Read the story

Each instruction is a number, picked as the code of a character. ! is 33, & 38, + 43, - 45, ? 63, . 46 and , 44. Every other number does nothing.

The machine

A, B and C are the three cells after P.

InstructionDoesP moves
! 33Write A into the cell B points to3
& 38Copy the cell A points to into the cell B points to3
+ 43Write A plus B into C4
- 45Write A minus B into C4
? 63Jump to B if A is above 0, to C if A is below 04
. 46Read the next input into A2
, 44Print A2

P moves on only when the instruction left it as it was. The machine halts when P is below 1 or every cell from P on is 0. The specification leaves input and output open. In this implementation input is bytes, the end of the input reads as 0, and , prints the character with A's code.

Example

Hello world.

Hello world. P starts at 1, and each 44 prints the number after it.

The specification also writes it with characters for the numbers. A token of one character other than a digit is read as its code.

Expected: Hello world!

The cat reads a byte into cell 2. Two & copy it into the ? and into the , further on, so the program rewrites its own operands. When the byte is above 0 it is printed, and ! 1 0 writes 1 into cell 0, which jumps back to the start. The end of the input reads as 0, and then ! 0 0 sets P to 0, which halts.

Expected: cat

The truth machine works the same way. On the number 0 it prints 0 once and halts; on 1 it prints 1 forever.

Patterns it teaches

  • Self-modifying code: & copies values into the operands of later instructions.
  • Code as data: the program and its variables share one memory, and the pointer is a cell of it.

Further

Cell-based

Write your own in Run Convert a program from miscmisc2

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: miscmisc2.

Command line

shib run miscmisc2 -e '1 46 0 38 2 10 38 2 12 44 0 63 0 9 0' --input ''
shib manual miscmisc2

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/miscmisc2.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org