Today I’ll show you how to program in Brainfuck, a simple, useless, esoteric programming language.
Here’s what we’ll be going over:
1. History
2. Basic Syntax
3. Loops
Chapter 1 – History Brainfuck was created in 1993 by Urban Muller. He wanted to create a language that could be used with the smallest possible compiler, inspired by the FALSE language’s 1024-byte compiler. There have been several compilers created smaller than 200 bytes, and one of only 100 bytes. (Taken from Wikipedia.
http://en.wikipedia.org/wiki/Brainfuck_(programming_language))
Chapter 2 – Basic Syntax There’s only 8 characters that are used in Brainfuck: the period (.), the comma (,), square brackets ([]), greater-less than symbols (<>), and the addition and subtraction operators (+-).
To start off with, the only thing brainfuck can do is print out letters in the ASCII table. To do this, it stores decimals in virtual “cells” which can be edited with the (+) and (-). You can shift which cell you are editing by using the (<) and (>). The best way to explain this is to show you a simple brainfuck program and step through each operation. This is the program we will be working with:
++>++--<--
Brainfuck starts you out on the 0’th cell, with all cells having the value of zero. So here is a graphic representation of the cells we have now, up to the 5’th cell (Where [n] is a cell, ‘n’ is the value of the cell, and * denotes the cell we are currently editing):
[*0] [0] [0] [0] [0]
Now, for the first operation, which is the (+). This increases the value of the cell we are pointing to, or editing, by one. So here is what we have after the first operation:
[*1] [0] [0] [0] [0]
Okay, cool. So you should be able to guess what the next operation does. On to the 3rd operation (>). This is one of the operators that allows us to change the cell we are pointing to, the other being (<). Now, after this operation, we have this:
[2] [*0] [0] [0] [0]
As you can see, our pointer is now at the second cell. So the fourth operation, another (+), edits the second cell. Therefore after the fifth operation we have this:
[2] [*2] [0] [0] [0]
On to the last unique operator in this program, the (-). This does exactly what you would think it would; subtracts from the current cell. At the end of the program we actually end up with the same thing we started out with:
[*0] [0] [0] [0] [0]
Now we can talk about another simple operator that didn’t appear in this example, the (,). This operator halts execution until an input is supplied. This input can be a number or letter, it doesn’t matter. Once you supply the input, the brainfuck replaces the value of the current cell you’re pointing at with the ASCII decimal equivalent of the character you supplied. Therefore if I supplied the letter ‘A’, the cell I was pointing to would contain the value 65. However, if I supplied a number and printed, it would print out that number.
So now you understand the basic syntax of brainfuck. It’s very simple, no? Alright, but now you may be wondering, “How does this allow us to print out letters?” Well the answer to that is very simple: when you use the (.) operator, it looks at the value of the current cell you are pointing to and prints out the ASCII table value of that decimal. Take a look at
http://www.asciitable.com/ to see the different key codes for each character. Brainfuck does include the extended characters too if you were wondering about that.
Chapter 3 – Loops Okay so that’s all fine and dandy, but who wants to type in and count 66 (+)’s to print out ‘A’? That’s where the ([) and (]) come in. These brackets act like loops, allowing you to repeat the code between them. Here’s how it works: whenever the compiler reaches a (]), it looks at the value of the current cell you’re pointing to. If this value is 0, it continues. If not, it starts the code at the previous ([) again. Here is an example:
++++++++++ [ - ]
What this does is add 10 to the 0th cell and then decrements it until it reaches 0. This happens because the first time the (]) is reached, cell 0 is 9. Because this is not equal to 0, it starts the code back at the first ([) it can find. Next time the compiler reaches the (]), cell 0 is 8. It returns. So on and so forth until the cell it’s pointing to has the value of 0. Here is a simple example that prints out ‘A’:
++++++ [ > ++++++++++ < - ] > +++++ .
Since you now know all the operators in this program, I’ll leave it to you to figure out why this prints out ‘A’. It’s fairly simple if you’ve been paying attention so far.
_______________________________________
Now you understand the basic concepts of the programming language brainfuck. However useless it may be, you now know, so congratulations. Go out and conquer the world with your newborn skills.