Files
brad ArantandClaude Opus 5.5 fe1daf180d 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>
2026-11-28 16:42:42 -08:00

411 lines
13 KiB
C

/* Z-80 assembler for the monitor (included by monitor.c).
*
* Zilog syntax, as the disassembler prints it. A line is
* [line number] [label:] [instruction or directive] [; comment]
* (the line number is there when the source was typed into BASIC). Numbers:
* 123, 0FFH, $FF, 0x1F, 1010B, 'A'; $ alone is the current address.
* Expressions use + - * / & | and parentheses; an operand that is all in
* parentheses is a memory reference: LD A,(LABEL+1).
* Directives: ORG, EQU (NAME EQU value), DB/DEFB/DEFM (numbers and "strings"),
* DW/DEFW, DS/DEFS n, END. Labels are up to 8 characters.
*
* Instructions are found in asm_tab (asmtab.h, generated from the
* disassembler by tools/gen-asmtab.py): mnemonic and operand pattern give the
* prefix and opcode; displacement and immediate bytes follow.
*/
#include "asmtab.h"
#define MAXSYM 300
struct sym { char name[8]; u16 val; };
static struct sym syms[MAXSYM];
static u16 nsym;
static u16 a_pc; /* address of the line being assembled */
static u16 a_lo, a_hi; /* lowest and highest byte written */
static u8 a_pass; /* 1: sizes and labels, 2: bytes */
static u8 a_undef; /* the expression used an undefined symbol */
static u8 a_end; /* END seen */
static const char *a_err; /* this line's error */
static const char *ap; /* parse pointer */
static void a_out(u16 addr, u8 b); /* write one byte (pass 2) */
static void a_fail(const char *m) { if (!a_err) a_err = m; }
static void a_sp(void) { while (*ap == ' ' || *ap == '\t') ap++; }
static u8 a_alpha(char c) { return (c >= 'A' && c <= 'Z') || c == '_' || c == '.'; }
static u8 a_alnum(char c) { return a_alpha(c) || (c >= '0' && c <= '9'); }
static void a_emit(u8 b)
{
if (a_pass == 2) {
a_out(a_pc, b);
if (a_pc < a_lo) a_lo = a_pc;
if (a_pc > a_hi) a_hi = a_pc;
}
a_pc++;
}
static i16 sym_find(const char *n)
{
u16 i;
for (i = 0; i < nsym; i++)
if (!strncmp(syms[i].name, n, 8)) return i;
return -1;
}
static void sym_def(const char *n, u16 v)
{
i16 i = sym_find(n);
if (a_pass == 2) {
if (i >= 0 && syms[i].val != v) a_fail("PHASE ERROR");
return;
}
if (i >= 0) { a_fail("DUPLICATE LABEL"); return; }
if (nsym == MAXSYM) { a_fail("TOO MANY LABELS"); return; }
strncpy(syms[nsym].name, n, 8);
syms[nsym++].val = v;
}
/* a name at ap (up to 8 characters kept) */
static void a_name(char *n)
{
u8 k = 0;
memset(n, 0, 8);
while (a_alnum(*ap)) { if (k < 8) n[k++] = *ap; ap++; }
}
/* ---------------------------------------------------------- expressions */
static u16 a_expr(void);
static u16 a_number(void)
{
const char *s = ap;
u16 v = 0;
u8 base = 10, n = 0;
if (s[0] == '0' && s[1] == 'X') { base = 16; s += 2; }
else {
while (a_alnum(s[n])) n++;
if (s[n - 1] == 'H') base = 16;
else if (s[n - 1] == 'B') {
u8 k;
for (k = 0; k < n - 1 && (s[k] == '0' || s[k] == '1'); k++);
if (k == n - 1) base = 2;
}
}
for (; a_alnum(*s); s++) {
char c = *s;
u8 d = c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : 99;
if ((c == 'H' && base == 16) || (c == 'B' && base == 2 && !a_alnum(s[1]))) { s++; break; }
if (d >= base) { a_fail("BAD NUMBER"); break; }
v = v * base + d;
}
ap = s;
return v;
}
static u16 a_factor(void)
{
u16 v;
a_sp();
if (*ap == '-') { ap++; return -a_factor(); }
if (*ap == '+') { ap++; return a_factor(); }
if (*ap == '(') {
ap++;
v = a_expr();
a_sp();
if (*ap == ')') ap++; else a_fail("MISSING )");
return v;
}
if (*ap == '\'' && ap[1] && ap[2] == '\'') { v = (u8)ap[1]; ap += 3; return v; }
if (*ap == '$') {
ap++;
if ((*ap >= '0' && *ap <= '9') || (*ap >= 'A' && *ap <= 'F')) {
v = 0;
while ((*ap >= '0' && *ap <= '9') || (*ap >= 'A' && *ap <= 'F'))
v = v * 16 + (*ap <= '9' ? *ap - '0' : *ap - 'A' + 10), ap++;
return v;
}
return a_pc;
}
if (*ap >= '0' && *ap <= '9') return a_number();
if (a_alpha(*ap)) {
char n[8];
i16 i;
a_name(n);
i = sym_find(n);
if (i >= 0) return syms[i].val;
a_undef = 1;
if (a_pass == 2) a_fail("UNDEFINED LABEL");
return 0;
}
a_fail("BAD EXPRESSION");
return 0;
}
static u16 a_term(void)
{
u16 v = a_factor(), w;
for (;;) {
a_sp();
if (*ap == '*') { ap++; v *= a_factor(); }
else if (*ap == '/') { ap++; w = a_factor(); if (w) v /= w; else a_fail("DIVIDE BY 0"); }
else return v;
}
}
static u16 a_expr(void)
{
u16 v = a_term();
for (;;) {
a_sp();
if (*ap == '+') { ap++; v += a_term(); }
else if (*ap == '-') { ap++; v -= a_term(); }
else if (*ap == '&') { ap++; v &= a_term(); }
else if (*ap == '|') { ap++; v |= a_term(); }
else return v;
}
}
/* ------------------------------------------------------------- operands */
#define OC_FIXED 0 /* a register, condition or (HL) etc. */
#define OC_IMM 1 /* a value */
#define OC_MEM 2 /* (value) */
#define OC_IXD 3 /* (IX+d) */
#define OC_IYD 4
struct opnd { u8 cls, kind; u16 val; };
/* Classify the operand in s (n characters, no surrounding blanks). */
static void a_operand(const char *s, u8 n, struct opnd *o)
{
char t[24];
u8 k;
if (n >= sizeof t) n = sizeof t - 1;
memcpy(t, s, n); t[n] = 0;
o->val = 0;
for (k = 1; k < ASM_KN; k++) {
if (k == K_N || k == K_NN || k == K_E || k == K_MN || k == K_MNN || k == K_IXD || k == K_IYD)
continue;
if (!strcmp(t, asm_kn[k])) { o->cls = OC_FIXED; o->kind = k; return; }
}
if (t[0] == '(' && t[n - 1] == ')' && t[1] == 'I' && (t[2] == 'X' || t[2] == 'Y') &&
(t[3] == '+' || t[3] == '-')) {
o->cls = t[2] == 'X' ? OC_IXD : OC_IYD;
t[n - 1] = 0;
ap = t + 3;
o->val = a_expr();
if (*ap) a_fail("BAD OPERAND");
return;
}
if (t[0] == '(' && t[n - 1] == ')') {
/* (a)+(b) is an expression, not memory: the brackets must pair up */
u8 d = 0, j, whole = 1;
for (j = 0; j < n; j++) {
if (t[j] == '(') d++;
else if (t[j] == ')' && --d == 0 && j != n - 1) whole = 0;
}
if (whole) {
t[n - 1] = 0;
ap = t + 1;
o->cls = OC_MEM;
o->val = a_expr();
if (*ap) a_fail("BAD OPERAND");
return;
}
}
ap = t;
o->cls = OC_IMM;
o->val = a_expr();
if (*ap) a_fail("BAD OPERAND");
}
/* Does source operand o fit pattern kind k? */
static u8 a_fits(const struct opnd *o, u8 k, u8 given)
{
if (!k) return !given;
if (!given) return 0;
if (k & 0x80) return o->cls == OC_IMM && o->val == (k & 0x7F);
if (k == K_N || k == K_NN || k == K_E) return o->cls == OC_IMM;
if (k == K_MN || k == K_MNN) return o->cls == OC_MEM;
/* (IX) alone is (IX+0) */
if (k == K_IXD) return o->cls == OC_IXD || (o->cls == OC_FIXED && !strcmp(asm_kn[o->kind], "(IX)"));
if (k == K_IYD) return o->cls == OC_IYD || (o->cls == OC_FIXED && !strcmp(asm_kn[o->kind], "(IY)"));
return o->cls == OC_FIXED && o->kind == k;
}
/* Assemble one instruction: mnemonic m, operands o[0..n-1]. */
static void a_instr(u8 m, struct opnd *o, u8 n)
{
u16 i;
u8 k1, k2, pf, op, len, j;
const struct opnd *imm = 0, *dsp = 0;
u8 ik = 0;
for (i = 0; i < ASM_N; i++) {
if (asm_tab[i][0] != m) continue;
if (a_fits(&o[0], asm_tab[i][1], n > 0) && a_fits(&o[1], asm_tab[i][2], n > 1)) break;
}
if (i == ASM_N) { a_fail("BAD OPERANDS"); return; }
k1 = asm_tab[i][1]; k2 = asm_tab[i][2]; pf = asm_tab[i][3]; op = asm_tab[i][4];
for (j = 0; j < 2; j++) {
u8 k = j ? k2 : k1;
if (k == K_IXD || k == K_IYD) dsp = &o[j];
else if (k == K_N || k == K_NN || k == K_E || k == K_MN || k == K_MNN) { imm = &o[j]; ik = k; }
}
len = pf == 0 ? 1 : pf >= 5 ? 4 : 2;
if (dsp && pf < 5) len++;
if (imm) len += ik == K_NN || ik == K_MNN ? 2 : 1;
if (dsp && (i16)dsp->val != (int8_t)dsp->val) a_fail("OFFSET OUT OF RANGE");
if (imm && (ik == K_N || ik == K_MN) && imm->val > 255 && imm->val < 0xFF80) a_fail("VALUE OVER 255");
switch (pf) {
case 1: a_emit(0xCB); break;
case 2: a_emit(0xED); break;
case 3: a_emit(0xDD); break;
case 4: a_emit(0xFD); break;
case 5: a_emit(0xDD); a_emit(0xCB); a_emit(dsp->val); break;
case 6: a_emit(0xFD); a_emit(0xCB); a_emit(dsp->val); break;
}
a_emit(op);
if (dsp && pf < 5) a_emit(dsp->val);
if (imm) {
u16 v = imm->val;
if (ik == K_E) {
i16 d = (i16)(v - (a_pc + 1));
if (a_pass == 2 && (d < -128 || d > 127)) a_fail("JUMP TOO FAR");
a_emit(d);
} else {
a_emit(v);
if (ik == K_NN || ik == K_MNN) a_emit(v >> 8);
}
}
(void)len;
}
/* the operands at ap, split at commas outside brackets and quotes */
static u8 a_split(struct opnd *o)
{
u8 n = 0;
a_sp();
while (*ap && n < 3) {
const char *s = ap;
u8 d = 0, q = 0, len;
while (*ap && (d || q || *ap != ',')) {
if (*ap == '\'' && !q && ap[1] && ap[2] == '\'') ap += 2;
else if (*ap == '"') q = !q;
else if (*ap == '(') d++;
else if (*ap == ')' && d) d--;
ap++;
}
len = ap - s;
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--;
if (n < 2) {
const char *keep = ap;
a_operand(s, len, &o[n]);
ap = keep;
}
n++;
if (*ap == ',') { ap++; a_sp(); }
}
return n;
}
/* DB/DW items: numbers and strings */
static void a_data(u8 words)
{
for (;;) {
a_sp();
if (*ap == '"' && !words) {
ap++;
while (*ap && *ap != '"') a_emit(*ap++);
if (*ap) ap++;
} else {
u16 v = a_expr();
a_emit(v);
if (words) a_emit(v >> 8);
}
a_sp();
if (*ap != ',') break;
ap++;
}
if (*ap) a_fail("BAD DATA");
}
/* Assemble one source line (uppercase except in quotes). */
static void a_line(char *s)
{
char w[8], lab[8];
struct opnd o[2];
u8 m, n, has_lab = 0;
char *c;
u8 q = 0;
a_err = 0;
/* cut the comment */
for (c = s; *c; c++) {
if (*c == '"') q = !q;
else if (*c == '\'' && !q && c[1] && c[2] == '\'') c += 2;
else if (*c == ';' && !q) { *c = 0; break; }
}
ap = s;
a_sp();
if (*ap >= '0' && *ap <= '9') { /* a BASIC line number */
const char *t = ap;
while (*t >= '0' && *t <= '9') t++;
if (*t == ' ' || !*t) ap = t;
}
a_sp();
if (!*ap) return;
if (!a_alpha(*ap)) { a_fail("SYNTAX ERROR"); return; }
a_name(w);
a_sp();
if (*ap == ':') { /* LABEL: */
ap++;
memcpy(lab, w, 8); has_lab = 1;
a_sp();
if (!*ap) { sym_def(lab, a_pc); return; }
if (!a_alpha(*ap)) { a_fail("SYNTAX ERROR"); return; }
a_name(w);
a_sp();
} else if (!strncmp(ap, "EQU", 3) && !a_alnum(ap[3])) {
memcpy(lab, w, 8); has_lab = 1;
memcpy(w, "EQU\0\0\0\0\0", 8);
ap += 3;
}
if (!strncmp(w, "EQU", 8)) {
u16 v = a_expr();
if (!has_lab) a_fail("EQU NEEDS A NAME");
else if (a_undef && a_pass == 1) a_fail("EQU BEFORE ITS VALUE");
else sym_def(lab, v);
return;
}
if (has_lab) sym_def(lab, a_pc);
if (!strncmp(w, "ORG", 8)) { a_pc = a_expr(); return; }
if (!strncmp(w, "END", 8)) { a_end = 1; return; }
if (!strncmp(w, "DB", 8) || !strncmp(w, "DEFB", 8) || !strncmp(w, "DEFM", 8)) { a_data(0); return; }
if (!strncmp(w, "DW", 8) || !strncmp(w, "DEFW", 8)) { a_data(1); return; }
if (!strncmp(w, "DS", 8) || !strncmp(w, "DEFS", 8)) { a_pc += a_expr(); return; }
for (m = 0; m < ASM_MN; m++)
if (!strncmp(w, asm_mn[m], 8) && strlen(asm_mn[m]) <= 8) break;
if (m == ASM_MN) { a_fail("UNKNOWN INSTRUCTION"); return; }
memset(o, 0, sizeof o);
n = a_split(o);
if (a_err) return;
if (n > 2) { a_fail("TOO MANY OPERANDS"); return; }
/* SUB A,n and friends: the A is optional */
if (n == 2 && o[0].cls == OC_FIXED && !strcmp(asm_kn[o[0].kind], "A") &&
(!strcmp(asm_mn[m], "SUB") || !strcmp(asm_mn[m], "AND") || !strcmp(asm_mn[m], "XOR") ||
!strcmp(asm_mn[m], "OR") || !strcmp(asm_mn[m], "CP"))) {
o[0] = o[1]; n = 1;
}
a_instr(m, o, n);
}
/* Upper-case a source line outside quotes, in place. */
static void a_upper(char *s)
{
u8 q = 0;
for (; *s; s++) {
if (*s == '"') q = !q;
else if (*s == '\'' && !q && s[1] && s[2] == '\'') s += 2;
else if (!q && *s >= 'a' && *s <= 'z') *s -= 32;
if (*s == '\t') *s = ' ';
}
}