diff options
Diffstat (limited to 'examples/add2.bf')
-rw-r--r-- | examples/add2.bf | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/examples/add2.bf b/examples/add2.bf new file mode 100644 index 0000000..42361db --- /dev/null +++ b/examples/add2.bf | |||
@@ -0,0 +1,27 @@ | |||
1 | [ Add two values | ||
2 | from https://en.wikipedia.org/wiki/Brainfuck#Adding_two_values | ||
3 | ] | ||
4 | |||
5 | ++ Cell c0 = 2 | ||
6 | > +++++ Cell c1 = 5 | ||
7 | |||
8 | [ Start your loops with your cell pointer on the loop counter (c1 in our case) | ||
9 | < + Add 1 to c0 | ||
10 | > - Subtract 1 from c1 | ||
11 | ] End your loops with the cell pointer on the loop counter | ||
12 | |||
13 | At this point our program has added 5 to 2 leaving 7 in c0 and 0 in c1 | ||
14 | but we cannot output this value to the terminal since it is not ASCII encoded | ||
15 | |||
16 | To display the ASCII character "7" we must add 48 to the value 7 | ||
17 | We use a loop to compute 48 = 6 * 8 | ||
18 | |||
19 | ++++ ++++ c1 = 8 and this will be our loop counter again | ||
20 | [ | ||
21 | < +++ +++ Add 6 to c0 | ||
22 | > - Subtract 1 from c1 | ||
23 | ] | ||
24 | < . Print out c0 which has the value 55 which translates to "7"! | ||
25 | |||
26 | Finally print a newline: | ||
27 | ---------------------------------------------. | ||