The Z80 assembler


Expressions are very powerful in the assembler. The following operators are allowed (in order of precedence):
a?b:cTernary operator, as in C.
a|bBitwise or.
a^bBitwise xor.
a&bBitwise and.
a==b and a!=bEquality.
a<b and a>b and a<=b and a>=bInequality.
a<<b and a>>bBitshifts.
a+b and a-bBinary addition and subtraction.
a*b and a/b and a%bMultiply, divide and modulo.
~a and +a and -aBitwise not and negation.
number or labelLiterals.
Watch out for the parenthesis. The command:
ld a, (3*5)+1
will result in an error, because the parser recognizes "ld a,(". Then it will parse until the ")", and find some junk: "+1". A workaround for such a situation is to prefix the expression with "0+".

Labels in expressions

Expression evaluation is a very complex part of the assembler. The implementation tries to allow everything which is possible. This is done in the following way: In all expressions, labels can be used. When an expression is encountered, it is computed. If the computation fails, it is scheduled to be computed at the end of assembling, when all label values are known. If a label is assigned a value (with equ), and the computation fails, it is put in a different queue. Recomputation is tried every time the label is used. If it fails, then the expression using the label also fails. This way labels may be defined in terms of other labels which are defined later. Recursive definitions are not allowed.

There are some expressions however, which must be calculated at the moment they occur, usually because the address of the next command depends on them. Those expressions are:


Literals are also quite flexible. The following literals can be distinguished:

Variable base numbers

Numbers can be specified in any base (radix) from 2 to 37. To use this, the number should start with a "@". The first character after the "@" defines the radix of the number. After that (and optional whitespace), the number itself is specified.

The radix defining character is the expression "10 - 1", in the desired base, so it is the highest digit that can be used in that base. After 9, letters a-z follow. For example, to write a hexadecimal number, it is possible to use "@f 1c", which is the same as "0x1c".