BFASM
- Not to be confused with asm2bf.
| Paradigm(s) | Imperative |
|---|---|
| Designed by | User:Waffelz |
| Appeared in | 2025 |
| Memory system | Variable-based |
| Dimensions | One-dimensional |
| Computational class | Unknown |
| Major implementations | See [1] |
| File extension(s) | *.bfasm |
About
BFASM is an imperative, assembly-like language designed by User:Waffelz that compiles directly to brainfuck. Its name comes from the words "brainfuck" and "assembly". As of v0.1, it currently has 12 statements. Although the preferred file extension for BFASM programs is *.bfasm, the current implementation can accept any file type.
Statements
| Statement | Function |
|---|---|
| set %x v | Set the variable %x to the value provided. Variable names must start with %. This is currently the only command that takes a number literal as an argument. |
| add %x %y %z | Compute %x + %y and store it in %z. |
| sub %x %y %z | Compute %x - %y and store it in %z. |
| mul %x %y %z | Compute %x * %y and store it in %z. |
| div %x %y %z | Compute %x / %y and store it in %z, rounding down if needed, |
| mod %x %y %z | Compute %x % %y and store it in %z. |
| out %x | Output the ASCII character stored in %x. |
| inp %x | Input an ASCII character and store it in %x. |
| cmp %x %y %z %w %v | Compare %x to %y. If %x = %y, set %z to 1. If %x > %y, set %w to 1. If %x < %y, set %v to 1. All other arguments are set to zero. |
| if %x | If %x is nonzero, execute the code until the matching end once, else skip it. |
| while %x | If %x is nonzero, execute the code until the matching end until it is, else skip it. |
| end | Close the matching if or while statement. |
Memory Structure
BFASM abstracts away all of brainfuck's pointer arithmetic and handles everything internally. Variables are reserved at the beginning of the tape in the order that they are defined. To perform operations, all operands are cloned to the next available free spaces, the result is computed, and is them moved into the target variable. A single cell at the start of the tape is reserved as %_
- A visual diagram of the tape structure after compiling a BFASM program.
Syntax/Semantics
Programming in BFASM is relatively simple. Every line in BFASM contains one statement, and every argument is separated by a space. Each line is trimmed, so whitespace at the beginning or end of a line is ignored. The current implementation (see [1]), as of v0.1, has an okay amount of error checking.
| Error | Cause |
|---|---|
| Too many or too little tokens on line {n} | The number of arguments given on line {n} does not match the expected amount. |
| Variables must start with '%' (found '{v}' on line {n}) | Variable {v} was initialized without a percent sign on line {n}. |
| Too many variables (max 10,000, line {n}) | Line {n} crosses over the variable limit of 10,000. |
| Expected number, got '{v}' on line {n} | Found {v} when expecting a number on line {n}. Currently only the set statement can cause this. |
| Too many or too little arguments | The number of arguments given to the executable does not match the expected amount. |
| Failed to open file {c} | For whatever reason, the file given to the executable could not be opened. |
| Found 'if'/'while' statement with no matching 'end' statement (line {n}) | The if or while statement on line {n} does not have a matching end statement. |
| Found 'end' statement with no matching 'if'/'while' statement (line {n}) | The end statement on line {n} does not have a matching if or while statement. |
| %_' is a reserved name (line {n}) | On line {n}, a variable with the name %_ was declared. |
| Value given to 'set' command must be between 0 and 255 (found {v}, line {n}) | The value given to the set command on line {n} is not between 0 and 255. |
All other errors silently continue with compilation.
Computational Class
While waffelz, the creator of BFASM, strongly believes it has equivalent power to a finite-state automaton, he has been unable to prove it.
Example Programs
Brainfuck code given is split by every 100 commands. Programs that take any input have sample inputs denoted with >>>
Print "Hello, World!"
BFASM Code
set %c 72 out %c set %c 101 out %c set %c 108 out %c out %c set %c 111 out %c set %c 44 out %c set %c 32 out %c set %c 87 out %c set %c 111 out %c set %c 114 out %c set %c 108 out %c set %c 100 out %c set %c 33 out %c
Generated Brainfuck (1.068 kB)
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..[-]+++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++.[-]++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-] ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++.
Output
Hello, World!
Cat
BFASM Code
set %t 1 set %i 0 while %t inp %i out %i end
Generated Brainfuck (8 B)
>+[>,.<]
Output
>>> h h >>> i i >>> 1 1 >>> 2 2 >>> $ $
Decimal Output
BFASM Code
set %i 0
inp %i
set %v 0
add %v %i %v
set %d 10
set %ones 0
set %tens 0
set %hund 0
mod %v %d %ones
div %v %d %v
mod %v %d %tens
div %v %d %v
add %hund %v %hund
set %= 0
set %> 0
set %< 0
set %d 48
add %hund %d %hund
add %tens %d %tens
add %ones %d %ones
set %d 99
cmp %i %d %= %> %<
if %>
out %hund
end
set %d 9
cmp %i %d %= %> %<
if %>
out %tens
end
out %ones
Generated Brainfuck (2.936 kB)
>,>[->+>+<<]>>[-<<+>>]<<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<[-<+>]<<[-]>[-<+>]++++++++++<[->>>>>+>+<<<<<< ]>>>>>>[-<<<<<<+>>>>>>]<<<<<[->>>>>+>+<<<<<<]>>>>>[-<<<<<+>>>>>]<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>[ -]>[-]>>[-]<<<<<<<[-]>>>>>>[-<<<<<<+>>>>>>]<<<<<<<<[->>>>>+>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>]<<<<<[->>> >>+>+<<<<<<]>>>>>[-<<<<<+>>>>>]<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>[-]>[-]>[-]<<<<<<<<[-]>>>>>>>>>[-< <<<<<<<<+>>>>>>>>>]<<<<<<<<<[->>>>>+>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>]<<<<<[->>>>>+>+<<<<<<]>>>>>[-<<<< <+>>>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]<<]<[->>+<]>>[<<<->+>>[-]]>[-]<<<<<<<[-]>>>[-<<<+>>>]<<[-]>>>[-<<<+>>>]<<[-]>>>[-<<<+>>>]<< <<<<<<<<<<[-]>>>>>>>>[->>+>+<<<]>>>[-<<<+>>>]<<<<<<<<<<<[->>>>>>>>>>>+>+<<<<<<<<<<<<]>>>>>>>>>>>>[-< <<<<<<<<<<<+>>>>>>>>>>>>]<[-<+>]<<<<<<<<<<<[-]>>>>>>>>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<<<<<<<[>>>>>>.<< <<<<[-]]>>>[-]+++++++++<<[->>>>>>>>>>>>+>+<<<<<<<<<<<<<]>>>>>>>>>>>>>[-<<<<<<<<<<<<<+>>>>>>>>>>>>>]< <<<<<<<<<<[->>>>>>>>>>>+>+<<<<<<<<<<<<]>>>>>>>>>>>>[-<<<<<<<<<<<<+>>>>>>>>>>>>]<<<<<+[>>>>[-<]<<]<[- >>+<]>>[<<<->+>>[-]]>[-]<<<<<<<[-]>>>[-<<<+>>>]<<[-]>>>[-<<<+>>>]<<[-]>>>[-<<<+>>>]<<<<<<<<<<<<[-]>> >>>>>>[->>+>+<<<]>>>[-<<<+>>>]<<<<<<<<<<<[->>>>>>>>>>>+>+<<<<<<<<<<<<]>>>>>>>>>>>>[-<<<<<<<<<<<<+>>> >>>>>>>>>]<[-<+>]<<<<<<<<<<<[-]>>>>>>>>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<<<<<<<[>>>>>.<<<<<[-]]>>>>.
Output
>>> a 97
Examples larger than 5 kB can be found here.
External Links
- [1] (C++ Implementation) GitHub Repo