Z80 assembler


Assembler directives

db or defb

db stands for "define byte". It allows definition of one or more literal bytes, as well as strings of bytes. All definitions should be separated by commas. Strings of bytes should be between double quotes. Example code:
label: equ 20
defb "This is a text", label/2, "This is more text", 10, 0

dw or defw

dw stands for "define word". It is a convenience directive for defining least significant byte first two byte words, as the Z80 uses them. Multiple expressions, separated by commas, may be specified. Example:
org 0x8000
pointertable: defw sub1, sub2
sub1: sub b
ret nz
sub2: ld h,0
ret

ds or defs

ds stands for "define space". It takes one or two arguments, num and val. It reserves num bytes of space and initializes them to val. If val is omitted, it defaults to 0. Example:
buffer: defs 20
sevens: defs 10, 7

include

As in C (but without the #), this includes an other source file. No substitution at all is done on the filename, which means that ~ cannot be used to refer to the home directory. Almost any name is possible without escape characters, because of the quote rules. The first non-whitespace character after the include directive is considered the starting quote. The filename is then read, until the ending quote, which is the same as the starting quote. Example:
include 'math.asm'
include -file'with"quotes".asm-
include zletter as quotes and spaces in name.asmz

if, else, endif

Parts of the code can be omitted using these conditional statements. else can be repeated as many times as desired. Code which is not assembled is checked for a correct command. Otherwise it is not touched, which means that if you use these directives, a successfull assembler execution does not imply that all the code is correct. Example:
org 0x8000
include "math.asm"
if $ < 0x9000 ; Only do the following if math.asm is small enough
ld a,3
else
ld a,6
else
;this is also only assembled if math.asm is small enough
ld h,8
endif
;this is always assembled
call math_init