Monitor: Z-80 assembler (ASM "file", A addr)

Two-pass table-driven assembler in the monitor: assembles BASIC-saved
source files from the file system, or one line at a time with A addr.
Labels, EQU/ORG/DB/DW/DS/END, expressions. Table generated from the
disassembler (tools/gen-asmtab.py); round-trip and sdasz80 tests, plus a
Z-80 build run in z80emu. Monitor RAM grows to 6000-7FFF for the symbol
table. Works around an SDCC 4.5 inline strcpy bug with s_copy().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-28 16:42:42 -08:00
1 parent 88b5942da5
commit fe1daf180d
16 files changed
+14320 -13257

No files matched your search

+74
View File
@@ -595,6 +595,8 @@ Type a command on the bottom line and press Enter. Numbers are in hex.
| `LOAD "NAME"` | Load a memory file back to where it was saved from |
| `LOAD "NAME" addr` | Load it at *addr* instead (8000h or above) |
| `DIR` | List the files in flash in the memory window (`M` brings the memory back) |
| `ASM "NAME"` | Assemble a source file into memory (see the assembler, below) |
| `A addr` | Assemble lines as you type them, from *addr*; an empty line ends |
| Up / Down | Scroll the disassembly by one instruction |
| Page Up / Page Down | Scroll the memory window |
| `?` | A short reminder of the commands |
@@ -623,6 +625,78 @@ the monitor, `B F000` then `X`. Back in BASIC, `CALL 61440` stops at the
breakpoint. Press Enter to step through each instruction, or `X` to let it
finish.
### The assembler
The monitor can assemble Z-80 source code into memory, either a whole file or
one line at a time.
**Writing source.** Type it into BASIC as numbered lines, just like a
program, and save it. BASIC doesn't run it; it just keeps the lines. The
assembler ignores the line numbers, and uses them in its error messages.
```
NEW
10 ; PRINT A MESSAGE
20 ORG 0F000H
30 START: LD HL,MSG
40 LOOP: LD A,(HL)
50 OR A
60 RET Z
70 LD C,A
80 WAIT: IN A,(0) ; CAN THE SCREEN TAKE A CHARACTER?
90 AND 2
100 JR Z,WAIT
110 LD A,C
120 OUT (1),A
130 INC HL
140 JR LOOP
150 MSG: DB "HELLO FROM ASSEMBLY",13,10,0
SAVE "HELLO"
```
A text file sent from the PC with `tools/send-bas.py` works the same way, as
long as each line starts with a number.
**Assembling.** `MONITOR`, then `ASM "HELLO"`. The monitor says
`ASSEMBLED F000-F028` and shows the code; errors are listed in the memory
window with their line numbers. Back in BASIC (`X`), `CALL 61440` runs it.
Or set a breakpoint first and step through it.
**One line at a time.** `A F100` starts the line assembler at F100h: type an
instruction, press Enter, and it is assembled and shown, then the next address
is offered. An empty line ends. Labels from the last `ASM` can be used.
**Syntax**: Zilog mnemonics, as the disassembler shows them.
| | |
|---|---|
| Labels | `NAME:` before an instruction, up to 8 letters and digits; `NAME EQU value` for constants |
| Numbers | `123`, `0FFH`, `$FF`, `0x1F`, `1010B`, `'A'`; `$` alone is the current address |
| Expressions | `+ - * / & |` and brackets: `LD BC,END-START`, `LD A,'Z'-1` |
| Memory operands | an operand all in brackets: `LD A,(COUNT)`, `LD (IX+5),A`, `LD A,(IX)` |
| `ORG addr` | assemble from *addr* (F000h if there is no `ORG`) |
| `DB` / `DEFB` / `DEFM` | bytes and `"strings"`: `DB "HI",13,10,0` |
| `DW` / `DEFW` | 16-bit words, low byte first |
| `DS n` / `DEFS n` | skip *n* bytes |
| `END` | stop here |
| `;` | comment to the end of the line |
- **Instructions:** every Z-80 instruction is accepted, including the
undocumented ones (`IXH`, `SLL`, `IN (C)`). The exception is the
three-operand `RES 0,(IX+d),B` forms. `SUB A,n` is accepted for `SUB n`, and
likewise for `AND`, `XOR`, `OR` and `CP`.
- **Where code can go:** the code must go at 8000h or above. F000h–F3F7h is
the safe place, since BASIC doesn't use it.
- **Labels:** up to 300.
- **Typing source into BASIC:** BASIC turns lower case to upper case (except
inside double quotes), so `'a'` becomes `'A'`. Indentation after the line
number isn't kept, so labels need their colon.
**Errors:** `UNKNOWN INSTRUCTION`, `BAD OPERANDS` (no such form), `UNDEFINED
LABEL`, `DUPLICATE LABEL`, `JUMP TOO FAR` (a `JR`/`DJNZ` beyond −128..127),
`VALUE OVER 255`, `OFFSET OUT OF RANGE`, `CAN'T WRITE BELOW 8000H`, `BAD
NUMBER`, `BAD EXPRESSION`.
### How it works
Pause, `MONITOR`, a breakpoint or the step counter raises the Z-80's
Binary file not shown.