Skip to content

brainfuck

Parodper edited this page Jul 17, 2020 · 6 revisions

BRAINFUCK Interpreter

The interpreter is very slow and doesn't have debug features. So if the code doesn't seem to be working try it on another interpreter. Also the cell and program size is limited by your avaliable program memory. To change the number of cells and possible nested loops modify the value of the variables I and Y respectively

The only input is by the CASIO Basic INPUT function, and the only output its the builtin OUTPUT function

If you exit the code using the AC key execute "0->DimZ" (or [0][Function][3][1][SHIFT][.]) to free the memory

Loop algorithm

  • If the loop starts check if G is set. If not save program location there. If yes push the program location to the stack. If the stack is full exit
  • If the loop ends check if the current cell content is below zero. If yes delete the last program location pushed and continue. If it isnt below zero get the last program location and jump there
  • If the stack is full or empty when a loop ends it will show an error

Flowchart: Flowchart

Characters

Character Numerical value Function
+ 77 Adds one to the current cell value
- 67 Subtracs one from the current cell value
* 24 Increases the cell pointer
÷ 87 Decreases the cell pointer
( 43 Opens loop
) 44 Ends loop
, 45 Gets input and saves it on the current cell overwriting
. 26 Shows the current cell value

Variables

  • A: Key input
  • B: Program counter
  • C: Program size
  • D: Cursor location on the horizontal axis
  • E: Cursor location on the vertical axis
  • F: Cell pointer
  • G: Loop buffer. Holds the first loop starting location
  • H: Loop stack pointer
  • I: Number of cells
  • Y: Loop stack size
  • Z: Z[] size. Z = C + I + Y
  • Z[]: Memory

Gotos

  • 0: Input section
  • 1: Error handling
  • 2: Program reading

Memory Layout

 | Program | Cells   | Loop Stack
 +---------+---------+---------+
|*|0|0|...|*|0|0|...|*|0|-|...|*|
 1-2-3-....C.........I.........Y Z
 1         10        1024      1034
Clone this wiki locally