PietASM is a small, textual, assembly-ish language, used for compiling into Piet code files. Largely, commands map one-to-one with Piet commands, although there are exceptions.
Leading whitespace is ignored.
Any text following a #
on a line is ignored as a comment.
# this text is ignored
PUSH 5 # the push is run, but this text is ignored!
If a command corresponds to a Piet command which pops arguments from the stack, the arguments may instead be passed as literals.
eg, these three code examples are equivalent:
PUSH 5
PUSH 3
ADD
PUSH 5
ADD 3
ADD 5 3
These commands are largely unchanged from their Piet definitions.
PUSH *num
- pushnum
onto the stack. Any number of constant arguments may be passed, to be pushed onto the stack in order.POP
- pop and discard the top of the stackDUP
- duplicate the top element of the stackADD
- add the top two elements of the stackSUB
- subtract the top two elements of the stackMUL
- multiply the top two elements of the stackDIV
- divide the top two elements of the stackMOD
- modulo the top two elements of the stackNOT
- replace the top of the stack with 0 if it is nonzero, and 1 if it is zeroGREATER
- pop the top two elements of the stack. Push 1 if the second-top is larger than the top, 0 otherwise.ROLL
- pop the top two elements of the stack. Roll the stack at a depth ofsecond-top
bytop
positions.INNUM
- read a number from stdin and put it on the stackINCHAR
- read a character from stdin and put its ascii value on the stackOUTNUM
- pop the top element of the stack and print it as a numberOUTCHAR
- pop the top element of the stack and print it as an ascii character
STOP
- end execution:FOO
- an identifier prefixed with a colon is a label. Labels may be jumped to (see below). All labels in a file must be unique.JUMP label
- jump to the specifiedlabel
(no colon)JUMPIF label
- pop the top of the stack, and if it is nonzero jump to the specifiedlabel
(no colon)
Note that there are no commands which correspond directly to Piet's switch
and pointer
commands, since the details of the Piet image are left to the PietASM compiler.
@EACH FOO=[1 2 3]
DUP
PUSH @FOO
@END
The @EACH
pragma can be used to duplicate a section of code.
The above code is equivalent to:
DUP
PUSH 1
DUP
PUSH 2
DUP
PUSH 3
The code between the @EACH
and @END
lines is added to the file once for each element between the square brackets.
The bracketed values are assigned to the metavariable defined before the =
, and can be used in place of constants by using the @
prefix.