esolang

C-Hex

Every instruction is a numbered control code in brackets, and you have to remember what each number does.

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

Tested with 2 known programs · Test method

Run it

Expected: 00

The idea

C-Hex comes from 2020, after the control codes of EarthBound ROM hacks, where [01] might set a flag. A program is a set of labels, and each label is a list of codes such as [04 00 $01 01]. Everything is hex. The specification calls it purposely horrible and a memory test, because the codes do not agree on how they work. The [00] family always prints the current cell, while [04] math needs every cell spelled out with $.

Read the story

The only error message is >:(.

The machine

A tape of 128 cells unless {tape_length: 0xNN} says otherwise. The program runs <main> up to its <end>. A value is hex, or $NN for the value of cell NN. Text in parentheses is a comment.

CodeDoes
[00 00] [00 01] [00 02]Print the current cell as hex, as a character, as a number
[01 V] [05 V]Set the current cell; move to cell V
[02 00 L] [02 01 L]Call label L; go to it and never come back
[03] [0A]Read a line into the cells; read a number into the current cell
[04 OP A B]Put A op B in the current cell, op 00 to 05 for + - * / ^ %
[06 OP A B L]Call L if the comparison holds, op 00 to 06 for == > < != <= >= %
[07 OP A B L E]The same, calling E when it does not
[08 V L] [09 OP A B L]Call L V times; call L while the comparison holds
[FF FF 00] [FF FF 01] [FF FF 02]Stop this label, this one and its caller, or all but main

A stopped label also ends the loop that called it, which is how the specification breaks out of a loop that would run forever.

Example

The call example prints the cell in <label>, comes back at <end> and prints it again.

The call example prints the cell in <label>, comes back at <end> and prints it again.

With [02 01 label] it goes to the label for good, so the second [00 00] never runs.

Expected: 0

Six times seven, printed as a number and then as hex.

Expected: 422A

The breaking example counts in cell 1 inside a loop that always runs. At 5 it calls a label that stops itself and its caller, and that ends the loop. It prints each number on its own line.

Expected: 1 2 3 4 5

The specification's own version prints cell 1 with [00 01], which its list of codes calls a character, and the output it states has one line break too many.

Patterns it teaches

Further

Cell-based

Write your own in Run Convert a program from C-Hex

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: c-hex.

Command line

shib run c-hex -e '(goto)
<label>
    [00 00]; (prints 0)
<end>
<main>
    [02 01 label]; (more like goto label)
    [00 00]; (prints 0)
<end>'
shib manual c-hex

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/c-hex.js

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

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