Reusable Labels

It is pretty likely that you will often need a loop label, and even with modules calling them _loop each time gets a bit painful.

A reusable label is made up of a sequence of + plus or - minus symbols. To avoid ambiguity with the addition and subtraction operators, you must surround them in {curly braces}. When you use the label in an expression, the label that is matched is the closest one behind you (in the case of -) or the closest one in front of you (in the case of +). I guess this really needs an example:

    ld b, 10

-: ; This is label (A)

    ld a, b
    call {+}    ; Calls label (B)
    djnz {-}    ; Loops back to label (A)

+: ; This is label (B)

    jr {+}      ; Jumps to label (C)

+: ; This is label (C)

    jp {-}      ; Jumps to label (A)

Pretty cool. If you need a little more flexibility, such as nesting loops, you can lengthen the names of the labels:

    ld b, 20
--:                 ; (A)
    push bc
    ld b, 30
-:                  ; (B)
    ; Do stuff
    djnz {-}        ; Loops to (B)

    pop bc
    djnz {--}       ; Loops to (A)

Another type of reusable label, @, exists. Rather than + and -, you need to specify the offset to it. For example:

@   ; (A)
@   ; (B)

    jp {@}      ; Jump one ahead, to C.
    jp {2@}     ; Jump two ahead, to D.
    jp {-1@}    ; Jump one backwards, to B.
    jp {-2@}    ; Jump two backwards, to A.

@   ; (C)
@   ; (D)