Malbolge
A ternary machine whose instructions depend on where they sit and encrypt themselves after running. Its first program was found by search, not written.
Ben Olmstead, 1998 · Turing complete: with unbounded memory · ●●● hard · esolangs.org
Tested with 1 example and 2 known programs · Hello World on Try It Online: identical · Test method
Run it
Hello World
Expected output
Hello World!
Output
The idea
Ben Olmstead designed Malbolge in 1998 to be as hard to program in as possible. It is named after Malebolge, the circle of Dante's hell for frauds. Where INTERCAL is hostile through silly bureaucracy, Malbolge is hostile through cryptography.
Read the story
Three tricks stack up. What an instruction means depends on its address. After it runs, it is replaced by a different character from a substitution table. And the only arithmetic is an arbitrary "crazy" operation on base-3 digits.
It took two years before anyone produced a program. That first Hello World was not written by a person: Andrew Cooke found it with a beam search in Lisp. Later, Lou Scheffer treated the language as a cipher to break and worked out how to write programs on purpose. Today people write Malbolge with generators and careful theory, not by hand.
The machine
Memory is 3^10 = 59,049 words of ten trits (base-3 digits). Code and data share it. Three registers start at 0: c (code pointer), d (data pointer) and a (accumulator).
The source is loaded from address 0; whitespace is skipped. The rest of memory is filled by applying the crazy op to the two previous words. Each step:
- If
[c]is not a printable character (33 to 126), stop. - The instruction is
([c] + c) mod 94:
| Value | Does |
|---|---|
| 4 | c = [d]: jump |
| 5 | Print a mod 256 |
| 23 | Read a character into a |
| 39 | a = [d] = rotate [d] right by one trit |
| 40 | d = [d] |
| 62 | a = [d] = crazy(a, [d]) |
| 81 | Stop |
| other | Nothing |
[c]is replaced through a fixed 94-character substitution table: the instruction encrypts itself.canddboth go up by 1.
The crazy op works trit by trit from a 3×3 table that matches no familiar logic. Only the eight instruction values are allowed in the source at load time, which is why most random text is rejected.
Example
This "Hello, world." by Kamila Szewczyk, from Wikipedia (CC BY-SA 4.0), runs in this implementation.
This "Hello, world." by Kamila Szewczyk, from Wikipedia (CC BY-SA 4.0), runs in this implementation.
(=<`#9]~6ZY327Uv4-QsqpMn&+Ij"'E%e{Ab~w=_:]Kw%o44Uqp0/Q?xNvL:`H%c#DD2^WV>gY;dts76qKJImZkj
The first two steps, by hand:
c = 0,[0]is((40).(40 + 0) mod 94 = 40, sod = [d] = [0] = 40. Then(is encrypted toy, andc,dbecome 1, 41.c = 1,[1]is=(61).(61 + 1) mod 94 = 62: the crazy op onaand[41].
Every character is chosen to mean something at its position. Shift the program one place and it means something else entirely.
(The quoting is ugly in a shell; saving it to hello.mal and running shib run malbolge hello.mal is easier.)
Quirks
- A run stops after 3,000,000 steps.
Try this
- Run the program above, then change one character and run it again. Most changes are rejected at load.
- Which characters are legal at address 0? (Find the eight values of
xwithx mod 94in the table.) - Hard: write a search (brute force, then beam search) that finds a short program printing
A. Use this implementation as the judge.
Patterns it teaches
- Hostile design: the high point of the genre. Each trick alone is manageable; together they defeat human intuition.
- Self-modifying code: every instruction rewrites itself after use. A loop has to be built so that its code comes back around the encryption cycle.
- Turing completeness: with 59,049 words it is a bounded machine. The variant Malbolge Unshackled lifts the limit and is universal.
Further
- esolangs.org/wiki/Malbolge
- Lou Scheffer's analysis and the reference interpreter: lscheffer.com/malbolge.shtml
- Andrew Cooke's account of the first program: acooke.org/malbolge.html (archived)
See also
Self-modifying
Cell-based
Write your own in Run Convert a program from Malbolge
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: malbolge.
Command line
shib run malbolge -e '(=<`#9]~6ZY32Vx/4Rs+0No-&Jk)"Fh}|Bcy?`=*z]Kw%oG4UUS0/@-ejc(:'\''8dc'shib manual malbolgeSource
- Module
langs/grid/index.js- Interpreter
langs/grid/malbolge.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org