Debugging the Brainbugger
Sterling Camden
I just couldn’t leave it alone. The charm of learning two languages at once by writing an interpreter for one using the other consumed my attention. Furthermore, I had left the door open for adding debugging facilities, and then I found myself needing them.
Daniel Cristonfani’s Fibonacci generator had me hooked. It’s so small, yet it produces a seemingly endless series of Fibonacci numbers in ASCII representation. These soon exceed the storage capacity for 64-bit, yet his program just keeps on spitting them out. I let it run until each one was nearly 1000 digits long.
You may call me a weak geek, but I wasn’t able to decipher what the code was doing in my head, nor with the aid of pencil and paper. So I decided to create a debugger for Brainfuck, so I could step through the code and watch its progress.
You can download the updated Brainfuck interpreter in Lisp with the debugger (ibf.lisp) below. Run that, and you get an “ibf>” prompt. There, you may enter the following commands:
add <code> - Add BF code to the end of the code stream
back - Back up one instruction (does not alter data)
clear - Clear all data cells
code - Display the code, with () around the current instruction
data - Display the data cells, with () around the current cell
del - Delete the current instruction
exit - Exit (same as quit)
go - Execute to the end
help - Display this helpful information
ins <code> - Insert code before the current instruction
load <file>* - Load instructions from file(s)
new - Start over with no instructions or data
quit - Quit (same as exit)
repeat <count> <cmd>- Repeat debugger command the specified number of times
reset - Clear all data cells and rewind to first instruction
rew - Move back to the first instruction (does not alter data)
s - Combination of step;code;data
save <file> - Save instructions to a file
skip - Skip the next instruction (does not alter data)
step - Execute the next instruction
Commands are case-insensitive, and may be combined on the same line separated by semi-colons (;). Thus, to step through Daniel’s Fibonacci generator, the session looks like this:
ibf> load fib.b
ibf> s
>(+)+++++++++>+>+[[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[[-]<[>+<-]>
>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]
]]]]]+>>>]<<<]…
0 (0)
ibf> s
>+(+)++++++++>+>+[[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[[-]<[>+<-]>
>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]
]]]]]+>>>]<<<]…
0 (1)
ibf> s
>++(+)+++++++>+>+[[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[[-]<[>+<-]>
>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]
]]]]]+>>>]<<<]…
0 (2)
ibf>
You can see the current instruction and the current data cell in context but highlighted by parentheses. Using this tool, I found that Daniel keeps the numbers in decimal (one digit per cell) and simply adds 48 to output each one as ASCII. Thus, he’s only limited by the number of cells that a specific Brainfuck engine supports. Typically, that’s 3000 – but my version is only limited by available memory. We could keep on fibbing all day.
Since the debugger supports inserting and deleting code as well as loading from and saving to a file, you could use it as a limited IDE for Brainfuck. The reset command will clear the data and rewind the program counter, so you can rerun your modified program until it works.
Posted in Brainfuck, Lisp |
1 Comment » RSS 2.0 | Sphere it!





[...] Debugging the BrainbuggerHow to clarify Brainfuck code? Just add Lisp!Tags: brainfuck lisp debugger [...]