Author Topic: Learning Brainfuck for Pleasure and Profit  (Read 3920 times)

0 Members and 1 Guest are viewing this topic.

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Learning Brainfuck for Pleasure and Profit
« on: May 31, 2014, 09:37:57 pm »
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:

Code: [Select]
++>++--<--
   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):

Code: [Select]
[*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:

Code: [Select]
[*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:

Code: [Select]
[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:

Code: [Select]
[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:

Code: [Select]
[*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:

Code: [Select]
++++++++++ [ - ]
   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’:

Code: [Select]
++++++ [ > ++++++++++ < - ] > +++++ .
   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.
« Last Edit: May 31, 2014, 10:10:21 pm by Matriplex »
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Learning Brainfuck for Pleasure and Profit
« Reply #1 on: May 31, 2014, 10:08:14 pm »
Awesome, I thought brainfuck would be more complex :P
Thanks, cookie++;
Easter egg in all *nix systems: E(){ E|E& };E

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Learning Brainfuck for Pleasure and Profit
« Reply #2 on: June 01, 2014, 01:35:52 am »
Awesome, I thought brainfuck would be more complex :P
Thanks, cookie++;

No problem :)
I did too, a lot of esoteric languages look complicated but turn out to be pretty simple.
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline hackernar

  • /dev/null
  • *
  • Posts: 5
  • Cookies: 0
    • View Profile
Re: Learning Brainfuck for Pleasure and Profit
« Reply #3 on: June 01, 2014, 07:00:29 am »
thank you for tut
and here's the answer
Code: [Select]
[*6]
[6] [*10]
[*5][10]
[5] [*20]
[*4] [20]
[4] [*30]
[*3] [30]
[3] [*40]
[*2] [40]
[2] [*50]
[*1] [50]
[1] [*60]
[*0] [60]
[0] [*65]
and . will print
value of * that is 65 in ASCI= A
« Last Edit: June 01, 2014, 07:01:25 am by hackernar »
When you feel like giving up,
remember
why you held on
for so long
in the first place.

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Learning Brainfuck for Pleasure and Profit
« Reply #4 on: June 02, 2014, 12:07:21 am »
thank you for tut
and here's the answer
Code: [Select]
[*6]
[6] [*10]
[*5][10]
[5] [*20]
[*4] [20]
[4] [*30]
[*3] [30]
[3] [*40]
[*2] [40]
[2] [*50]
[*1] [50]
[1] [*60]
[*0] [60]
[0] [*65]
and . will print
value of * that is 65 in ASCI= A

Exactly, good job. If you want to mess around with it a little more, check this out: http://esoteric.sange.fi/brainfuck/impl/interp/i.html
\x64\x6F\x75\x65\x76\x65\x6E\x00