BEGIN TYPING YOUR SEARCH ABOVE AND PRESS RETURN TO SEARCH. PRESS ESC TO CANCEL

A Brief Look at brainfuck

I’ve posted about this before, but it has since been deleted (along with my website). Here’s a refresher:

I know. You’re thinking – “This guy’s got a mouth on him”. Well, in this case, I do. I’ve been looking into the brainfuck programming language, and I said I’d like to give it a go.

This is My Experience of Using brainfuck for 30 Minutes

A Quick Introduction

According to wikipedia:

“Brainfuck is an esoteric programming language noted for its extreme minimalism. The language consists of only eight simple commands and an instruction pointer. It is designed to challenge and amuse programmers, and was not made to be suitable for practical use. It was created in 1993 by Urban Müller.”

 

3 Reasons to Learn brainfuck

  • You’re bored
  • You want to show off to your mates
  • You want hot girls to throw themselves at you

Commands in brainfuck

There is a total of 8 commands in brainfuck. Each command consists of a single character and every character that is not a command is ignored by the interpreter.

Brainfuck is represented by an **array with 30,000 cells** initialized to zero and a data pointer pointing at the current cell (Starting at cell 0).

The eight commands are:

Command Explanation
+ Increments the value at the current cell by one.
Decrements the value at the current cell by one.
> Moves the data pointer to the next cell (cell on the right).
< Moves the data pointer to the previous cell (cell on the left).
. Prints the ASCII value at the current cell (i.e. 65 = ‘A’).
, Reads a single input character into the current cell.
[ If the value at the current cell is zero, skips to the corresponding ] . Otherwise, move to the next instruction.
] If the value at the current cell is zero, move to the next instruction. Otherwise, move backwards in the instructions to the corresponding [

My First Ever brainfuck Program

This took me 15 to 20 minutes to create, which includes the time I spent learning the language. To execute it, I am using an online brainfuck interpreter, which is written in javascript and it works pretty well. Check it out HERE My first program is pretty simple, it just prints the word “ROBBIE” to the screen. Here it is:

Explanation

To explain what’s going on, I’ll break the program down and explain every single command to help you understand what exactly is going on. Also, remember that any character that is not a command is ignored by the interpreter i.e. every alpha-numeric character and white space.

Tip: If you have a pencil and paper, it might be useful to draw an array to keep track of what’s in each cell.

How the original code prints each letter:

Printing the letter R

We want to print R, that means we’ve got to set a cell in the array to 82 (the ASCII number for R). Sounds easy, right? You could simply insert 82 + commands and output it using the . command .

It’s correct, but there’s a cleaner and a more clever way to do it using loops.

We are going to use cell 0 as the loop counter. When the loop counter gets to 0, the loop will end and we’ll move on to the next instruction after the ] command.

I’ll give you the algorithm for printing the letter R using loops:

Initialise cell 0 to 8
While cell 0 != 0
move to the next cell (cell 1)
increment cell 1 by 10
move back to the previous cell (cell 0)
decrement cell 0 by 1

Code to Print R

Command by command instructions:

Printing the Letters OBB

This is very similar to Printing R. If you understand that, then you should have no problem understanding how to print OBB.

Printing I

This is a different approach to printing a letter. Here, we will use the value of the previously assigned cell (the letter B in this case).

We are going to move the value from cell 3 (66 i.e. the letter B ) into cell 4, and then manipulate the value of cell 4 to make it equal to 73 (i.e. the letter I).

To do this we need to move 66 (B) to the next cell and add 7 to turn it into an I.

The code to move the current cell into the next cell:

Code explained command by command:

Printing the Letter E

You’ve just printed the letter I. Printing E uses the same concept as printing I (moving the value (I) to the next cell and manipulating it to change it to an E)

Finished


Basic Algorithms

Now that you’ve conquered the basics of looping, here is a list of useful brainfuck algorithms.

There you have it. You’ve created your first brainfuck program, learned about loops and moving a value from one cell to the next, and you’ve got a list of the basic algorithms that you can play around with.

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked