TGUA.DEV | Home | Blog | About | More | GUEST  
Reference ID: 41 | Category:
BrainFuck - Hello world

Today, we're going to write a basic 'Hello, World!' program in Brainfuck. But first, let's talk about the 5 basic operations in Brainfuck:

'+' increases the value of the current cell. For example, if you want to change the value to 2 (which corresponds to the ASCII character 'STX'), the correct code would be ++.

'-' is the opposite: it decreases the value of the current cell by one.

'>' and '<' move the data pointer (or 'cursor') one cell to the right (>) or one cell to the left (<), respectively.

'[' and ']' define a loop. Anything within the brackets will repeat as long as the current cell's value is not zero. For example, if you want to add 5 three times, you can use the code +++[>+++++<-] In this case, cell 0 is initialized to 3 and acts as a counter. The loop adds 5 to cell 1, then decrements the counter until it reaches zero.

'.' prints the value of the current cell as an ASCII character. For instance, if the current cell contains the value 33 (which is ! in ASCII), you can use the Brainfuck code +++++++[>+++++<-]>--. to print !.

This has been a basic explainer of the functions we're going to be using today.

Now down to the Hello World, AKA hell to a normal human.
First we'll print 'H', which after looking at a reference sheet is ASCII character 72. First we'll reach this number, which in this case we'll loop +10 7 times, then add two.
+++++++[>++++++++++<-]>++
Then we'll add a print to show the result:
+++++++[>++++++++++<-]>++.
And testing this on an online compiler, we can confirm it outputs 'H'.
Now it gets slightly easier, we now need to print 'e' (ASCII 101). To Achieve this we'll add 29 to the existing value of 72, to do this we'll first add 30 with a loop of adding 5 six times, then subtracting one and using '.' to output our second character:
<++++++[>+++++<-]>-.
After testing this correctly outputs 'e', now so far our complete code is +++++++[>++++++++++<-]>++.<++++++[>+++++<-]>-.
Now we'll print the double 'l'(ASCII 108), to do this we'll simply add 7 without any loops and print twice.
+++++++..
Now we will append that to the end of the current code creating +++++++[>++++++++++<-]>++.<++++++[>+++++<-]>-.+++++++.., which correctly compiles to 'Hell'.
Now while saying hell in Brainfuck is a very good description of the language, that's reserved for Malbolge. Now let's continue with 'o' (ASCII 111).
To achieve this code we'll simply add 3 to the current value and print with the code +++., this changes our complete code to +++++++[>++++++++++<-]>++.<++++++[>+++++<-]>-.+++++++..+++., which gives the result 'Hello'.
So, we have Hello, but we need world, though I wish I didn't have to, let's rush through the code for ' World'.
Next is a space (' ')(ASCII 32), now this is going to be a bit harder to jump from 111 to class='language-brainfuck'32, so we'll actually start using cell 2 and three, and starting from zero again with >++++++[>+++++<-]>++..
Now 'w'(ASCII 119), now again, i'd be a jump to go from 32 to 119, so we're actually going back to using cell 1 again with <<++++++++.
Now 'o', which we've used already so just reversing the last code with --------..
Next is 'r'(ASCII 114), again very simply adding 3, so +++..
Again, 'l', ASCII 108, so we'll just decrease 6 with ------..
Now, Finally... 'd'(ASCII 100), this time we'll simply subtract 8 again with --------.

Yes! We now have a correctly written and compiled Hello world program in BrainFuck! The final code is 120 characters and is as follows:
+++++++[>++++++++++<-]>++.<++++++[>+++++<-]>-.+++++++..+++.>++++++[>+++++<-]>++.<<++++++++.--------.+++.------.--------.

Date of writing: --



Comments section
Posts are stored on there server they are sent to; Comments will not be viewable if you are connected to a different server.





Comments


No comments yet.