A brainfuck way to program the machine...
Brainfuck is probably the craziest language I have ever had the pleasure of coming across. Notable for its extreme minimalism, the language consists of only eight simple commands, a data pointer and an instruction pointer. While it is fully Turing complete, it is not intended for practical use, but to challenge and amuse programmers. Brainfuck simply requires one to break commands into microscopic steps.
The language's name is a reference to the slang term brainfuck, which refers to things so complicated or unusual that they exceed the limits of one's understanding. And, yes, there are quite a few tutorials that you will find on google about the language and how to program in it.
The language only consists of 8 operators, yet with the 8 operators,
<>+-[],.
You are capable of writing almost any program you can think of.
I would suggest an ASCII chart with all the ASCII chars and their decimal equivalent value. Next on the items would be a calculator. Any will do. It will help you figure out the Greatest Common Factors for use in incrementing a memory block quickly.
ASCII TABLE for Reference
Let's Start with the BASICS
Let’s First understand the meaning of 8 Operators:
> = increases memory pointer, or moves the pointer to the right 1 block.
< = decreases memory pointer, or moves the pointer to the left 1 block.
+ = increases value stored at the block pointed to by the memory pointer
- = decreases value stored at the block pointed to by the memory pointer
[ = like c while(cur_block_value != 0) loop.
] = if block currently pointed to's value is not zero, jump back to [
, = like c getchar(). input 1 character.
. = like c putchar(). print 1 character to the console
The C Equivalent of Above Operators are:
As the name suggests, Brainfuck programs tend to be difficult to comprehend. This is partly because any mildly complex task requires a long sequence of commands and partly because the program's text gives no direct indications of the program's state. These, as well as Brainfuck's inefficiency and its limited input/output capabilities, are some of the reasons it is not used for serious programming. Nonetheless, like any Turing complete language, Brainfuck is theoretically capable of computing any computable function or simulating any other computational model, if given access to an unlimited amount of memory.
Some Important Rules to consider:
Any arbitrary character besides the 8 listed above should be ignored by the compiler or interpreter. Characters besides the 8 operators should be con- sidered comments.
All memory blocks on the "array" are set to zero at the beginning of the program. And the memory pointer starts out on the very left most memory block.
Loops may be nested as many times as you want. But all [ must have a corre- sponding ].
Installation of BrainFuck Compiler:
Follow this link to Download and Install Brainfuck Interpreter in Your System.
Another link for Windows : Brainfuck Developer - a Brainfuck IDE (4mhz.de)
Writing First Program in BrainFuck:
Code 1:
[-]
Meaning:
Well, that's what they say anyway, but I hardly consider that a program. All it does is enter a loop that decreases the value stored at the current memory pointer until it reaches zero, then exits the loop. But since all memory blocks start out at zero, it will never enter that loop.
Code 2:
+++++[-]
Above program in c:
*p=+5;
while(*p != 0){
*p--;
}
Meaning:
In the above program we are incrementing the current memory pointers value to 5, then entering a loop that decreases the value located at the memory pointer till it is zero, then exits the loop.
Code 3:
>>>>++
Above program in c:
*p=+5;
while(*p != 0){
*p--;
}
Meaning:
This will move the memory pointer to the fourth memory block, and increment the value stored there by 2. So it looks like
memory blocks
-------------
[0][0][0][2][0][0]...
^
memory pointer
As you can see in the 'k-rad' ASCII diagram, our memory pointer points to the fourth memory block, and it increments the value there by 1. since there was nothing there before, it now contains the value: 2. If we take that same program, and add more onto the end of it like:
>>>>++<<+>>+
At the end of our program, our memory layout will look like this:
memory blocks
-------------
[0][1][0][3][0][0]...
^
memory pointer
The pointer was moved to the fourth block, increased the value by 2, moved back 2 blocks to the second block, increment value stored there by 1, and then the pointer moved 2 blocks to the right again to the fourth block and increment value stored there by one. And at the end of the program the memory pointer lies back on the fourth memory block. That is fine and dandy, but we can't really see anything. So let's write a program that will produce actual output.
Let’s Say Hello World to BrainFuck!
[ This program prints "Hello World!" and a newline to the screen, its
length is 106 active command characters. [It is not the shortest.]
This loop is an "initial comment loop", a simple way of adding a comment
to a BF program such that you don't have to worry about any command
characters. Any ".", ",", "+", "-", "<" and ">" characters are simply
ignored, the "[" and "]" characters just have to be balanced. This
loop and the commands it contains are ignored because the current cell
defaults to a value of 0; the 0 value causes this loop to be skipped.
]
++++++++ Set Cell #0 to 8
[
>++++ Add 4 to Cell #1; this will always set Cell #1 to 4
[ as the cell will be cleared by the loop
>++ Add 2 to Cell #2
>+++ Add 3 to Cell #3
>+++ Add 3 to Cell #4
>+ Add 1 to Cell #5
<<<<- Decrement the loop counter in Cell #1
] Loop until Cell #1 is zero; number of iterations is 4
>+ Add 1 to Cell #2
>+ Add 1 to Cell #3
>- Subtract 1 from Cell #4
>>+ Add 1 to Cell #6
[<] Move back to the first zero cell you find; this will
be Cell #1 which was cleared by the previous loop
<- Decrement the loop Counter in Cell #0
] Loop until Cell #0 is zero; number of iterations is 8
The result of this is:
Cell no : 0 1 2 3 4 5 6
Contents: 0 0 72 104 88 32 8
Pointer : ^
>>. Cell #2 has value 72 which is 'H'
>---. Subtract 3 from Cell #3 to get 101 which is 'e'
+++++++..+++. Likewise for 'llo' from Cell #3
>>. Cell #5 is 32 for the space
<-. Subtract 1 from Cell #4 for 87 to give a 'W'
<. Cell #3 was set to 'o' from the end of 'Hello'
+++.------.--------. Cell #3 for 'rl' and 'd'
>>+. Add 1 to Cell #5 gives us an exclamation point
>++. And finally a newline from Cell #6
[ This program prints "Hello World!" and a newline to the screen, its
length is 106 active command characters. [It is not the shortest.]
This loop is an "initial comment loop", a simple way of adding a comment
to a BF program such that you don't have to worry about any command
characters. Any ".", ",", "+", "-", "<" and ">" characters are simply
ignored, the "[" and "]" characters just have to be balanced. This
loop and the commands it contains are ignored because the current cell
defaults to a value of 0; the 0 value causes this loop to be skipped.
]
++++++++ Set Cell #0 to 8
[
>++++ Add 4 to Cell #1; this will always set Cell #1 to 4
[ as the cell will be cleared by the loop
>++ Add 2 to Cell #2
>+++ Add 3 to Cell #3
>+++ Add 3 to Cell #4
>+ Add 1 to Cell #5
<<<<- Decrement the loop counter in Cell #1
] Loop until Cell #1 is zero; number of iterations is 4
>+ Add 1 to Cell #2
>+ Add 1 to Cell #3
>- Subtract 1 from Cell #4
>>+ Add 1 to Cell #6
[<] Move back to the first zero cell you find; this will
be Cell #1 which was cleared by the previous loop
<- Decrement the loop Counter in Cell #0
] Loop until Cell #0 is zero; number of iterations is 8
The result of this is:
Cell no : 0 1 2 3 4 5 6
Contents: 0 0 72 104 88 32 8
Pointer : ^
>>. Cell #2 has value 72 which is 'H'
>---. Subtract 3 from Cell #3 to get 101 which is 'e'
+++++++..+++. Likewise for 'llo' from Cell #3
>>. Cell #5 is 32 for the space
<-. Subtract 1 from Cell #4 for 87 to give a 'W'
<. Cell #3 was set to 'o' from the end of 'Hello'
+++.------.--------. Cell #3 for 'rl' and 'd'
>>+. Add 1 to Cell #5 gives us an exclamation point
>++. And finally a newline from Cell #6
The above in ONE Line:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
Output:
Hello World!
Let’s code to find Factorial of a Number in Brainfuck:
+++++++++++++++++++++++++++++++++ c1v33 : ASCII code of !
>++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++ c2v61 : ASCII code of =
>++++++++++ c3v10 : ASCII code of EOL
>+++++++ c4v7 : quantity of numbers to be calculated
> c5v0 : current number (one digit)
>+ c6v1 : current value of factorial (up to three digits)
<< c4 : loop counter
[ block : loop to print one line and calculate next
>++++++++++++++++++++++++++++++++++++++++++++++++. c5 : print current number
------------------------------------------------ c5 : back from ASCII to number
<<<<.-.>.<.+ c1 : print !_=_
>>>>> block : print c6 (preserve it)
> c7v0 : service zero
>++++++++++ c8v10 : divizor
<< c6 : back to dividend
[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<] c6v0 : divmod algo borrowed from esolangs; results in 0 n d_n%d n%d n/d
>[<+>-] c6 : move dividend back to c6 and clear c7
>[-] c8v0 : clear c8
>> block : c10 can have two digits; divide it by ten again
>++++++++++ c11v10: divizor
< c10 : back to dividend
[->-[>+>>]>[+[-<+>]>+>>]<<<<<] c10v0 : another divmod algo borrowed from esolangs; results in 0 d_n%d n%d n/d
>[-] c11v0 : clear c11
>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]c13v0 : print nonzero n/d (first digit) and clear c13
<[++++++++++++++++++++++++++++++++++++++++++++++++.[-]] c12v0 : print nonzero n%d (second digit) and clear c12
<<<++++++++++++++++++++++++++++++++++++++++++++++++.[-] c9v0 : print any n%d (last digit) and clear c9
<<<<<<. c3 : EOL
>>+ c5 : increment current number
block : multiply c6 by c5 (don't preserve c6)
>[>>+<<-] c6v0 : move c6 to c8
>> c8v0 : repeat c8 times
[
<<<[>+>+<<-] c5v0 : move c5 to c6 and c7
>>[<<+>>-] c7v0 : move c7 back to c5
>-
]
<<<<- c4 : decrement loop counter
]
Fibonacci Series Program in BrainFuck!:
+++++++++++ number of digits to output
> #1
+ initial number
>>>> #5
++++++++++++++++++++++++++++++++++++++++++++ (comma)
> #6
++++++++++++++++++++++++++++++++ (space)
<<<<<< #0
[
> #1
copy #1 to #7
[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]
<
divide #7 by 10 (begins in #7)
[
>
++++++++++ set the divisor #8
[
subtract from the dividend and divisor
-<-
if dividend reaches zero break out
copy dividend to #9
[>>+>+<<<-]>>>[<<<+>>>-]
set #10
+
if #9 clear #10
<[>[-]<[-]]
if #10 move remaining divisor to #11
>[<<[>>>+<<<-]>>[-]]
jump back to #8 (divisor possition)
<<
]
if #11 is empty (no remainder) increment the quotient #12
>>> #11
copy to #13
[>>+>+<<<-]>>>[<<<+>>>-]
set #14
+
if #13 clear #14
<[>[-]<[-]]
if #14 increment quotient
>[<<+>>[-]]
<<<<<<< #7
]
quotient is in #12 and remainder is in #11
>>>>> #12
if #12 output value plus offset to ascii 0
[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]
subtract #11 from 10
++++++++++ #12 is now 10
< #11
[->-<]
> #12
output #12 even if it's zero
++++++++++++++++++++++++++++++++++++++++++++++++.[-]
<<<<<<<<<<< #1
check for final number
copy #0 to #3
<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]
<- #3
if #3 output (comma) and (space)
[>>.>.<<<[-]]
<< #1
[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-
]
And the no comment neat little block:
+++++++++++
>+>>>>++++++++++++++++++++++++++++++++++++++++++++
>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
>[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
+++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
++++++++++++++++++++++++++++++++++++++++++++.[-]<<
<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]
Link for Brainfuck - C Interpreter:
Link for Brainfuck - Java Interpreter:
BrainFuck Interpreter in Java - GeeksforGeeks
Reference Links:
BrainFuck Interpreter in Java - GeeksforGeeks
brainfuck - Esolang (esolangs.org)
programs in Brainfuck? - Stack Overflow
Brainfuck: code that was designed to hurt | The Outline
Coding Games and Programming Challenges to Code Better (codingame.com)
0 comments:
Post a Comment