Advertisement
opsftw

BF: BF tutorial

Sep 16th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TUT - BrainFuck - TUT
  2.  
  3. Introduction
  4. 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.
  5.  
  6. How to get BrainFuck
  7. Just google "BrainFuck interpreter" if your on windows and you ill find many.
  8.  
  9. If your on linux, just goto the terminal and type in:
  10. [code]
  11. sudo apt-get install beef
  12. [/code]
  13.  
  14. Basics
  15. ------
  16. 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.
  17.  
  18. These opperatord are
  19. ---------------------
  20. > = increases memory pointer, or moves the pointer to the right 1 block.
  21. < = decreases memory pointer, or moves the pointer to the left 1 block.
  22. + = increases value stored at the block pointed to by the memory pointer
  23. - = decreases value stored at the block pointed to by the memory pointer
  24. [ = like c while(cur_block_value != 0) loop.
  25. ] = if block currently pointed to's value is not zero, jump back to [
  26. , = like c getchar(). input 1 character.
  27. . = like c putchar(). print 1 character to the console
  28.  
  29. TOPOLOGY KEY
  30. ------------
  31. 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:
  32.  
  33. (0) (0) (0) (0) (0) (0) etc... -- represents our cells we are working with
  34. ^  -- represents the pointers location (note: pointer allways starts on the first cell at the beginning of a programm)
  35.  
  36. LOOPS
  37. -----
  38. 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.
  39.  
  40. so we start with this:
  41. (0) (0)
  42. ^
  43.  
  44. An easy way to get 50 is 5 multiplied by 10 right?
  45.  
  46. so lets start off our code by incrementing the first cell to 10
  47. [code]
  48. +++++ +++++
  49. [/code]
  50.  
  51. 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:
  52. (10) (0)
  53. ^
  54.  
  55. ok so ow that were set for our loop lets add in the []'s for the loop:
  56. [code]
  57. +++++ +++++
  58. [
  59.  
  60. ]
  61. [/code]
  62. 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:
  63. [code]
  64. +++++ +++++
  65. [
  66.  >    at the start of the loop we are in the first cell, so lets move over 1 cell to cell 2
  67.  +++++  add 5 to the cell
  68.  <    go back to the first cell
  69.  -    subtract 1 from the first cell
  70. ]
  71. [/code]
  72.  
  73. 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:
  74. (0) (50)
  75. ^
  76.  
  77. Not to hard so far right? just some simple math. but How do we get output?...
  78.  
  79. So how do we pass data to the console (stdout)?
  80. we use the operator '.', periods print out the ASCII equivalent for the value in the current cell to the console (passes it to stdout).
  81.  
  82. Hello World
  83. 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.
  84.  
  85. ASKII CODE
  86. --
  87. H = 72
  88. i = 105
  89. New Line = 10
  90.  
  91. so here's what we got:
  92.  
  93. (0) (0) (0) (0)
  94. ^
  95.  
  96. 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:
  97.  
  98. First, we need to set our first cell to 7 for our loop:
  99. [code]
  100. +++++ ++
  101. [/code]
  102.  
  103. Now our topology looks like:
  104. (7) (0) (0) (0)
  105. ^
  106.  
  107. Now lets set-up the rest of our loop:
  108. [code]
  109. +++++ ++
  110. [
  111.    >  Move to the second cell
  112.    +++++ +++++  add 10 to the second cell
  113.    < Move back to the first cell
  114.    - subtract 1 from the first cell
  115. ]
  116. [/code]
  117.  
  118. Now our topology looks like:
  119. (0) (70) (0) (0)
  120. ^
  121.  
  122. 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:
  123. [code]
  124. >  move to the second cell
  125. ++ add 2 to the cell
  126. [/code]
  127.  
  128. now our total code looks like this so far:
  129. [code]
  130. +++++ ++
  131. [
  132.    >  Move to the second cell
  133.    +++++ +++++  add 10 to the second cell
  134.    < Move back to the first cell
  135.    - subtract 1 from the first cell
  136. ]
  137. >
  138. ++
  139. [/code]
  140.  
  141. And our topology now looks like:
  142. (0) (72) (0) (0)
  143. ..^  Remember we never moved back our pointer yet after we increased it from 70 to 72 so its still on the second cell.
  144.  
  145. So now we want the 3rd cell to be 105 for the 'i'.
  146.  
  147. What's the easiest way to get there? 11 multiplied by 10 then subtract 5 maybe?
  148.  
  149. so lets move our pointer back and set the first cell to 10 for the loop:
  150. [code]
  151. +++++ ++
  152. [
  153.    >  Move to the second cell
  154.    +++++ +++++  add 10 to the second cell
  155.    < Move back to the first cell
  156.    - subtract 1 from the first cell
  157. ]
  158. >
  159. ++
  160. <
  161. +++++ +++++
  162. [/code]
  163.  
  164. And now to make our loop and subtract 5 after:
  165. [code]
  166. +++++ ++
  167. [
  168.    >  Move to the second cell
  169.    ++ ++  add 10 to the second cell
  170.    < Move back to the first cell
  171.    - subtract 1 from the first cell
  172. ]
  173. >
  174. ++
  175. <
  176. +++++ +++++
  177. [
  178.    >> move over to the third cell
  179.    +++++ +++++ + add 11 to the cell
  180.    << move back to the first cell
  181.    - subtract 1 from the cell
  182. ]
  183. >>-----<<
  184. [/code]
  185.  
  186. And our topology now looks like:
  187. (0) (72) (105) (0)
  188. ^
  189. 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:
  190. [code]
  191. >>> move over to the fourth cell
  192. ++ ++ set it to 10
  193. << 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'.
  194. [/code]
  195.  
  196. Now let's at that to our code:
  197. [code]
  198. +++++ ++
  199. [
  200.    >  Move to the second cell
  201.    +++++ +++++  add 10 to the second cell
  202.    < Move back to the first cell
  203.    - subtract 1 from the first cell
  204. ]
  205. >
  206. ++
  207. <
  208. +++++ +++++
  209. [
  210.    >> move over to the third cell
  211.    +++++ +++++ + add 11 to the cell
  212.    << move back to the first cell
  213.    - subtract 1 from the cell
  214. ]
  215. >>-----<<
  216. >>>
  217. +++++ +++++
  218. <<
  219. [/code]
  220.  
  221. Now our topology looks like:
  222. And our topology now looks like:
  223. (0) (72) (105) (10)
  224. ..^
  225.  
  226. All that needs to be done now is to print out the cells (remember we use the '.' operator to print out).
  227.  
  228. 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:
  229. [code]
  230. +++++ ++
  231. [
  232.    >  Move to the second cell
  233.    +++++ +++++  add 10 to the second cell
  234.    < Move back to the first cell
  235.    - subtract 1 from the first cell
  236. ]
  237. >
  238. ++
  239. <
  240. +++++ +++++
  241. [
  242.    >> move over to the third cell
  243.    +++++ +++++ + add 11 to the cell
  244.    << move back to the first cell
  245.    - subtract 1 from the cell
  246. ]
  247. >>-----<<
  248. >>>
  249. +++++ +++++
  250. <<
  251. . to print out the second cell
  252. [/code]
  253.  
  254. 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:
  255. [code]
  256. +++++ ++
  257. [
  258.    >  Move to the second cell
  259.    +++++ +++++  add 10 to the second cell
  260.    < Move back to the first cell
  261.    - subtract 1 from the first cell
  262. ]
  263. >
  264. ++
  265. <
  266. +++++ +++++
  267. [
  268.    >> move over to the third cell
  269.    +++++ +++++ + add 11 to the cell
  270.    << move back to the first cell
  271.    - subtract 1 from the cell
  272. ]
  273. >>-----<<
  274. >>>
  275. +++++ +++++
  276. <<
  277. . to print out the second cell
  278. >. to print out the third cell
  279. >. to print out the fourth cell
  280. [/code]
  281.  
  282. And that's it, that will print out 'hi' and breaks to the net line.
  283.  
  284.  
  285. USER INPUT
  286. 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.
  287.  
  288. So say we want to make a program that someone can type in there name and it will print it back to them?
  289.  
  290. So lets say 10 cells (characters in length) is enough for a first name aye?
  291.  
  292. So all we do is like a ',' then move to the net cell and repeat like so:
  293. [code]
  294. ,>,>,>,>,> ,>,>,>,>, 10 cells to accept input
  295. [/code]
  296.  
  297. 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.
  298.  
  299. 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:
  300. [code]
  301. ,>,>,>,>,>,>,>,>,>, 10 cells to accept input
  302. << now we are back at the 1st cell again
  303. .>.>.>.>.>.>.>.>.>. we print out the 10 cells
  304. [/code]
  305.  
  306. 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:
  307. [code]
  308. ,>,>,>,>,>,>,>,>,>, 10 cells to accept input
  309. << now we are back at the 1st cell again
  310. .>.>.>.>.>.>.>.>.>. we print out the 10 cells
  311. > move to the 11th cell
  312. +++++ +++++ increment it to 10 for New Line
  313. . Print out the New Line
  314. [/code]
  315.  
  316. And there you go
  317.  
  318. 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.
  319.  
  320. Any questions? Please ask
  321.  
  322. ~Tux. (formerly Green Hat)
  323.  
  324. 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