Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TUT - BrainFuck - TUT
- Introduction
- BrainFuck is probably the most crazy simplest, hardest, ridiculous, coolest Language I have come across thus far. There is lots of material out there on it but it all seems to mostly be abstract theory over it and there are not many tutorials out there on it so I thought I would provide you with one because the language itself is actually very easy to grasp. Also keep in mind, it;s an esoteric language, it's meant as a challenge.
- How to get BrainFuck
- Just google "BrainFuck interpreter" if your on windows and you ill find many.
- If your on linux, just goto the terminal and type in:
- [code]
- sudo apt-get install beef
- [/code]
- Basics
- ------
- The idea behind brainfuck is memory manipulation. Basically you are given an array of 30,000 1byte memory blocks. The array size is actually dependent upon the implementation used in the compiler or interpretor, but standard brainfuck states 30,000. Within this array, you can increase the memory pointer, increase the value at the memory pointer, etc. Now in BF you only have 8 operators to work with, anything else is counted as comment and is ignored by the interpreter.
- These opperatord are
- ---------------------
- > = 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
- TOPOLOGY KEY
- ------------
- For the examples I will be drawing out graphically the cells to how visually what is going on, this it what it will look like:
- (0) (0) (0) (0) (0) (0) etc... -- represents our cells we are working with
- ^ -- represents the pointers location (note: pointer allways starts on the first cell at the beginning of a programm)
- LOOPS
- -----
- loops run until the cell that the pointer was initially in at the beginning of the loop is set to 0. so say we are making a loop to try to jump the second cell to 50. We will be using the first 2 cells.
- so we start with this:
- (0) (0)
- ^
- An easy way to get 50 is 5 multiplied by 10 right?
- so lets start off our code by incrementing the first cell to 10
- [code]
- +++++ +++++
- [/code]
- We have 10 +'s so this increases the cell witch the pointer is currently being pointed at to 10 (I put them into groups of 5 to make it visually more appealing, but remember, at execution the space there is ignored) and now our cell topology looks like:
- (10) (0)
- ^
- ok so ow that were set for our loop lets add in the []'s for the loop:
- [code]
- +++++ +++++
- [
- ]
- [/code]
- But what do we fill it with? We want to get to 50 right? do why not each loop increase the second cell by 5 and decrease the first by 1? how? like this (remember, < and > are to move cells and the comments in there would be stripped out by the interpreter:
- [code]
- +++++ +++++
- [
- > at the start of the loop we are in the first cell, so lets move over 1 cell to cell 2
- +++++ add 5 to the cell
- < go back to the first cell
- - subtract 1 from the first cell
- ]
- [/code]
- now this will keep running until our initial cell we started on (the first cell) is set to 0, ad now we end up with this as our cell topology:
- (0) (50)
- ^
- Not to hard so far right? just some simple math. but How do we get output?...
- So how do we pass data to the console (stdout)?
- we use the operator '.', periods print out the ASCII equivalent for the value in the current cell to the console (passes it to stdout).
- Hello World
- ok so lets start our code, in our code we will be printing out 'Hi' so we will only be using the first 4 cells for our program.
- ASKII CODE
- --
- H = 72
- i = 105
- New Line = 10
- so here's what we got:
- (0) (0) (0) (0)
- ^
- So first we need to get that second cell to 72 so we can print out or h. So what's the quickest way to get there? How about 10 multiplied by 7 witch would be 75 then add 2? Lets do it:
- First, we need to set our first cell to 7 for our loop:
- [code]
- +++++ ++
- [/code]
- Now our topology looks like:
- (7) (0) (0) (0)
- ^
- Now lets set-up the rest of our loop:
- [code]
- +++++ ++
- [
- > Move to the second cell
- +++++ +++++ add 10 to the second cell
- < Move back to the first cell
- - subtract 1 from the first cell
- ]
- [/code]
- Now our topology looks like:
- (0) (70) (0) (0)
- ^
- SO now we just need to move over to the second cell and add 2 more and we will end up with 72 by adding this to the end of our code:
- [code]
- > move to the second cell
- ++ add 2 to the cell
- [/code]
- now our total code looks like this so far:
- [code]
- +++++ ++
- [
- > Move to the second cell
- +++++ +++++ add 10 to the second cell
- < Move back to the first cell
- - subtract 1 from the first cell
- ]
- >
- ++
- [/code]
- And our topology now looks like:
- (0) (72) (0) (0)
- ..^ Remember we never moved back our pointer yet after we increased it from 70 to 72 so its still on the second cell.
- So now we want the 3rd cell to be 105 for the 'i'.
- What's the easiest way to get there? 11 multiplied by 10 then subtract 5 maybe?
- so lets move our pointer back and set the first cell to 10 for the loop:
- [code]
- +++++ ++
- [
- > Move to the second cell
- +++++ +++++ add 10 to the second cell
- < Move back to the first cell
- - subtract 1 from the first cell
- ]
- >
- ++
- <
- +++++ +++++
- [/code]
- And now to make our loop and subtract 5 after:
- [code]
- +++++ ++
- [
- > Move to the second cell
- ++ ++ add 10 to the second cell
- < Move back to the first cell
- - subtract 1 from the first cell
- ]
- >
- ++
- <
- +++++ +++++
- [
- >> move over to the third cell
- +++++ +++++ + add 11 to the cell
- << move back to the first cell
- - subtract 1 from the cell
- ]
- >>-----<<
- [/code]
- And our topology now looks like:
- (0) (72) (105) (0)
- ^
- So now all we need is to set the 4th cell to 10 for the New Line. Sense it's only 10 we can do this without a loop like so:
- [code]
- >>> move over to the fourth cell
- ++ ++ set it to 10
- << move back to our second cell to get ready to start printing out characters. remember, the second cell holds 72 witch is ASKII for 'H'.
- [/code]
- Now let's at that to our code:
- [code]
- +++++ ++
- [
- > Move to the second cell
- +++++ +++++ add 10 to the second cell
- < Move back to the first cell
- - subtract 1 from the first cell
- ]
- >
- ++
- <
- +++++ +++++
- [
- >> move over to the third cell
- +++++ +++++ + add 11 to the cell
- << move back to the first cell
- - subtract 1 from the cell
- ]
- >>-----<<
- >>>
- +++++ +++++
- <<
- [/code]
- Now our topology looks like:
- And our topology now looks like:
- (0) (72) (105) (10)
- ..^
- All that needs to be done now is to print out the cells (remember we use the '.' operator to print out).
- so since we are in the second cell already all e need to do is add a '.' to the end of our code to print out th 2nd cell witch holds our 'H' like this:
- [code]
- +++++ ++
- [
- > Move to the second cell
- +++++ +++++ add 10 to the second cell
- < Move back to the first cell
- - subtract 1 from the first cell
- ]
- >
- ++
- <
- +++++ +++++
- [
- >> move over to the third cell
- +++++ +++++ + add 11 to the cell
- << move back to the first cell
- - subtract 1 from the cell
- ]
- >>-----<<
- >>>
- +++++ +++++
- <<
- . to print out the second cell
- [/code]
- then we just move over one to print the 'i' and then over one more and print the next line, so our finished code should look like:
- [code]
- +++++ ++
- [
- > Move to the second cell
- +++++ +++++ add 10 to the second cell
- < Move back to the first cell
- - subtract 1 from the first cell
- ]
- >
- ++
- <
- +++++ +++++
- [
- >> move over to the third cell
- +++++ +++++ + add 11 to the cell
- << move back to the first cell
- - subtract 1 from the cell
- ]
- >>-----<<
- >>>
- +++++ +++++
- <<
- . to print out the second cell
- >. to print out the third cell
- >. to print out the fourth cell
- [/code]
- And that's it, that will print out 'hi' and breaks to the net line.
- USER INPUT
- for input we use the ',' operator witch accepts exactly one ASCII character, translated it to its representative number, ad stores it in the current cell.
- So say we want to make a program that someone can type in there name and it will print it back to them?
- So lets say 10 cells (characters in length) is enough for a first name aye?
- So all we do is like a ',' then move to the net cell and repeat like so:
- [code]
- ,>,>,>,>,> ,>,>,>,>, 10 cells to accept input
- [/code]
- NOTE: in the program, when the user hits enter, if tey haven't used all the cells up it will just skip over the excess.
- Now we want to print it out? so we move back to the first cell then move accross the cells and print them out using the '.' operator like so:
- [code]
- ,>,>,>,>,>,>,>,>,>, 10 cells to accept input
- << now we are back at the 1st cell again
- .>.>.>.>.>.>.>.>.>. we print out the 10 cells
- [/code]
- And just to make it look better in the console, let's add a new line ok? so lets move to the 11th cell, increment it to 10 as that's the ASCII value for New Line. Like this:
- [code]
- ,>,>,>,>,>,>,>,>,>, 10 cells to accept input
- << now we are back at the 1st cell again
- .>.>.>.>.>.>.>.>.>. we print out the 10 cells
- > move to the 11th cell
- +++++ +++++ increment it to 10 for New Line
- . Print out the New Line
- [/code]
- And there you go
- Well that's just the basics, I will be making a second TUT on implementing conditional statements later on depending on if it's wanted or not.
- Any questions? Please ask
- ~Tux. (formerly Green Hat)
- NOTE: if you see any spelling mistakes or incorrect/misinformation then please tell me. Thank-You
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement