Master My First Brainfuck

IMG_6000B
learn how to use and program the My First Brainfuck computer

This is the second chapter – describing how to use the computer. If you want to build one, or understand what it is, read the first post: My First Brainfuck computer

 Intro

The computer is divided in an input section (left side) and output section (right side).

ook5c_ill

The input section is used to:

  • Write a new program (green field)
  • Edit the program (yellow field) – for the advanced user
  • Run the program (magenta field)
  • Input data to a running program (cyan field)

The output section is used to:

  • Display the program output (magenta field)
  • Display the current command  (green field)
  • Display the current position (magenta & green)

Use the keyboard

The menu that controls the function of the keyboard is divided in four groups (rows), each with three functions (columns).

Program group

bf-pgmThe first group is used to add a new Brainfuck command at the program pointer position. Since two keys are required for a new command, audio feedback indicates if the key is the first or the second. After the second key, the command is saved and the program pointer is increased. In the right green field you can see the key combination for every command. Example: “[“ is written by pressing “!” + “?”.

menu1

.                            Is used to write a new command
?                           Is used to write a new command
!                            Is used to write a new command

View/ edit group

The second group is used to move the program position and to change what is displayed on the output.

menu2

nxt             next command  – increase program position
pre             previous command  – decrease program position
typ             change display type – alter between three modes:

      • data (memory value @ memory position) (dat LED)
      • command (program value @ program position) (cmd LED)
      • program position (dat and cmd LED)

Execute/ delete group

The third group is used to execute the program and to delete a command

menu3

run                       run the program – starting from program position
stp                       step the program – run until next data out  “.”
del                       delete the command and move all commands above, one step down
(Works like delete – not like backspace)

Data input / insert group

The last group is used to enter data and to insert a new command. The mode LED will automatically indicate mode4 when a “,” instruction is executed. The 4-bit output will show the value of the current memory position and the data is entered bit by bit with the nxt and inv keys

menu4

nxt                      move to next bit and write the value after the fourth bit.
The current bit is indicated by a flashing light.
inv                       invert the current (flashing) bit. A Long flash means “1” and a short flash                            means “0”
ins                       insert  space for a new command at the program position.
The new command is written using the first menu

How to read the display

The display is divided in two parts; the output (cyan) and the output type (magenta/ green).

The output can have three different meanings:

  1. Data – output from the program
  2. Command – used while reading or editing a program
  3. Program position – used when editing a program to know the current position.

To change what is displayed, press the type key on the second menu (yellow field)

Data (magenta field)

disp1

The output will display program output (from a “.” Instruction) when the dat LED is on and the cmd LED is off. The above example means that value 13 is displayed. (The buzzer will also play a C2 note.)

Command (green field)

disp2

The output will display the current command when the cmd LED is on and the dat LED is off. The above example means that the current command is “<”.

Program position

disp3

The output will display the least significant nibble of the current program position if the dat and cmd LEDs are on at the same time. The above example means that the program position is 3.

The Brainfuck language

Brainfuck was created in 1993 by Urban Müller (It is based on the language “P” with added commands for input and output). The language consists of eight commands, listed below. A Brainfuck program is a sequence of these commands. The program pointer begins at the first command, and each command it points to is executed, after which it normally moves forward to the next command. The program terminates when the program pointer moves past the last command.

Commands

Brainfuck has eight commands, each consisting of a single character:

Group description Brainfuck command Command description
Move memory pointer > increment the memory pointer (to point to the next cell to the right).
< decrement the memory pointer (to point to the next cell to the left).
Change memory value + increment (increase by one) the nibble at the memory pointer.
decrement (decrease by one) the nibble at the memory pointer.
Input/output . output the nibble at the memory pointer.
, accept four bits of input, storing its value in the at the memory pointer.
While loop [ if the nibble at the memory pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
] if the nibble at the memory pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [command.

 

If you are familiar with C, this translation might help you understand the commands better:

brainfuck C equivalent
Program Start char array[30000];
char *ptr=array;
> ++ptr;
< –ptr;
+ ++*ptr;
–*ptr;
. putchar(*ptr);
, *ptr=getchar();
[ while (*ptr) {
] }

 An additional command

In addition to the eight commands of Brainfuck, I added a ninth command “/” (a version of the input that does not halt the program), in order to be able to write games on the My First Brainfuck Computer. Since ook uses 3*3 combinations of “. ! ?” “??” was available. The “/” command reads the keyboard and writes 0 to the memory if no key was pressed at the execution of the command and 1-3 corresponding to the key that was pressed. If you don’t like extra commands and want to write hardcore Brainfuck, simply ignore this command ;)

Write your first program

Here are some programs that are a perfect start to learn and understand the brainfuck program language:

Chromatic scale

+[.+]-[-.]

Bf        +  [  .  +  ]  -  [  -  .  ]
Ook       .. !? !. .. ?! !! !? !! !. ?!

Program description:

Ascending scale:
+         load 1 to pos0 (counter)
[.+       start loop, output data (1), increase pos0 (2)
]         repeat loop until pos0 wraps around and reaches zero.
Descending scale:
–         load 15 to pos0 (counter)
[-.       Start loop, decrease pos0 (14), output data (14)
]         repeat loop until pos0 reaches zero.

Larson scanner (knight rider light)

+>++>++++>++++++++[.<.<.<.>.>.>]

Bf:              +  >  +  +  >  +  +  +  +  >  +  +  +  +  +  +  +  +  [   .  <  .  <  .  <  .  >  .  >  .  >  ]
Ook:             .. .? .. .. .? .. .. .. .. .? .. .. .. .. .. .. .. .. !?  !. ?. !. ?. !. ?. !. .? !. .? !. .? ?!

Program description:

+>++>                      load 1(0001) in pos0, 2(0010) in pos1
++++>++++++++      load 4(0100) in pos2, 8(1000) in pos3
[.<.<.<.                     loop, show pos3, pos 2, pos1, pos0
>.>.>]                       show pos1, pos2, pos3, repeat loop

 Larson scanner smart

>++++>+>>+<<[[>]>[<++>>++<-]<[<]>-]>[.>.>.>.<.<.<]

I thought I was smart and implemented a multiply by 2 algorithm in order to generate the initial memory values for the Larson scanner. The problem is that it actually increases the number of instructions compared to the first version…

 

(I will publish more program examples in the future – stay tuned)

Now it’s up to you to write your own program!
Please share your work by submitting program examples in the comment field below.

See also

·         My First Brainfuck computer

Licensing

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

One Reply to “Master My First Brainfuck”

Leave a Reply