BrainFu** Interpreter and Debugger

Run Your Favorite Applications Online

BrainFu / Tutorial / Page 2

Learn Programming in 30 minutes, Here and Now
Page 2 of 3

Published 2024/04/29

Let's write our own “Hello World!” program from scratch. The first task such a program has to accomplish is to output the letter “H”.

In the BrainFu language, output of programs is interpreted according to the ASCII encoding, just like any other common programming language does. Therefore, to output the letter “H”, we need to find out its number in the ASCII code table.

Here is the ASCII code table:

			|    0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
	   -----+----------------------------------------------------
		 32 |       !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /
		 48 |    0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?
		 64 |    @  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O
		 80 |    P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _
		 96 |    `  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o
		112 |    p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~ 
        

The letter “H” is at the intersection of 8 and 64. The code of any symbol in the table equals the sum of the row and the column. Therefore, the code for the letter “H” equals 8 + 64 = 72.

Outputting a Letter “H”

To output a letter “H”, we must first generate its code, which is 72. It is a very easy task: we just need to increment a memory cell by 72, which can be accomplished by repeating, 72 times, incrementation by 1. Incrementing a cell by one can be accomplished by the command +. We can create the number 72 as 8 groups of 9 commands +.

Here is the entire program that outputs a letter “H” on the screen. To see this program in action, you have to run it. In the interpreter, select “Custom” for the source code of the program, and paste the given program in the source code box. If the debugger is open, then close it by clicking on the “Disable Debugger” button. Then, click on the “Run” button.

+++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ . (end)

The last command in the program is the command ., which outputs the value 72. The value 72 will be displayed as the letter “H”.

To get a better understanding of this program, you need to run it inside the debugger. Enable the debugger ans then click on the “Debug Start” button. Use the “Step by 1 instruction” button and “Step by 10 instructions” button to slowly step through the code, until the program ends and outputs the letter “H”.

Outputting a Word “Hello”

To output a letter “e”, we must first find its code in the ASCII code table. The code of the letter “e” equals 96 + 5, which is 101.

But, we already have the memory cell set to the value 72. To increase the value of the cell to 101, we only need to add 29 increment commands. This time, we will use 3 groups of 10 increments, and one decrement.

Here is the entire program, with some comments added in. You should copy this program into the interpreter, and run it.

generate 72 : +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ output “H” = 72 : . add 29 : ++++++++++ ++++++++++ ++++++++++ - output “e” = 101 : .

The comments will be ignored by the interpreter, because they do not contain any symbols for the commands of the BrainFu language. Therefore, you can just paste the given program in the source code box, and run it.

You should also step through this program with the debugger.

Outputting Letters “ll”

The letter “l” has the ASCII code 96 + 12, which equals 108. Therefore, we just need to add 7 more increments:

generate 72 : +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ output “H” = 72 : . add 29 : ++++++++++ ++++++++++ ++++++++++ - output “e” = 101 : . add 7 : +++++++ output “ll” = 108 : . .

Run the program first, then step through it with the debugger.

Outputting Letter “o”

The letter “o” has the ASCII code 96 + 15, which equals 111. Therefore, we just need to add 3 more increments.

Run the program first.

generate 72 : +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ +++++++++ output “H” = 72 : . add 29 : ++++++++++ ++++++++++ ++++++++++ - output “e” = 101 : . add 7 : +++++++ output “ll” = 108 : . . add 3 : +++ output “o” = 111 : .

The program above outputs an entire word “Hello”. Programming in the BrainFu language is really easy, as you can see.

A Shorter Way

It might seem somewhat impractical to generate the number 72 by explicitly writing 72 increment commands. However, there is a shorter way to do it. We can use the “skip” and “repeat” commands to create iterations. The “skip” command is denoted by the symbol [, and the “repeat” command is denoted by the symbol ].

To generate the number 72, we can use 9 iterations of 8 increments. The following program will not be explained in detail yet, instead it is provided to demonstrate the concept of iterations. The program first performs 9 increments of the second memory cell, and then repeats 9 times the incrementation of the first memory cell by 8.

generate 72 : > +++ +++ +++ [ < ++++ ++++ > - ] < output “H” = 72 : . add 29 : ++++++++++ ++++++++++ ++++++++++ - output “e” = 101 : . add 7 : +++++++ output “ll” = 108 : . . add 3 : +++ output “o” = 111 : .

You should first run this program to see the results. Then, you should run this program with the debugger, step by step. When running the program step by step, try to observe the iterating part of the program. Keep the “Memory watch” interface open to track changes of cell values.

The next part of this tutorial will explain iterations in more detail.