www.mycpu.eu (C) 2023 by Dennis Kuschel 

MyCPU Understands Brainfuck'd

brainfuck (or short: BF) is some kind of esoteric programming language. It has a minimalistic command set that covers only eight commands. Research has proven that brainfuck is indeed turing complete. That means that every operation can be done with brainfuck, even very complex programs can be written.

>increment the pointer (to point to the next cell to the right)
<decrement the pointer (to point to the next cell to the left)
+increment (increase by one) the byte at the pointer
-decrement (decrease by one) the byte at the pointer
.output the value of the byte at the pointer
,accept one byte of input, storing its value in the byte at the pointer
[jump forward to the command after the corresponding ] if the byte at the pointer is zero
]jump back to the command after the corresponding [ if the byte at the pointer is nonzero

Here is an example for a Hello World program. This is no joke! When you start this program on MyCPU it really outputs the string "Hello World". The program does the following: It pre-computes three values from that the ASCII values for the letters of Hello World are derived. Difficult? No! That's the way brainfuck works. To understand the program you should know that all cells are initialized to zero when the program is started.


The language was developed by Urban Müller in 1993 with the intention of designing a language which could be implemented with the smallest possible compiler. Today many (very simple) brainfuck interpreters are available. Here is an example for a really small one written in C:

char m[9999],*n[99],*r=m,*p=m+5000,**s=n,d,c;main(){for(read(0,r,4000);c=*r;

brainfuck on MyCPU: The MyCPU has a built-in brainfuck compiler. A brainfuck program is compiled to MyCPU machine code in the moment the program is called. This is also known as Just-In-Time-Compilation. MyCPU implements a JIT-Compiler! There are two ways two execute a brainfuck program on MyCPU: implicit and explicit. A brainfuck program is explicitly called when the brainfuck compiler is directly invoked by the built-in command "bf". Assuming the above example program is saved has hello.b, the call would look like this:
8:/brainfuck/> bf hello.b
The BF programs that come with MyCPU can be implicitly called. You can simply enter the program name (e.g. "hello.b") without the prefixed "bf". To tell MyCPU that the program is a BF program that can be compiled and executed, the first two characters in the brainfuck program must be the letters "bf". If the directory where your brainfuck programs are stored is listed in the global search path, the BF programs can be called from anywhere, just like built-in system commands.

Note that some BF programs need some additional input, and some programs will produce output that you might want to save to disk. Please use the standard-I/O-pipes to stream the data.

The brainfuck environment on MyCPU consists of a data memory of 16384 8-bit cells and 32kb of program memory for the compiled BF program. Some applications may need a higher cell width to be executed correctly. Thus it is possible to switch the compiler to generate code that uses 16-bit wide cells. The available data memory is then organized as 8192 16-bit cells. For example the PI-calculation-program requires 16-bit cells to calculate PI correctly:
8:/brainfuck/> yapi.b

8:/brainfuck/> yapi.b -16

The parameter -16 tells the compiler to use 16-bit cells instead of the default 8-bit cells. Note that 16-bit stuff is much slower than the 8-bit code.

To try out brainfuck, you can download the latest MyCPU Emulator. Start the emulator executable, type "R" and wait until MyCPU has come up. Then change into the directory 8:/brainfuck (enter "cd brainfuck") and enter "dir" to display the directory content. You will see several brainfuck program files (ending with ".b"). To start a brainfuck program simply enter it's name, for example "hello.b":

 8:/> cd brainfuck

 8:/brainfuck> dir

 hello.b              108 08-11-05 22:23
 196.b               2921 08-11-06 15:08
 99.b                3058 08-11-06 15:08
 abc.b               1490 08-11-06 15:08
 bfi.b               8705 08-11-06 15:09
 fib.b                445 08-11-06 15:09
 kbfi.b              4994 08-11-06 15:09
 morse.b             2634 08-11-06 15:10
 numwarp.b           1240 08-11-06 15:10
 prime.b             4360 08-11-06 18:38
 quine.b              678 08-11-06 15:15
 squares.b            365 08-11-06 15:16
 text2bf.b            393 08-11-06 15:20
 triangle.b          1697 08-11-06 15:11
 yapi.b              1274 08-11-23 18:10
 readme              2833 08-11-06 18:19
      16 files         37195 bytes
      0 folders     2672384 bytes free

 8:/brainfuck> type hello.b


 8:/brainfuck> hello.b
 Hello World!


Here is a short help about the invocation of the compiler:

 8:/brainfuck> bf /?

 -<Brainfuck>+ compiler for MyCPU
 Syntax: bf bfsourcefile [-16] [-w outfile]
   -16  Use 16 bit data cells (slow)
   -w   Do not execute compiled binary but write it to disk


brainfuck resources:

    Wikipedia article about brainfuck
    BF is Turing-complete
    brainfuck programs