In order to gain a deeper understanding to this fine language, I'll explain a simple hello world program. Brainfuck is a language which I am convinced was designed as a joke. In reality it was designed to be a simple language with the smallest possible compiler while still having some ability to do mathematical calculations. It consists of eight instructions, as follows:

  • > : move the memory pointer to the next cell,
  • < : move the memory pointer to the previous cell,
  • +': increment the memory cell under the memory pointer,
  • -: decrement the memory cell under the memory pointer,
  • , : fills the memory cell under the memory pointer with the ASCII value of next character from the input,
  • . : writes the contents of the memory cell under the memory pointer as a character with the corresponding ASCII value,
  • [ : moves to the command following the matching ']', if the memory cell under the memory pointer is zero
  • ] : moves to the command following the matching '[', if the memory cell under the memory pointer is not zero

You're probably thinking, what the fuck? So a better way to think of this is in C terms where p is an int:

  • > is ++p
  • < is --p
  • + is ++*p
  • - is --*p
  • . is putchar(*p)
  • , is getchar(*p)
  • [ is while(*p) {
  • ] is }

Now, you're probably still thinking, what the fuck? So let's look at how to write a hello world program. Given the 8 commands we have available, we can see that to print out hello, we need to assign bytes the correct ascii value and then print it out. First, a look at the entire program


You'll notice this follows a pattern of >, several + characters, and then [ ] with some +'s in between and ending with a -. This is because we're doing the same thing again and again, getting up to a byte value and printing out the character. So let's look at just how the H is printed. H has an ascii value of 72, so we need to assign that value to our pointer.

First, we have a > which advances us to the next pointer, so we're ready to work. Next we have 9 +'s, which gives our pointer a value of 9. Next we have the brackets, which enter into a loop. The first < takes us back to the previous pointer, and the 8 + characters after that add 8 to that pointer. Finally we advance to the next pointer, and subtract 1.

Ok, what the heck does this really mean? Let's take it step by step.

> - advances us to pointer #1
+++++++++ - assigns pointer #1 the value of 9
[ ... ] - says to do the enclosed while the current pointer is greater than 0
<++++++++>- - while in our loop, we go back to pointer 0 and increment it by 8, then come back to pointer 1, and decrease it by 1

So when you put this all together, you see that all we are really doing is setting a variable to the value of 9, and then while that variable is greater than 0, we add eight to another variable and decrement the first variable by 1. In effect, we are multiplying 8 by 9, which as well all know, is 72!

So now we have a pointer with the value of 72, which is an ASCII character for H. All we do now is jump back to pointer 0 (which is what we gave the value of 72), print it out (the . ), and then advance to the next pointer and do it again for e.

Pretty sweet, eh? That's pretty much it to this program, just keep in mind that for futher values we're using the same pointer so the incrementing/decrementing starts off at the previous value.

Log in or register to write something here or to contact authors.