Assembler in GNU as syntax, objdump-style disassembly, UCASE$ and LCASE$

The monitor's assembler takes GNU as directives (.org, .equ, .byte, .half,
.word, .ascii, .string, .space, .align, .balign ...), # comments, 0x/0b
numbers, '\n' constants, . for the current address, << >> ^ ~, %hi/%lo and
the FP CSR short forms; the old ORG/DB/EQU/0FFH forms remain as aliases. The
disassembler prints lower case and 0x hex as objdump does. The monitor is
built with -msave-restore and puts .rodata first, to fit its ROM.
BASIC gains UCASE$ and LCASE$. Manual, README and tests updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-12-01 18:14:19 -08:00
1 parent 25c8286cf2
commit fa4752fd7a
16 files changed
+7921 -7615

No files matched your search

+167 -55
View File
@@ -1,26 +1,37 @@
/* RISC-V assembler for the monitor (included by monitor.c).
*
* The syntax the disassembler prints. 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. Registers are X0-X31 or their
* ABI names (ZERO RA SP GP TP T0-T6 S0-S11 FP A0-A7); memory operands are
* offset(register). CSRs by name (MSTATUS, MEPC, CYCLE...) or number.
* GNU as 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; ; also
* starts a comment). Case doesn't matter: lines are upper-cased outside
* quotes before they are read. Numbers: 123, 0x1F, 0b1010, 'A', '\n'; . is
* the current address. Expressions use + - * / & | ^ << >> ~ and parentheses,
* and %hi(x) and %lo(x), the LUI/AUIPC and ADDI parts of x. Registers are
* x0-x31 or their ABI names (zero ra sp gp tp t0-t6 s0-s11 fp a0-a7); memory
* operands are offset(register). CSRs by name (mstatus, mepc, cycle...) or
* number.
*
* Instructions: RV32I and M, CSRRW..CSRRCI, ECALL EBREAK MRET MNRET WFI FENCE
* FENCE.I, and the usual short forms: NOP LI LA MV NOT NEG SEQZ SNEZ SLTZ SGTZ
* J JR JAL target RET CALL BEQZ BNEZ BLEZ BGEZ BLTZ BGTZ BGT BLE BGTU BLEU CSRR
* CSRW CSRS CSRC CSRWI CSRSI CSRCI.
* F: FLW FSW FMADD.S FMSUB.S FNMSUB.S FNMADD.S FADD.S FSUB.S FMUL.S FDIV.S
* FSQRT.S FSGNJ.S FSGNJN.S FSGNJX.S FMIN.S FMAX.S FEQ.S FLT.S FLE.S FCVT.W.S
* FCVT.WU.S FCVT.S.W FCVT.S.WU FMV.X.W FMV.W.X FCLASS.S and FMV.S FNEG.S
* FABS.S; float registers F0-F31 or FT0-FT11 FS0-FS11 FA0-FA7; a rounding
* mode (RNE RTZ RDN RUP RMM DYN) may follow the operands, DYN if not. LI is one instruction for a constant of
* -2048..2047 known by then, else LUI and ADDI; LA is AUIPC and ADDI.
* Directives: ORG, EQU (NAME EQU value), DB/DEFB/DEFM (numbers and "strings"),
* DH (16 bits), DW/DEFW (32 bits), DS/DEFS n, ALIGN n, END. Instructions must
* be on a 4-byte boundary. Labels are up to 8 characters.
* Instructions: RV32I and M, csrrw..csrrci, ecall ebreak mret mnret wfi fence
* fence.i, and the usual short forms: nop li la mv not neg seqz snez sltz sgtz
* j jr jal target ret call beqz bnez blez bgez bltz bgtz bgt ble bgtu bleu
* csrr csrw csrs csrc csrwi csrsi csrci.
* F: flw fsw fmadd.s fmsub.s fnmsub.s fnmadd.s fadd.s fsub.s fmul.s fdiv.s
* fsqrt.s fsgnj.s fsgnjn.s fsgnjx.s fmin.s fmax.s feq.s flt.s fle.s fcvt.w.s
* fcvt.wu.s fcvt.s.w fcvt.s.wu fmv.x.w fmv.w.x fclass.s and fmv.s fneg.s
* fabs.s; float registers f0-f31 or ft0-ft11 fs0-fs11 fa0-fa7; a rounding
* mode (rne rtz rdn rup rmm dyn) may follow the operands, dyn if not. li is
* one instruction for a constant of -2048..2047 known by then, else lui and
* addi; la is auipc and addi.
* Directives: .org, .equ/.set name, value (or name = value), .byte, .half
* (.short .2byte), .word (.long .4byte), .ascii, .string/.asciz (a 0 after
* each string; \n \r \t \0 \\ \" escapes), .space/.skip/.zero n, .align n
* and .p2align n (to 2^n), .balign n (to n), .end; .text .data .section .globl
* .global .option and .type are accepted and ignored. Instructions must be on
* a 4-byte boundary. Labels are up to 8 characters (more are allowed; the
* first 8 count).
* The older forms still work: ORG, NAME EQU value, DB/DEFB/DEFM (no escapes),
* DH, DW/DEFW, DS/DEFS n, ALIGN n (to n), END; numbers 0FFH, $FF, 1010B; $
* for the current address.
*/
#define MAXSYM 200
struct sym { char name[8]; u32 val; u16 line; };
@@ -42,6 +53,28 @@ 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'); }
/* the length of the character constant at p ('A' or '\N'), 0 if none */
static u8 a_chlen(const char *p)
{
if (p[0] != '\'') return 0;
if (p[1] == '\\' && p[2] && p[3] == '\'') return 4;
if (p[1] && p[2] == '\'') return 3;
return 0;
}
/* the character an escape stands for: \N \R \T \0 \E, else itself */
static u8 a_esc(char c)
{
switch (c) {
case 'n': case 'N': return 10;
case 'r': case 'R': return 13;
case 't': case 'T': return 9;
case '0': return 0;
case 'e': case 'E': return 27;
}
return c;
}
static void a_emit(u8 b)
{
if (a_pass == 2) {
@@ -85,15 +118,19 @@ static void a_name(char *n)
/* ---------------------------------------------------------- expressions */
static u32 a_expr(void);
/* the LUI/AUIPC part and the ADDI part of a 32-bit value */
static u32 hi20(u32 v) { return (v + 0x800) >> 12 & 0xFFFFF; }
static i32 lo12(u32 v) { return sx(v, 12); }
static u32 a_number(void)
{
const char *s = ap;
u32 v = 0;
u8 base = 10, n = 0;
while (a_alnum(s[n])) n++;
if (s[0] == '0' && s[1] == 'X') { base = 16; s += 2; }
else if (s[0] == '0' && s[1] == 'B' && s[n - 1] != 'H') { base = 2; s += 2; }
else {
while (a_alnum(s[n])) n++;
if (s[n - 1] == 'H') base = 16;
else if (s[n - 1] == 'B') {
u8 k;
@@ -104,7 +141,7 @@ static u32 a_number(void)
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 ((c == 'H' && base == 16 && !a_alnum(s[1])) || (c == 'B' && base == 2 && !a_alnum(s[1]))) { s++; break; }
if (d >= base) { a_fail("BAD NUMBER"); break; }
v = v * base + d;
}
@@ -118,6 +155,15 @@ static u32 a_factor(void)
a_sp();
if (*ap == '-') { ap++; return -a_factor(); }
if (*ap == '+') { ap++; return a_factor(); }
if (*ap == '~') { ap++; return ~a_factor(); }
if (*ap == '%') { /* %HI(x), %LO(x) */
u8 hi = !strncmp(ap + 1, "HI(", 3);
if (!hi && strncmp(ap + 1, "LO(", 3)) { a_fail("BAD EXPRESSION"); return 0; }
ap += 3;
v = a_factor();
return hi ? hi20(v) : (u32)lo12(v);
}
if (*ap == '.' && !a_alnum(ap[1])) { ap++; return a_pc; }
if (*ap == '(') {
ap++;
v = a_expr();
@@ -125,7 +171,8 @@ static u32 a_factor(void)
if (*ap == ')') ap++; else a_fail("MISSING )");
return v;
}
if (*ap == '\'' && ap[1] && ap[2] == '\'') { v = (u8)ap[1]; ap += 3; return v; }
if (a_chlen(ap) == 4) { v = a_esc(ap[2]); ap += 4; return v; }
if (a_chlen(ap) == 3) { v = (u8)ap[1]; ap += 3; return v; }
if (*ap == '$') {
ap++;
if ((*ap >= '0' && *ap <= '9') || (*ap >= 'A' && *ap <= 'F')) {
@@ -174,6 +221,9 @@ static u32 a_expr(void)
else 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 if (*ap == '<' && ap[1] == '<') { ap += 2; v <<= a_term(); }
else if (*ap == '>' && ap[1] == '>') { ap += 2; v >>= a_term(); }
else return v;
}
}
@@ -192,7 +242,8 @@ static void a_split(void)
const char *s = ap;
u8 d = 0, q = 0, len;
while (*ap && (d || q || *ap != ',')) {
if (*ap == '\'' && !q && ap[1] && ap[2] == '\'') ap += 2;
if (!q && a_chlen(ap)) ap += a_chlen(ap) - 1;
else if (q && *ap == '\\' && ap[1]) ap++;
else if (*ap == '"') q = !q;
else if (*ap == '(') d++;
else if (*ap == ')' && d) d--;
@@ -315,9 +366,6 @@ static u32 enc_u(u32 base, u8 rd, u32 imm)
if (a_pass == 2 && imm > 0xFFFFF) a_fail("VALUE OUT OF RANGE");
return base | rd << 7 | (imm & 0xFFFFF) << 12;
}
/* the LUI/AUIPC part and the ADDI part of a 32-bit value */
static u32 hi20(u32 v) { return (v + 0x800) >> 12 & 0xFFFFF; }
static i32 lo12(u32 v) { return sx(v, 12); }
/* ----------------------------------------------------------------- F */
static void a_need(u8 n);
@@ -385,7 +433,7 @@ static void a_finstr(const struct fins *t)
u32 b = t->base;
i32 off;
u8 r, s;
if (a_pc & 3) { a_fail("NOT ON A WORD: USE ALIGN 4"); return; }
if (a_pc & 3) { a_fail("NOT ON A WORD: USE .ALIGN 2"); return; }
switch (t->f) {
case G_LD: a_need(2); r = a_freg(0); s = a_mem(1, &off); a_emit32(enc_i(b, r, s, off)); return;
case G_ST: a_need(2); r = a_freg(0); s = a_mem(1, &off); a_emit32(enc_s(b, s, r, off)); return;
@@ -413,7 +461,7 @@ static void a_finstr(const struct fins *t)
enum {
F_R, F_I, F_SH, F_LD, F_ST, F_B, F_U, F_JAL, F_JALR, F_CSR, F_CSRI, F_FIX,
P_LI, P_LA, P_MV, P_NOT, P_NEG, P_SEQZ, P_SNEZ, P_SLTZ, P_SGTZ, P_J, P_JR, P_RET,
P_CALL, P_BZ, P_BZS, P_BSW, P_CSRR, P_CSRW, P_CSRWI
P_CALL, P_BZ, P_BZS, P_BSW, P_CSRR, P_CSRW, P_CSRWI, P_FR, P_FS
};
struct ins { char name[8]; u8 f; u32 base; };
static const struct ins itab[] = {
@@ -450,6 +498,9 @@ static const struct ins itab[] = {
{ "CSRR", P_CSRR, 0 }, { "CSRW", P_CSRW, 0x00001073 }, { "CSRS", P_CSRW, 0x00002073 },
{ "CSRC", P_CSRW, 0x00003073 }, { "CSRWI", P_CSRWI, 0x00005073 },
{ "CSRSI", P_CSRWI, 0x00006073 }, { "CSRCI", P_CSRWI, 0x00007073 },
/* the float CSRs: read (CSRR), and write ([rd,] rs: CSRRW) */
{ "FRFLAGS", P_FR, 0x00102073 }, { "FRRM", P_FR, 0x00202073 }, { "FRCSR", P_FR, 0x00302073 },
{ "FSFLAGS", P_FS, 0x00101073 }, { "FSRM", P_FS, 0x00201073 }, { "FSCSR", P_FS, 0x00301073 },
};
#define NINS (sizeof itab / sizeof itab[0])
@@ -460,7 +511,7 @@ static void a_instr(const struct ins *t)
u32 b = t->base, v;
i32 off;
u8 rd, rs;
if (a_pc & 3) { a_fail("NOT ON A WORD: USE ALIGN 4"); return; }
if (a_pc & 3) { a_fail("NOT ON A WORD: USE .ALIGN 2"); return; }
switch (t->f) {
case F_R: a_need(3); a_emit32(enc_r(b, a_reg(0), a_reg(1), a_reg(2))); return;
case F_I: a_need(3); a_emit32(enc_i(b, a_reg(0), a_reg(1), (i32)a_imm(2))); return;
@@ -496,6 +547,8 @@ static void a_instr(const struct ins *t)
v = a_imm(1);
if (!a_undef && !a_fwd && (i32)v >= -2048 && (i32)v <= 2047) {
a_emit32(enc_i(0x13, rd, 0, (i32)v));
} else if (!a_undef && !a_fwd && !(v & 0xFFF)) {
a_emit32(enc_u(0x37, rd, v >> 12)); /* LUI alone, as GNU as does */
} else {
a_emit32(enc_u(0x37, rd, hi20(v)));
a_emit32(enc_i(0x13, rd, rd, lo12(v)));
@@ -524,6 +577,10 @@ static void a_instr(const struct ins *t)
case P_BSW: a_need(3); rs = a_reg(0); a_emit32(enc_b(b, a_reg(1), rs, a_imm(2))); return;
case P_CSRR: a_need(2); a_emit32(0x2073 | a_reg(0) << 7 | (u32)a_csr(1) << 20); return;
case P_CSRW: a_need(2); a_emit32(b | a_reg(1) << 15 | (u32)a_csr(0) << 20); return;
case P_FR: a_need(1); a_emit32(b | a_reg(0) << 7); return;
case P_FS:
if (a_nops == 1) { a_emit32(b | a_reg(0) << 15); return; }
a_need(2); a_emit32(b | a_reg(0) << 7 | a_reg(1) << 15); return;
case P_CSRWI:
a_need(2);
v = a_imm(1);
@@ -533,15 +590,24 @@ static void a_instr(const struct ins *t)
}
}
/* DB/DH/DW items: numbers and (for DB) strings */
static void a_data(u8 size)
/* data items: numbers, and for bytes "strings". str: 1 strings with
escapes (.ascii), 2 the same with a 0 after each (.string); 0 DB's, kept
as they are. */
static void a_data(u8 size, u8 str)
{
for (;;) {
a_sp();
if (*ap == '"' && size == 1) {
ap++;
while (*ap && *ap != '"') a_emit(*ap++);
while (*ap && *ap != '"') {
if (str && *ap == '\\' && ap[1]) { a_emit(a_esc(ap[1])); ap += 2; }
else a_emit(*ap++);
}
if (*ap) ap++;
if (str == 2) a_emit(0);
} else if (str) {
a_fail("BAD STRING");
return;
} else {
u32 v = a_expr();
u8 k;
@@ -556,20 +622,37 @@ static void a_data(u8 size)
static u8 is_w(const char *w, const char *s) { return !strncmp(w, s, 8); }
/* directives, and the old names that mean the same */
enum { D_EQU = 1, D_SET, D_ORG, D_END, D_DB, D_ASCII, D_STRING, D_DH, D_DW, D_DS, D_SPACE,
D_ALIGN, D_P2ALIGN, D_IGNORE };
static const struct { char n[8]; u8 d; } dtab[] = {
{ "EQU", D_EQU }, { ".EQU", D_SET }, { ".SET", D_SET }, { "ORG", D_ORG }, { ".ORG", D_ORG },
{ "END", D_END }, { ".END", D_END }, { "DB", D_DB }, { "DEFB", D_DB }, { "DEFM", D_DB },
{ ".BYTE", D_DB }, { ".ASCII", D_ASCII }, { ".STRING", D_STRING }, { ".ASCIZ", D_STRING },
{ "DH", D_DH }, { ".HALF", D_DH }, { ".SHORT", D_DH }, { ".2BYTE", D_DH }, { "DW", D_DW },
{ "DEFW", D_DW }, { ".WORD", D_DW }, { ".LONG", D_DW }, { ".4BYTE", D_DW }, { "DS", D_DS },
{ "DEFS", D_DS }, { ".SPACE", D_SPACE }, { ".SKIP", D_SPACE }, { ".ZERO", D_SPACE },
{ "ALIGN", D_ALIGN }, { ".BALIGN", D_ALIGN }, { ".ALIGN", D_P2ALIGN }, { ".P2ALIGN", D_P2ALIGN },
{ ".TEXT", D_IGNORE }, { ".DATA", D_IGNORE }, { ".SECTION", D_IGNORE }, { ".GLOBL", D_IGNORE },
{ ".GLOBAL", D_IGNORE }, { ".OPTION", D_IGNORE }, { ".TYPE", D_IGNORE }, { "", 0 }
};
/* Assemble one source line (uppercase except in quotes). */
static void a_line(char *s)
{
char w[8], lab[8], fm[12];
const char *mn;
u8 has_lab = 0, q = 0;
u8 has_lab = 0, q = 0, d;
u16 m;
u32 n, f;
char *c;
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; }
if (q && *c == '\\' && c[1]) c++;
else if (*c == '"') q = !q;
else if (!q && a_chlen(c)) c += a_chlen(c) - 1;
else if ((*c == ';' || *c == '#') && !q) { *c = 0; break; }
}
ap = s;
a_sp();
@@ -593,30 +676,58 @@ static void a_line(char *s)
mn = ap;
a_name(w);
a_sp();
} else if (!strncmp(ap, "EQU", 3) && !a_alnum(ap[3])) {
memcpy(lab, w, 8); has_lab = 1;
} else if ((!strncmp(ap, "EQU", 3) && !a_alnum(ap[3])) || (*ap == '=' && w[0] != '.')) {
memcpy(lab, w, 8); has_lab = 1; /* NAME EQU value, NAME = value */
ap += *ap == '=' ? 1 : 3;
memcpy(w, "EQU\0\0\0\0\0", 8);
ap += 3;
}
if (is_w(w, "EQU")) {
u32 v = a_expr();
for (m = 0; dtab[m].d && !is_w(w, dtab[m].n); m++);
d = dtab[m].d;
if (d == D_SET) { /* .EQU NAME, value */
if (!a_alpha(*ap)) { a_fail("EQU NEEDS A NAME"); return; }
a_name(lab); has_lab = 1;
a_sp();
if (*ap == ',') ap++;
d = D_EQU;
}
if (d == D_EQU) {
n = 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);
else sym_def(lab, n);
return;
}
if (has_lab) sym_def(lab, a_pc);
if (is_w(w, "ORG")) { a_pc = a_expr(); return; }
if (is_w(w, "END")) { a_end = 1; return; }
if (is_w(w, "DB") || is_w(w, "DEFB") || is_w(w, "DEFM")) { a_data(1); return; }
if (is_w(w, "DH")) { a_data(2); return; }
if (is_w(w, "DW") || is_w(w, "DEFW")) { a_data(4); return; }
if (is_w(w, "DS") || is_w(w, "DEFS")) { a_pc += a_expr(); return; }
if (is_w(w, "ALIGN")) {
u32 n = a_expr();
if (!n || (n & (n - 1))) a_fail("ALIGN TO A POWER OF 2");
else while (a_pc & (n - 1)) a_emit(0);
switch (d) {
case D_ORG: a_pc = a_expr(); return;
case D_END: a_end = 1; return;
case D_DB: a_data(1, 0); return;
case D_ASCII: a_data(1, 1); return;
case D_STRING: a_data(1, 2); return;
case D_DH: a_data(2, 0); return;
case D_DW: a_data(4, 0); return;
case D_DS: a_pc += a_expr(); return;
case D_SPACE: /* n [, fill] */
n = a_expr(); f = 0;
a_sp();
if (*ap == ',') { ap++; f = a_expr(); }
while (n--) a_emit(f);
return;
case D_ALIGN: case D_P2ALIGN:
n = a_expr();
if (d == D_P2ALIGN) n = n < 32 ? 1u << n : 0; /* .ALIGN, .P2ALIGN: to 2^n */
a_sp();
if (!n || (n & (n - 1))) a_fail("ALIGN TO A POWER OF 2");
else if (*ap == ',') { /* n, fill */
ap++;
f = a_expr();
while (a_pc & (n - 1)) a_emit(f);
} else { /* zeros to a word, then NOPs */
while (a_pc & (n - 1) & 3) a_emit(0);
while (a_pc & (n - 1)) a_emit32(0x13);
}
return;
case D_IGNORE: return;
}
/* F instructions, whose names can be longer than 8 */
for (m = 0; m < 11 && a_alnum(mn[m]); m++) fm[m] = mn[m];
@@ -641,8 +752,9 @@ 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;
if (q && *s == '\\' && s[1]) s++;
else if (*s == '"') q = !q;
else if (!q && a_chlen(s)) s += a_chlen(s) - 1;
else if (!q && *s >= 'a' && *s <= 'z') *s -= 32;
if (*s == '\t') *s = ' ';
}