asm-stackfuck
A simple assembly- and brainfuck-inspired stack-based language.
The language has a few goals:
- Be stack-based
- Look like assembly
- Have a similar token set to brainfuck
Tokens
push <int> ; pushes an integer to the stack
add ; pushes the sum of the last two elements on the stack
sub ; pushes the difference of the last two elements on the stack, also sets relevant flags
cmp ; sets the zero flag as if it had subtracted, but doesn't actually perform subtraction
dup ; duplicates the top of the stack
swap ; swaps the top two elements on the stack
rotr ; rotates top three elements right
rotl ; same but to the left
in ; pushes a single character of user input
out ; outputs top element of stack as char
jnz @<section> ; jumps to section if zero flag set with `cmp` or `sub` is not active
jz @<section> ; jumps to section if zero flag is active
jmp @<section> ; jumps to section
Sections
Pretty sure assembly calls them labels. They label a part of the program. The program requires a label called _start
. Lets you do:
_start:
push 64
loop:
push 1
add
dup
out
dup
push 90
cmp
jnz @loop
push 10
out
Which is a program that prints the ascii characters ‘A’ – ‘Z’. Note how references to sections in jump statements are prepended with an @ character.
Compiling and running
The compiler is written in Python. Simply do
chmod +x ./stackfuck.py
./stackfuck.py <program>
nasm -f elf output.asm
ldd -m elf_i386 -s -o output output.o
./output
Try compiling and running the programs in examples/
to see how it works.