“Brainfuck? Something wrong?”
Last week I somehow managed to come across brainfuck, the esoteric programming language, again. I had read about it some years ago and also had the idea once to make a brainfuck interpreter.
Why? Do we always need a rational reason? Maybe I just wanted to see if it will fuck up my brain or if I will stay mentally sane. Maybe I just thought it’s very funny. Maybe god decided I had to try to do it… nah… not in my reality… anyway…
“So what is brainfuck?”
Brainfuck is a so called ‘esoteric programming language’ created by a Swiss guy named Urban Müller in 1993 (also check the wikipedia article). A esoteric programming language is a fun thing. An instrument to practice logical thinking if you want. There’s even a ‘dialect’ of brainfuck for Orang-Utans called Ook!. Now that’s silly!
Brainfuck consists of 8 commands
- You have two commands to move the pointer in memory ( > , < ) .
- You have two commands to changed the values in the memory ( + , – ).
- You have two commands to open/close loops ( [ , ] ). If memory[pointer] == 0 the loop will end. So you have to do some sort of loop-counter, else you will get an infinite loop.
- You have two commands for output (.) and input (,) . The dot “.” will output the character based on the ASCII-value that is in the current memory location. The comma “,” will get a character from the user. The ASCII-value will be saved to the current memory location.
So … you move around in memory and increase, decrease and display values. You can actually do very complex calculations like multiplication, division, etc. You just have to think how you can achieve that in brainfuck.
++ Increase value of first memory cell two times = 2
[>++<-] loop until first memory cell is 0 , in every loop move to the second cell, add 2 to the second cell and then go back to the first cell and decrease its value with 1
In the end the first cell will be empty and the second cell will contain the value 4.
We take example 1) and add some more code.
++ Increase value of first cell two times = 2
[>++ Move to second cell and increase value two times = 2
[>++<-] Move to third cell, increase value by 2, move back to second cell and decrease value by 1
<-] Move to first cell and decrease value by one.
These nested loops will increase the value of the third cell to 8. The other two cells will contain the value 0 because they were ‘used’ by the loops.
“Very interesting … bla bla bla … and now?”
If you find errors please tell me.
Happy coding. 😉