Introduction to Esoteric Language Malbolge Masahiko Sakai (Nagoya University) 99 Bottles of Beer(1/3) 99 Bottles of Beer(2/3) 99
Total Page:16
File Type:pdf, Size:1020Kb
99 bottles of beer(2/3) 99 bottles of beer(1/3) • Top list (2008): ours in Malbolge is No.1 ! • http://www.99-bottles-of-beer.net/ • Collection of programs that output the song Introduction to 99 bottles of beer on the wall, 99 bottles of beer. Esoteric Language Malbolge Take on down and pass it around, 98 bottles of beer on the wall. ... 2 bottles of beer on the wall, 2 bottles of beer. Masahiko Sakai (Nagoya University) Take on down and pass it around, 1 bottle of beer on the wall. 1 bottle of beer on the wall, 1 bottle of beer. Take on down and pass it around, no more bottles of beer on the 10 Dec 2010, College of Technology, VNU wall. No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall. • Programs in more than 1200 different lan- guages 1 2 3 99 bottles of beer(3/3) Esoteric Programming Languages Malbolge • Comments for Malbolge • Language designed to be HARD to read and • A programming language come from HELL write - 10 trits machine defined by its interpreter - INTERCAL [Woods, Lyon 1972] - Each operation is modified after execution - BrainF∗ ∗ ∗ [M¨uller 1993] - Only one trit-wise operation is poor - Befunge [Pressey 1997] A,[D] := crz(A,[D]) - Malbolge [Olmstead 1998] [D]\ A 0 1 2 0 1 0 0 • Proposed as jokes 1 1 0 2 • References 2 2 2 1 http://members.tripod.com/rkusnery/weird.html - Hard to load data, because non-operations http://www.99-bottles-of-beer.net/toplist_esoteric.html are not loadable. 4 5 6 Our Motivation History of Malbolge • Programs written in Esoteric Languages are This talk like encrypted but Executable without en- • [Ben Olmsted 1998] Language Proposal cryption • Explanation of Malbolge • [Andrew Cooke] “HEllo WORld” program • Overview of Programming technique • [Anthony Youhas 2000] Three programs that - Bootstrapping output strings - Low level assembly language • [Lou Scheffer] Program like “cat” - High level assembly language • [Tomasz Wegrzanowski 2004] Method to produce a program that outputs a given string 7 8 9 Syntax of Malbolge Example of Malbolge program • String of printable characters (33 to 126 in Semantics of Malbolge(1/3) ASCII) where spaces are ignored. • “HEllo WORld” program by Cooke. • Each i-th character x must be an operator % cat hello-fake.mal • Malbolge machine (=<‘$9]7<5YXz7wT.3,+O/o’K%$H"’~D|#z@b=‘{^L const char xlat1[] = - Address area = One word = ten trit x8%$Xmrkpohm-kNi;gsedcba‘_^]\[ZYXWVUTSRQPO 10 "+b(29e*j1VMEKLyC})8&m#~W>qxdRp0wkrUo" NMLKJIHGFEDCBA@?>=<;:9876543s+O<oLm 0 to 59048 = 3 − 1 "[D7,XTcA\"lI.v%{gJh4G\\-=O@5‘_3i<?Z’" % ./malbolge hello-fake.mal - Codes and data are stored in memory ";FNQuY]szf$!BS/|t:Pn6^Ha"; HEllO WORld - Three registers if(strchr( "ji*p</vo", • Operations obtained by xlat1[(x-33+i)%94] A: accumulator xlat1[( x-33+i ) % 94]) == NULL) C: code pointer jpp<jp<pop<<jo*<popp<o*p<pp<pop<pop<jijoj/ D: data pointer fputs("invalid character\n", stderr ); o<vvjpopoopo<ojo/ovooooooooooooooooooooooo Note that illegal operator is not loadable, but oooooooooooooooooooooooooooo*p<v*<* executable as Nop. 10 11 12 (3/3) Semantics of Malbolge(2/3) Semantics of Malbolge • Eight operators, which take no operands Example of execution • Execution operator notation action j MovD D := [D] const char xlat2[] = • “HEllo WORld” "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1C" i Jmp C := [D] "B6v^=I_0/8|jsb9m<.TVac‘uY*MK’X~xDl}REokN:#?G\"i@"; * Rot A,[D] := rotr([D]) A C D adr data opr for (;;) { if ( mem[c] < 33 || mem[c] > 126 ) continue; p Opr A,[D] := crz(A,[D]) 0000000000t 0 0 0 0000001111t ( j MovD switch ( xlat1[( mem[c]-33+c )%94] ) { < Out putchar(A) case ’j’: d = mem[d]; break; 0000000000t 1 41 1 0000002021t = p Opr case ’i’: c = mem[d]; break; / In A := getchar() 1111112211t 2 42 2 0000002020t < p Opr case ’*’: a = mem[d] = v Hlt halt mem[d]/3 + mem[d]%3*19683; break; 0000002200t 3 43 3 0000010120t ‘ < Out case ’p’: a = mem[d] = crz( a, mem[d] ); break; o Nop no operation case ’<’: putc( a, stdout ); break; other printable Nop no operation case ’/’: x = getc( stdin ); a = x; break; 41 0000002211t L / In case ’v’: return; non-printable — infinite loop } 42 0000011110t x o Nop mem[c] = xlat2[mem[c] - 33]; 43 0000002002t 8 < Out if ( c == 59048 ) c = 0; else c++; • Codes are rewritten after step execution if ( d == 59048 ) d = 0; else d++; } • C and D registers are incremented after step execution 13 14 15 Repeated access data on memory(2/2) Trit-wise operations • Example: implementation of pseudo-code (1/2) Repeated access data on memory Fact: Any trit-wise operations are function- Pseudo code label data/opr ally composable from 0, 1, 2 by crz • No load nor save operation. We have only C Rot label operation Nop Proof: By exhaustive search - Rot: A,[D] := rotr([D]) Rot CON2 Opr X Y Opr X Example: inc( , ) increment - Opr: A,[D] := crz(A,[D]) MovD Y\X 0 1 2 Rot CON2 Rot Problem: Hard to access data repeatedly Opr X 0 1 2 0 • Nop 1 1 2 0 Opr • A solution: preparing codes so that D register CON2: 2222222222t 2 0 1 2 loops in data area (D register is incremented MovD Y: inc(X, Y) = crz(crz(crz(crz(2, X), 0), Y),crz(2, X)) in similar to C register) X: D CON2: 2222222222t • Note: It does not guarantee the existence of A := X (nondestructive Y: load of X) X: codes, since Opr is destructive operation CON2 - 1 16 17 18 Generating special constants(3/3) Generating special constants(1/3) Generating special constants(2/3) • P20: 2222222220t • CON1: 1111111111t • CON2: 2222222222t Precondition: TMP has a pattern .......2.. Precondition: variable CON1 has no 1s Precondition: TMP has a pattern ...20... (each . is not 2) and P20 = CON2 (... parts contain no 2s) Rot CON1 Rot TMP Opr CON1 Rot CON1 Rot TMP Opr CON2 Opr P20 • CON0: 0000000000t Opr CON2 Rot CON1 Now variable CON2 has been set 1111111111t • Possible initial code for data area Opr CON0 Rot TMP adr label opr data Opr CON0 Opr CON2 85 TMP In 0000011200t Opr CON0 repeat 10 times in total 88 CON1 Nop 0000002202t 91 Hlt 84 19 20 21 Nondestructive data copy Successor function: X := X+1 • Y := X using Z for temporal area Precondition: Zero clear Rot CON1 • Opr Z Y = 2222222220t and • X := 0 Opr Z Z contains no 2s Rot CON1 Opr Y Rot CON1 Opr Y Opr Y Opr X Opr Y Opr Z Opr X Rot CON2 Opr X Rot CON2 Rot CON2 Opr Z Opr Y Opr X Opr X Opr X Opr Z Rot X Rot CON2 Opr Z Opr Y Opr X repeat 10 times in total Opr Z Opr Y 22 23 24 Generating arbitral data(1/2) Generating arbitral data(2/2) • Trit-wise operation f(X, Y) = crz(2,crz(X, Y)) This talk Y\X 0 1 2 0 2 0 0 • Generation of arbitral data is possible from 1 2 0 1 X = 1111111111t by previous code and “Rot X” • Explanation of Malbolge 2 1 1 2 • Loading P20 • Overview of Programming technique • Precondition: right-most trit of X be 1 Rot CON0 - Bootstrapping Opr X - Low level assembly language Rot CON2 Opr P2X Opr X • Loading P21 - High level assembly language makes right-most trit of X to Rot CON1 0 if A = 2222222221t (=P21) Opr P2X 1 if A = 2222222222t (=CON2) 2 if A = 2222222220t (=P20) where P2X is 2222222220t or 2222222221 without changing the other trits 25 26 27 Low level assembly language Character-replacement analysis(1/3) • Designed for writing loop programs. • Replacement by xlat2[] • Syntax • Example of low level assembly code for a pseudo code const char xlat2[] = - Labeled sequence of U_JMP, U_ROT, R_ROT, "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1C" L1: R_ROT "B6v^=I_0/8|jsb9m<.TVac‘uY*MK’X~xDl}REokN:#?G\"i@"; U_MOV_PC, R_MOV_PC and so on, and Flags for (;;) { U_ROT X ... OMITTED ... • Semantics: ternary virtual machine Rot X ENTRY: U_ROT X mem[c] = xlat2[mem[c] - 33]; } - Registers: A and PC Opr X X: 30537 FLAG1 • Periodic table - Need to execute R_hoge after executing op- X: 30527 L1 erator U_hoge R_OPR ID period sequence Pseudo code END #1 2 F J - Variables must be placed below and near #2 4 *r}i the operation. All operation between the Low level assembly code #3 5 )f‘<3 operation and the variable are skipped un- #4 6 %guox: executed. #5 9 2PB>LOCUI2 - Flags act as flip-flopped U_MOV_PC #6 68 !5-wN1W0{GS~9[ · · · 28 29 30 Character-replacement analysis(2/3) • Operators for sequence with period two adr op. F op. J adr op. F op. J Character-replacement analysis(3/3) 7 – Hlt 59 – Rot Implementation of L Ass(1/3) 11 Hlt – 60 – MovD • Jmp is not replaced by execution, instead the 24 – Jmp 63 Rot – operater at one earlier location of the new • Basic idea 25 – Out 64 MovD – 28 Jmp – 82 – Opr location is replaced - Construct operation units each of which 29 Out – 86 Opr – • Character corresponding to SNop exists for ev- consists of an operation with cycling pe- 43 – In 88 – Nop ery location riod two 47 In – 92 Nop – - Stable Nop (SNop): no operation appears in - Use D register as program counter PC - Address are in mod 94 the sequence except Nop or Nop - Symbol – represents non-operator that works as Nop -e.g.