Skip to the content.

gbforth assembler

gbforth includes an assembler. This is useful when you want more control about how the hardware is used or you need to optimize some piece of code.

You can combine Forth and Assembler as you please. You can even write all your game in assembler, yet having the power of all the Forth at compile-time.


gbforth’s assembler lives in a separate vocabulary, which can be accessed using the words [asm] and [endasm].

It provides a traditional postfix notation. Operands are specified first, and then the instruction. For example, the code below will move the number 42 to the register HL.


42 # HL ld,


To learn more, you can have a look to the hello world example written in assembler, or the the lib/ directory.


Operands are kept in an auxiliary stack. Registers and flags push themselves to this stack, immediate values have to be pushed manually.


You can push immediate values by using the word #. You can use prefixes to specify the base of your immediate values.


42 # HL ld,     \ decimal
$ff # HL ld,    \ hex
%1011 # HL ld,  \ binary



Registers like A, B, SP, or HL push themselves to the operand stack.

Some registers also support memory indirection. In that case, the register is wrapped in square brackets: [BC], [DE], [HL], [C].

Finally, there are the operands [HL+] and [HL-]. This means that the register HL will be used to reference the memory, and then the register HL will be incremented or decremented.


Flags, like registers, push themselves to the operand stack.

There are 4 flags available to make an instruction conditional:

These flags can be used in combination with the instructions call, and jp, and jr, and ret,.


$1234 # #NZ jp,  \ jump if last result was non-zero
#C ret,          \ return if carry flag is set


Memory references

Memory references can be pushed to the operand stack using the word ]*.


$FF40 ]* A ld,  \ load value at address $FF40 into A



Labels can be created using the word label. These labels are simple constants, and can only be referenced after they are defined:


label countToZero
A dec,
countToZero #NZ jp,


For local references however (like in the example above), you are encouraged to make use of two other concepts that the gbforth assembler provides: stack-based references, and structured control flow. These also support cases where you need to forward-reference an address (which is not possible with label).

Anonymous stack-based references

gbforth’s assembler provides word pairs to create anonymous stack-based references:

They can be used to implement simple loops or jumps, when there is no need to give a name to the reference:


( backward reference )
A dec,
<there #NZ jp,

( forward reference )
there> #C jp,
B inc,


For cases where you need a long forward reference, you can use the word named-ref> to assign a name to the reference:


there> #Z jp,
named-ref> longFwdJump

( ... )

longFwdJump \ replaces >here


Structured control flow

The assembler exposes a few words similar to control flow words available in Forth:

The words if,, until, and while, have to be combined with one of the flag operands (because they are essentially jp, instructions).

These can be used to structure the control flow without using the underlying references directly:


  A dec,
#Z until,

#NC if,
  B inc,
  B dec,