Appendix C
Operators and precedence
This appendix gathers what chapter 8 says about expressions into a few tables, for reference: the operators and their precedence, the cases at the edges of the 16-bit range, and the values an expression can be built from.
C.1 The operators
Table C.1 lists every operator, grouped by level of precedence. Level 1 binds the tightest: in an expression with more than one operator, the operators of the lower level are worked out first.
| Level | Operator | What it does | Example and value |
| 1 | HIGH | high byte | high 1234h = 12h |
| LOW | low byte | low 1234h = 34h | |
| 2 | * | multiplication | 6*7 = 42 |
| / | division, unsigned | 17/5 = 3 | |
| MOD | remainder of the division, unsigned | 17 mod 5 = 2 | |
| SHL | shift left | 1 shl 4 = 10h | |
| SHR | shift right | 8000h shr 15 = 1 | |
| 3 | - | negation, before a value | -1 = 0FFFFh |
| 4 | + | addition | 2+3 = 5 |
| - | subtraction | 10-4-3 = 3 | |
| 5 | EQ NE | equal, not equal | 1 eq 1 = 0FFFFh |
| LT LE | less than, less or equal | 2 le 1 = 0 | |
| GT GE | greater than, greater or equal | 2 gt 1 = 0FFFFh | |
| 6 | NOT | inverts every bit | not 0 = 0FFFFh |
| 7 | AND | bitwise and | 0f0h and 3ch = 30h |
| 8 | OR | bitwise or | 0f0h or 0fh = 0FFh |
| XOR | bitwise exclusive or | 0ffh xor 0fh = 0F0h |
These rules apply to all of them (section 8.5):
- Operators of the same level are worked out from left to right: 10-4-3 is (10-4)-3, 1 shl 3 * 2 is 10h, and 1 or 2 xor 3 is (1 or 2) xor 3, which is 0.
- Brackets change the order: 2+3*4 is 14, and (2+3)*4 is 20.
- HIGH and LOW apply to the value right after them: high 1234h+1 is (high 1234h)+1, which is 13h.
- Negation binds less tightly than *, / and MOD: -2*3 is -(2*3), and -3/2 is -(3/2), which is −1.
- NOT binds less tightly than the comparisons: not 1 eq 1 is not (1 eq 1), which is 0.
- AND binds more tightly than OR and XOR: 2 or 1 and 0 is 2 or (1 and 0), which is 2.
- The comparisons give 0FFFFh for true and 0 for false, and compare unsigned numbers: -1 gt 1 is true.
- The word operators can be written in upper or lower case, and need a space or a bracket on each side: a and b, not aandb.
These are M80’s levels. When in doubt, add brackets: they cost nothing.
C.2 At the edges
Every value is a 16-bit number, from 0 to 65535, and a negative number is stored as its 16-bit complement (section 8.1). Table C.2 shows what the operators give at the edges of that range.
| Expression | Value | Why |
| 65535+1 | 0 | the value wraps round |
| 0-1 | 0FFFFh | the value wraps round |
| 17/0 | 0FFFFh | division by 0 is not an error |
| 17 mod 0 | 0 | nor is the remainder of it |
| 1 shl 16 | 0 | every bit is shifted out |
| 1 shl 0ffffh | 0 | likewise |
| (-3)/2 | 7FFEh | −3 is 0FFFDh, and division is unsigned |
| (-7) mod 2 | 1 | −7 is 0FFF9h, and the remainder is unsigned |
| -3/2 | 0FFFFh | this is -(3/2): negation comes after / |
| high -1 | 0FFh | −1 is 0FFFFh |
Where a line needs only one byte, such as the operand of ld a,n or of DB, TATARA uses the low byte of the value and does not check that it fits.
C.3 Values
C.3.1 Numbers, characters and the location counter
A number is decimal unless a letter after it names another base (section 8.2). It must start with a digit, so a hexadecimal number that starts with a letter needs a 0 in front.
| Base | Suffix | Example | Value |
| decimal | none, or D | 100, 100d | 100 |
| hexadecimal | H | 100h, 0ffh | 256, 255 |
| octal | O or Q | 100o, 100q | 64 |
| binary | B | 100b | 4 |
B and D are hexadecimal digits as well as suffixes, so always end a hexadecimal number with its H: 10b is 2.
A character constant is one or two characters between apostrophes or quotation marks, and its value is their code: ’A’ is 41h, and "AB" is 4142h, with the first character in the high byte (section 8.3).
$ on its own is the location counter: the address of the line it is written in, in the current segment (section 8.4).
C.3.2 Kinds of value
An expression is absolute, relocatable or external (section 8.6). An absolute value is complete when TATARA assembles it; a relocatable one is an address in a segment, which TANREN finishes; an external one is a name declared with EXTRN. Only these combinations are allowed:
| Expression | Result | Example |
| absolute + relocatable, or the reverse | relocatable | a1+2 |
| relocatable − absolute | relocatable | a1-2 |
| relocatable − relocatable, same segment | absolute | a2-a1 |
| external + absolute, or the reverse | external | far+2 |
| external − absolute | external | far-2 |
Every other operator, including negation, NOT, HIGH and LOW, needs absolute values. An external name can appear only once in an expression. A relocatable or external value can go only where two bytes hold it, as in ld hl,a1 or DW; a one-byte operand must be absolute.