You are on page 1of 2

The Unit Distance Graph and AC

Instructor: Padraic Bartlett

Lecture 1: Malbolge, A Quick Guide


Week 5

Mathcamp 2012

Malbolge, named after the eighth circle of Hell in Dantes Inferno, is a language specifically designed to be impossible to write useful programs in. It took two years before anyone
discovered how to write Hello, World1 in it: furthermore, this wasnt even done by humans. (A beam-search algorithm2 was used to generate the program.)
You may have noticed that Nic and Asilatas class 5 Programming Languages in 10
Days somehow forgot to talk about this language! Lets fix that.

Malbolge: Setup and Instruction Sets

Registers. Malbolge has three registers, a, c, and d. When the program starts, all three of
these registers are 0; as the program runs3 , these values may change. The register
c is special; it points to the current instruction. d and a, conversely, are usually
somehow related to whatever data youre currently manipulating.
Memory. Binary is boring. Accordingly, Malbolge works in ternary! Specifically, Malbolge runs
in a block of 310 = 59048 memory locations, each of which contains a number of length
310 . Conveniently, this allows any one of these blocks x to either be interpreted as a
number (in which case we write it as x) or as an address, pointing to the value stored
in one of our other 310 blocks (in which case we write it as [x].)
Instructions. Malbolge has eight instructions. To figure out what instruction to do at any point in
time, Malbolge does the most natural thing possible: it takes the value at [c], adds
the number c to it, and takes that sum mod 94. After doing this, perform the corresponding operation:
1

Or, more accurately, HEllO WORld.


Roughly speaking, this search takes a program, generates a number of possible successors to it by
adding random little bits to the end of them, picks a handful that it thinks are likely to work out at the end
because theyre at least printing out something, and then repeats this search on the successors. Basically a
miniaturized version of evolution.
3
Assuming that your program does run.
2

Value of ([c] + c) mod 94


4
5

Instruction
jump(d)
print(a)

23

a = input

39

a = [d] = rotate([d])

40
62

d = [d]
a = [d] = crazy([d], a)

68
81

nop
halt

Result
Set c, the code pointer, to the value at [d].
Print the character given by a,
mod 256, as an ASCII character.
Take a character from standard input,
put it in a.
Take the ternary string at [d], rotate
it around to the right, put it in a and [d].
Put the value at [d] into d.
Perform the crazy operation using the
value at [d] and a, and store the result
at [d], a.
Does nothing.
Halts.

The crazy operation referenced above is a trit-wise operation on two ternary strings of
length k that returns a ternary string of length k. It works character-by-character on the
ternary string by using the following table:
0
1
2

0
1
1
2

1
0
0
2

2
0
2
1

After each instruction, Malbolge helpfully takes the value at [c], replaces it with itself
mod 94, and then encrypts the result using the following table:
result
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

encrypted
57
109
60
46
84
86
97
99
96
117
89
42
77
75
39
88
126
120
68

result
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

encrypted
108
125
82
69
111
107
78
58
35
63
71
34
105
64
53
122
93
38
103

result
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

encrypted
113
116
121
102
114
36
40
119
101
52
123
87
80
41
72
45
90
110
44

result
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

encrypted
91
37
92
51
100
76
43
81
59
62
85
33
112
74
83
55
50
70
104

result
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

encrypted
79
65
49
67
66
54
118
94
61
73
95
48
47
56
124
106
115
98

This step stops you from accidentally repeating any given instructions, so that your
code is always new and interesting!
Finally, once youve done this step, you increase both c and d by 1, and repeat the
execution cycle. A html compiler can be found at
http://matthias-ernst.eu/malbolge/debugger.html
You enter your code via ASCII values (i.e. each ascii character is a ternary number.) It
doesnt support input yet, but Im sure you can still do fascinating things without it.
2

You might also like