![]() |
|
Brainfuck Tutorial - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Coding (https://sinister.ly/Forum-Coding--71) +--- Thread: Brainfuck Tutorial (/Thread-Brainfuck-Tutorial) Pages:
1
2
|
Brainfuck Tutorial - Slarek - 09-15-2013 Brainfuck
Brainfuck is simple, limited and inefficient coding language, which useless to create anything useful. Although you can amaze other people or entertain your brain with it: code is almost never clear and small things need big amount of code, which makes no sense with a quick glance. Where you can find interpreter? There are pretty marginally information about Brainfuck in the Internet and there are not too many interpreters for Brainfuck. Here's a link for working interpreter http://esoteric.sange.fi/brainfuck/impl/interp/i.html. I've made one interpreter with C, but I recommend using the link above. Interpreter is easy to make by yourself if you know other language: everything you need to know about Brainfuck is in this tutorial. Basics Brainfuck program usually uses about 30000 memory slots which usually have the size of one byte; in some cases the size may be different. In every memory slot, there is one integer, always zero at the beginning of the program. The basic operation of this language is to change the memory slot (characters < and >), changing the value by one (characters + and -), printing the slot's value as ASCII-character (.) and reading to memory slot (,) and a loop ( [ ] ) which will be repeated as longs the memory slot's value, which was chosen at the beginning, is non-equivalent to zero. Language is based on these 8 characters and every other character will be ignored. This way you don't have to write anything special to write a comment. Just make sure that your comment won't mess up the code. Changing the memory slot: characters < and > Greater than- and less than-characters are good to understand as an arrow to left and right. They will change the memory slot by one in the certain way. If we get out of the 30000 byte's memory table, the program will either crash or jump to the opposite edge of the table depending on the implementation. At first, the leftmost memory slot is chosen. You can choose whether you call it first or zeroth memory slot, but in this is tutorial we call it the zeroth. Following code simply goes first three memory slots to right and then three back to its starting point: Code: >>> <<<Changing the value: characters + and - Plus and minus-signs increase and decrease the value of the chosen memory slot by one. Since the slot is the size of one byte, it holds the value between [-128, 127] or [0, 255], depends on how you want to read it(if you want read more about this:LINK). When the value range is exceeded, the program will jump back to the first slot. When the program starts, all the slots are set to zero. This code sets the values 0,1,2,3 to first four memory slots of the program(of course you don't have set the 0 value): Code: > + > ++ > +++Data transfer: characters . and , Period prints the memory slot's value and comma reads input from user. Slots are the size of one byte so one slot is one character. The value will be printed as a same sign, which depends on the used characters. A character's numerical value is 65, number 0 is 48. This why we ask 4 characters from user and print them in reverse order: Code: ,>,>,>,
.<.<.<.Loop: characters [ and ] Square brackets defines the loop. From starting square bracket, program will check if memory slot is zero. If it is, loop will be skipped. If not, loop will be run. When program reaches the ending square bracket, it will jump back to the starting bracket. Often it's appropriate to return at the same memory slot where it left, when loop has ended; loop won't do it by itself. This code prints the numbers from 0 to 9. At first we set the zero's value to be 10: loop must be executed 10 times. Then we set the first place's value to be zero's ASCII-code which is 48. Loop will always decrease the zeroth place, print the value of the first place and increase it by one. After ten times there are again zero at the zeroth place so the loop will end. Code: ++++++++++
> ++++++++++++++++ ++++++++++++++++ ++++++++++++++++ <
[->.+<]Summing numbers Summing is laborious. Since you can't move the memory slot's value at once, it must be move to other place one by one. Summing will happen by moving two memory slot's values to the same slot. In this small code we sum up 3 and 4. At first we put 3 to the zeroth slot and 4 to the first slot. In the loop, we decrease from the zeroth place and increase to second and fourth slot until zeroth place's value is zero. This way original value is still in the memory but it's stored in two other slots. Next we move the value of the first slot to second and third slot which makes the value of the second slot to be 7, third slot's value to be 4 and fourth slot's value to 3. And finally we add zero's ASCII-value to every slot which makes them to be printable. Hope this picture makes it even bit more clear: Code: +++>
++++<
[->>+>>+<<<<]
>
[->+>+<<]
++++++++++++++++ ++++++++++++++++ ++++++++++++++++
[->+>+>+<<<]
>>>
.<.<.Complicated example Here's simple program which counts Fibonacci's numbers and prints them in octal. Program has C++-style comments. Since Brainfuck handles all the characters it knows, all the + and - signs couldn't be used in comments. Code: // Print 1 and 1
// One in the zeroth slot and space in slot m13
// "1 1" will be printed
++++++++++++++++ ++++++++++++++++ ++++++++++++++++ + .
>>>>>>>>>>>>> ++++++++++++++++ ++++++++++++++++ .
<<<<<<<<<<<<< .
[-] // zero in the zeroth place
+++++ //How many times two numbers are counted?
>+ >+ << // m1 = 1 and m2 = 1
[-> // while (m0) choose m1
// Numbers movements; & means plus-sign
// m1 m2 m3
// a b
// a&b b
// a&b a & 2b
// a&b a & 2b
// a&b a & 2b
>[-<+>>+<] // m1 &= m2 ja m3 &= m2 ja m2 = 0
<[->+>>+<<<] // m2 &= m1 ja m4 &= m1 ja m1 = 0
>>>[-<<<+>>>] // m1 &= m4 ja m4 = 0
<[-<+>]> // m2 &= m3 ja m3 = 0
// Convert the number into octal format
>[-]>[-]>[-]>[-] // m5 m6 m7 m8 = 0
>[-]>[-]>[-]>[-] // m9 m10 m11 m12 = 0
<<<<<<<<<<<[->>>>+>+<<<<<] // m5 = m6 = m1 ja m1 = 0
>>>>>[-<<<<<+>>>>>] // m1 = m6 ja m6 = 0
<<<<[->>>>>>>+>+<<<<<<<<] // m9 = m10 = m2 ja m2 = 0
>>>>>>>>[-<<<<<<<<+>>>>>>>>] // m2 = m10 ja m10 = 0
<<<<< // choose m5
// Convert m5 into octals and place them into slots m6, m7 and m8
// (m6 = m5) and (m7 = 4 * m5) and (m8 = 32 * m5) and (m5 = 0)
// which means (m8 / 32 = m5 mod 8) and (m7 / 4 = m5 mod 64)
[->+>++++>++++++++++++++++++++++++++++++++<<<]
// (m5 = m7 / 4) and (m6 = m6 minus m7 / 4) and (m7 = 0)
>>[----<<+>->]
// (m7 = m8 / 32) and (m8 = 0)
>[--------------------------------<+>]
// (m8 = m7) ja (m5 = m5 minus m7) and (m7 = 0)
<[->+<<<->>]
// (m7 = m6 / 8) and (m6 = 0)
<[-------->+<]
// (m6 = m5 / 8) and (m5 = 0)
<[-------->+<]
// (m5 = m7 / 8) and (m7 = 0)
>>[--------<<+>>]
// Into printable format
++++++++++++++++++++++++++++++++++++++++++++++++
[-<<+>+>>+<]
// Print the space from m13 and then octal number
>>>>>>.<<<<<<
<<.>.>>.
// Same action for the numbers in slots m9 m10 m11 m12
>[->+>++++>++++++++++++++++++++++++++++++++<<<]
>>[----<<+>->]
>[--------------------------------<+>]
<[->+<<<->>]
<[-------->+<]
<[-------->+<]
>>[--------<<+>>]
++++++++++++++++++++++++++++++++++++++++++++++++
[-<<+>+>>+<]
>>.<<
<<.>.>>.
// Back to the slot m0 and to start of the loop
<<<<<<<<<<<
<]Same program without line breaks or comments Code: ++++++++++++++++ ++++++++++++++++ ++++++++++++++++ +.>>>>>>>>>>>>>+ ++++++++++++++++ +++++++++++++++. <<<<<<<<<<<<<.[- ]+++++>+>+<<[->> [-<+>>+<]<[->+>> +<<<]>>>[-<<<+>> >]<[-<+>]>>[-]>[ -]>[-]>[-]>[-]>[ -]>[-]>[-]<<<<<< <<<<<[->>>>+>+<< <<<]>>>>>[-<<<<< +>>>>>]<<<<[->>> >>>>+>+<<<<<<<<] >>>>>>>>[-<<<<<< <<+>>>>>>>>]<<<< <[->+>++++>+++++ ++++++++++++++++ +++++++++++<<<]> >[----<<+>->]>[- ---------------- ---------------< +>]<[->+<<<->>]< [-------->+<]<[- ------->+<]>>[-- ------<<+>>]++++ ++++++++++++++++ ++++++++++++++++ ++++++++++++[-<< +>+>>+<]>>>>>>.< <<<<<<<.>.>>.>[- >+>++++>++++++++ ++++++++++++++++ ++++++++<<<]>>[- ---<<+>->]>[---- ---------------- ------------<+>] <[->+<<<->>]<[-- ------>+<]<[---- ---->+<]>>[----- ---<<+>>]+++++++ ++++++++++++++++ ++++++++++++++++ +++++++++[-<<+>+ >>+<]>>.<<<<.>.> >.<<<<<<<<<<<<]Epilogue That was Brainfuck. Simple and limited language, that it is hard to use for real programs. It is very good brain bender since you have to think even with the simplest things. If you're interested, please visit here: http://esolangs.org/wiki/Brainfuck RE: Brainfuck Tutorial - noize - 09-15-2013 Quote:Plus and minus-signs increase and decrease the value of the chose memory slot by one. Since the slot is the size of one byte, it holds the value between[-128, 127] or [0, 255] depends on how you want to read it. When the value range is exceeded, the memory slot rotates around. I already found that kind of range in SQL, but would you mind explaining that a bit more in depth? Because, to a beginner, that would sound nonsense at all, and I'd like myself to read more about it. I really don't get what you mean with "rotates around". RE: Brainfuck Tutorial - Slarek - 09-15-2013 (09-15-2013, 09:58 AM)noize Wrote:Quote:Plus and minus-signs increase and decrease the value of the chose memory slot by one. Since the slot is the size of one byte, it holds the value between[-128, 127] or [0, 255] depends on how you want to read it. When the value range is exceeded, the memory slot rotates around. So sorry. I can't always express myself in English. What part you didn't understand in that upper quote? And rotates around means, that if the slot range is 0-100 then and the program reaches the 100th slot, it will go back to 0. RE: Brainfuck Tutorial - noize - 09-15-2013 (09-15-2013, 10:02 AM)Slarek Wrote:(09-15-2013, 09:58 AM)noize Wrote:Quote:Plus and minus-signs increase and decrease the value of the chose memory slot by one. Since the slot is the size of one byte, it holds the value between[-128, 127] or [0, 255] depends on how you want to read it. When the value range is exceeded, the memory slot rotates around. I see what you meant with "rotates around". For the other part, I'd like to know when the first range is used and when the second ("depends on how you want to read it" - I don't get this). I know one byte can't hold a range larger than 255, as a byte is made of 8 bits (in binary 255 is 11111111), but when one range is used instead of the other? RE: Brainfuck Tutorial - Slarek - 09-15-2013 (09-15-2013, 10:11 AM)noize Wrote:(09-15-2013, 10:02 AM)Slarek Wrote:(09-15-2013, 09:58 AM)noize Wrote:Quote:Plus and minus-signs increase and decrease the value of the chose memory slot by one. Since the slot is the size of one byte, it holds the value between[-128, 127] or [0, 255] depends on how you want to read it. When the value range is exceeded, the memory slot rotates around. I meant that do you prefer reading it as one byte's range is [-128, 127] or do you want to think that one byte is [0, 255]. I think i'll remove it since it caused confusion RE: Brainfuck Tutorial - noize - 09-15-2013 (09-15-2013, 10:15 AM)Slarek Wrote: I meant that do you prefer reading it as one byte's range is [-128, 127] or do you want to think that one byte is [0, 255]. I think i'll remove it since it caused confusion I'd rather like if you left it, just, maybe adding a line to explain the difference between signed and unsigned bytes. RE: Brainfuck Tutorial - noize - 09-15-2013 I'm backfeeding as I go through it. Now I found this: (09-15-2013, 08:36 AM)Slarek Wrote: Data transfer: characters . and , First thing is that until the last line ("This way we ask..."), I didn't realize what the comma char was used for. Maybe you could replace "read" with "read input from user", because I got it just reading that. It is not very explicit that the binary value stored in the memory slot represents a (respective decimal) ASCII code (ASCII codes are well-known for their decimal form, but here they're stored in binary form, wrong?). You never even use the word "ASCII", and I would remove the "Usually", 'cause the ASCII alphabet is not changing. RE: Brainfuck Tutorial - Slarek - 09-15-2013 (09-15-2013, 10:49 AM)noize Wrote: I'm backfeeding as I go through it. Now I found this: What have i thought? No clue. Thanks for reply RE: Brainfuck Tutorial - Deque - 09-17-2013 Well done paper. I would like to add your tutorial to our language introduction compilation. Do you allow me that? I disagree with one thing, though. Brainfuck is not a complicated language, actually it is the opposite. It is so simple and limited, that it is hard to use for real programs. RE: Brainfuck Tutorial - Slarek - 09-18-2013 @Deque That would be an honor. I'll fix that, thank you. |