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:
1 parent
25c8286cf2
commit
fa4752fd7a
16 files changed
+7921
-7615
No files matched your search
+105
-67
@@ -550,6 +550,20 @@ ACOS(-1) = 180 20! = 2.4329E+18
|
||||
| `LEFT$(A$, N)` | First *N* characters |
|
||||
| `RIGHT$(A$, N)` | Last *N* characters |
|
||||
| `MID$(A$, S [, N])` | *N* characters (or the rest) starting at position *S* (from 1) |
|
||||
| `UCASE$(A$)` | The string in capitals: only `a`–`z` change |
|
||||
| `LCASE$(A$)` | The string in small letters: only `A`–`Z` change |
|
||||
|
||||
BASIC turns what you type into capitals outside quotes, but text that comes
|
||||
from `INPUT`, a file or `CHR$` keeps its case. `UCASE$` is the way to compare
|
||||
an answer whatever case it was typed in, `IF UCASE$(A$) = "YES" THEN …`, and
|
||||
the two together tidy a name:
|
||||
|
||||
```
|
||||
10 INPUT "YOUR NAME"; N$
|
||||
20 PRINT "HELLO, "; UCASE$(LEFT$(N$, 1)); LCASE$(MID$(N$, 2))
|
||||
```
|
||||
|
||||
typed `aDA LOVELACE`, prints `HELLO, Ada lovelace`.
|
||||
|
||||
### Machine and screen
|
||||
|
||||
@@ -992,9 +1006,9 @@ graphics come back exactly as they were.
|
||||
T1 00000000 T2 00000000 S0 00013FB0 S1 0000A01E A0 00000000 A1 00012000
|
||||
...
|
||||
ADDR WORD INSTRUCTION STACK
|
||||
>*12000 04100513 LI A0,65 13F70 00000000
|
||||
12004 F0A000A3 SB A0,-255(ZERO) 13F74 0000A01E
|
||||
12008 00008067 RET ...
|
||||
>*12000 04100513 li a0,65 13F70 00000000
|
||||
12004 F0A000A3 sb a0,-255(zero) 13F74 0000A01E
|
||||
12008 00008067 ret ...
|
||||
MEMORY
|
||||
12000 13 05 10 04 A3 00 A0 F0-67 80 00 00 00 00 00 00 ....g...........
|
||||
```
|
||||
@@ -1007,7 +1021,8 @@ graphics come back exactly as they were.
|
||||
|
||||
### Commands
|
||||
|
||||
Type a command on the bottom line and press Enter. Numbers are in hex.
|
||||
Type a command on the bottom line and press Enter. Numbers are in hex, with
|
||||
or without `0x`.
|
||||
|
||||
| Command | Does |
|
||||
|---|---|
|
||||
@@ -1024,7 +1039,7 @@ Type a command on the bottom line and press Enter. Numbers are in hex.
|
||||
| `F` | Show the float registers F0–F31 and FCSR, as bit patterns, in the memory window (`M` brings the memory back) |
|
||||
| `SAVE "NAME" start end` | Save memory *start* to *end* (inclusive) to a file in flash |
|
||||
| `LOAD "NAME"` | Load a memory file back to where it was saved from |
|
||||
| `LOAD "NAME" addr` | Load it at *addr* instead (0A000h or above) |
|
||||
| `LOAD "NAME" addr` | Load it at *addr* instead (A000 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 |
|
||||
@@ -1043,9 +1058,11 @@ Type a command on the bottom line and press Enter. Numbers are in hex.
|
||||
- **The float registers** are saved and put back like the others, while the
|
||||
FPU is on (`mstatus.FS`, which BASIC turns on); `F` says when it is off.
|
||||
- **The disassembler** shows every RV32IMF instruction and the CSR and system
|
||||
instructions, using the usual short forms where they apply (`LI`, `MV`, `J`,
|
||||
`RET`, `BEQZ`, `CSRR` and so on), with hex numbers like `1F0H` and `0A200H`;
|
||||
anything else is shown as `DW` and its value.
|
||||
instructions the way GNU `objdump` does: in lower case, with the usual
|
||||
short forms where they apply (`li`, `mv`, `j`, `ret`, `beqz`, `csrr` and so
|
||||
on), hex numbers like `0x1f0`, and the float registers by their ABI names
|
||||
(`fa0`, `ft1`). Anything else is shown as `.word` and its value. What it
|
||||
shows, the assembler reads back.
|
||||
|
||||
### A machine-code example
|
||||
|
||||
@@ -1056,8 +1073,8 @@ RUN
|
||||
MONITOR
|
||||
```
|
||||
|
||||
The `POKE`s put `LI A0,65 : SB A0,-255(ZERO) : RET` at 12000h; it prints an
|
||||
`A` (port 1 is at -255 from address 0). In the monitor, `B 12000` then `X`.
|
||||
The `POKE`s put `li a0,65`, `sb a0,-255(zero)` and `ret` at 0x12000; it
|
||||
prints an `A` (port 1 is at -255 from address 0). In the monitor, `B 12000` then `X`.
|
||||
Back in BASIC, `CALL 73728` stops at the breakpoint. Press Enter to step
|
||||
through each instruction, or `X` to let it finish.
|
||||
|
||||
@@ -1066,89 +1083,110 @@ through each instruction, or `X` to let it finish.
|
||||
The monitor can assemble RISC-V 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.
|
||||
**Writing source.** The assembler reads the GNU assembler's RISC-V syntax,
|
||||
as in the RISC-V manuals and `gcc -S` output. Type the source 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 12000H
|
||||
30 START: LA A1,MSG
|
||||
40 LOOP: LBU A0,0(A1)
|
||||
50 BEQZ A0,DONE
|
||||
60 WAIT: LBU T0,-256(ZERO) ; CAN THE SCREEN TAKE A CHARACTER?
|
||||
70 ANDI T0,T0,2
|
||||
80 BEQZ T0,WAIT
|
||||
90 SB A0,-255(ZERO)
|
||||
100 ADDI A1,A1,1
|
||||
110 J LOOP
|
||||
120 DONE: RET
|
||||
130 MSG: DB "HELLO FROM ASSEMBLY",13,10,0
|
||||
10 # print a message
|
||||
20 .org 0x12000
|
||||
30 start: la a1, msg
|
||||
40 loop: lbu a0, 0(a1)
|
||||
50 beqz a0, done
|
||||
60 wait: lbu t0, -256(zero) # can the screen take a character?
|
||||
70 andi t0, t0, 2
|
||||
80 beqz t0, wait
|
||||
90 sb a0, -255(zero)
|
||||
100 addi a1, a1, 1
|
||||
110 j loop
|
||||
120 done: ret
|
||||
130 msg: .string "Hello from assembly\r\n"
|
||||
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.
|
||||
|
||||
BASIC stores what you type in capitals, except inside double quotes, so
|
||||
`LIST` shows `30 START: LA A1, MSG`. The assembler doesn't mind either way,
|
||||
but a character constant changes: `'a'` becomes `'A'`. BASIC also drops the
|
||||
spaces after the line number, so a label needs its colon. And BASIC knows
|
||||
nothing of `\"`, so a string can't contain a double quote; use `.byte 34`.
|
||||
|
||||
**Assembling.** `MONITOR`, then `ASM "HELLO"`. The monitor says
|
||||
`ASSEMBLED 12000-12041` and shows the code; errors are listed in the memory
|
||||
window with their line numbers. Back in BASIC (`X`), `CALL 73728` runs it.
|
||||
Or set a breakpoint first and step through it.
|
||||
|
||||
**One line at a time.** `A 12100` starts the line assembler at 12100h: type
|
||||
**One line at a time.** `A 12100` starts the line assembler at 0x12100: 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**: as the disassembler shows it.
|
||||
**Syntax**: GNU as's, which is also what the disassembler shows.
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 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: `LI A0,END-START`, `LI A1,'Z'-1` |
|
||||
| Registers | `X0`–`X31` or `ZERO RA SP GP TP T0`–`T6 S0`–`S11 FP A0`–`A7` |
|
||||
| Memory operands | *offset*`(`*register*`)`: `LW A0,8(SP)`, `SB A0,-255(ZERO)`, `LBU T0,(A1)` |
|
||||
| CSRs | by name (`MSTATUS`, `MEPC`, `MCAUSE`, `CYCLE`, `TIME`, `INSTRET`, …) or number |
|
||||
| `ORG addr` | assemble from *addr* (12000h if there is no `ORG`) |
|
||||
| `DB` / `DEFB` / `DEFM` | bytes and `"strings"`: `DB "HI",13,10,0` |
|
||||
| `DH` | 16-bit halfwords, low byte first |
|
||||
| `DW` / `DEFW` | 32-bit words, low byte first |
|
||||
| `DS n` / `DEFS n` | skip *n* bytes |
|
||||
| `ALIGN n` | skip to a multiple of *n* (instructions must be on a multiple of 4) |
|
||||
| `END` | stop here |
|
||||
| `;` | comment to the end of the line |
|
||||
| Labels | `name:` before an instruction, or on a line of its own; letters, digits, `_` and `.`, of which the first 8 count |
|
||||
| Constants | `.equ name, value`, `.set name, value` or `name = value` |
|
||||
| Numbers | `123`, `0x1f`, `0b1010`, `'A'`, `'\n'`; `.` is the current address |
|
||||
| Expressions | <code>+ - * / & | ^ << >> ~</code> and brackets: `li a0, end - start`, `li a1, 'Z' - 1`; `%hi(x)` and `%lo(x)` split an address for `lui` and `addi`, or a load or store: `lui a5, %hi(table)` then `lw a0, %lo(table)(a5)` |
|
||||
| Registers | `x0`–`x31` or `zero ra sp gp tp t0`–`t6 s0`–`s11 fp a0`–`a7` |
|
||||
| Memory operands | *offset*`(`*register*`)`: `lw a0, 8(sp)`, `sb a0, -255(zero)`, `lbu t0, (a1)` |
|
||||
| CSRs | by name (`mstatus`, `mepc`, `mcause`, `cycle`, `time`, `instret`, …) or number |
|
||||
| `.org addr` | assemble from *addr* (0x12000 if there is no `.org`) |
|
||||
| `.byte` | bytes: `.byte 13, 10, 'A'` |
|
||||
| `.half` / `.short` | 16-bit halfwords, low byte first |
|
||||
| `.word` / `.long` | 32-bit words, low byte first: `.word start, 0x12345678` |
|
||||
| `.ascii "text"` | the characters of the text; `\n`, `\r`, `\t`, `\0`, `\\` and `\"` stand for a line feed, return, tab, 0, `\` and `"` |
|
||||
| `.string "text"` / `.asciz "text"` | the same with a 0 after it |
|
||||
| `.space n` / `.skip n` / `.zero n` | *n* bytes of 0, or of *fill* with `.space n, fill` |
|
||||
| `.align n` / `.p2align n` | to a multiple of 2*ⁿ*: `.align 2` for a word. The gap is filled with zeros up to a word, then `nop`s, so code can run through it; `.align n, fill` fills it with *fill* |
|
||||
| `.balign n` | to a multiple of *n* bytes |
|
||||
| `.end` | stop here |
|
||||
| `.text`, `.data`, `.section`, `.globl`, `.global`, `.option`, `.type` | accepted and ignored, so code written for GNU as needn't be changed |
|
||||
| `#` or `;` | comment to the end of the line |
|
||||
|
||||
- **Instructions:** all of RV32I and M, `CSRRW`…`CSRRCI`, `ECALL`, `EBREAK`,
|
||||
`MRET`, `MNRET`, `WFI`, `FENCE`, `FENCE.I`, and the short forms `NOP`, `LI`,
|
||||
`LA`, `MV`, `NOT`, `NEG`, `SEQZ`, `SNEZ`, `SLTZ`, `SGTZ`, `J`, `JR`, `JAL`
|
||||
*target* (saving RA), `JALR` *register*, `RET`, `CALL`, `BEQZ`, `BNEZ`,
|
||||
`BLEZ`, `BGEZ`, `BLTZ`, `BGTZ`, `BGT`, `BLE`, `BGTU`, `BLEU`, `CSRR`, `CSRW`,
|
||||
`CSRS`, `CSRC`, `CSRWI`, `CSRSI`, `CSRCI`.
|
||||
- **Floating point:** `FLW`, `FSW`, `FADD.S`, `FSUB.S`, `FMUL.S`, `FDIV.S`,
|
||||
`FSQRT.S`, `FMADD.S`, `FMSUB.S`, `FNMSUB.S`, `FNMADD.S`, `FMIN.S`, `FMAX.S`,
|
||||
`FSGNJ.S`, `FSGNJN.S`, `FSGNJX.S` (and `FMV.S`, `FNEG.S`, `FABS.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`. Float registers are `F0`–`F31` or `FT0`–`FT11`,
|
||||
`FS0`–`FS11`, `FA0`–`FA7`; a rounding mode (`RNE`, `RTZ`, `RDN`, `RUP`, `RMM`)
|
||||
may follow the operands, `FADD.S FA0,FA1,FA2,RTZ`, and without one the
|
||||
instruction uses FCSR's (round to nearest unless changed). CSRs `FFLAGS`,
|
||||
`FRM` and `FCSR`.
|
||||
- **`LI`** is one instruction (`ADDI`) for a value from −2048 to 2047 that is
|
||||
known by then, otherwise two (`LUI`, `ADDI`); **`LA`** is always two
|
||||
(`AUIPC`, `ADDI`); **`CALL`** is `JAL RA`, which reaches 1 MB either way.
|
||||
- **Where code can go:** RAM from 0A000h up. 12000h–12FFFh is the safe place,
|
||||
since BASIC doesn't use it.
|
||||
The assembler of earlier versions used other spellings, and sources written
|
||||
for it still assemble: `ORG`, `NAME EQU value`, `DB`/`DEFB`/`DEFM` (with
|
||||
strings taken as they are, no escapes), `DH`, `DW`/`DEFW`, `DS n`/`DEFS n`
|
||||
(skip *n* bytes), `ALIGN n` (to a multiple of *n*), `END`, numbers like
|
||||
`0FFH`, `$FF` and `1010B`, and `$` for the current address.
|
||||
|
||||
- **Instructions:** all of RV32I and M, `csrrw`…`csrrci`, `ecall`, `ebreak`,
|
||||
`mret`, `mnret`, `wfi`, `fence`, `fence.i`, and the short forms `nop`, `li`,
|
||||
`la`, `mv`, `not`, `neg`, `seqz`, `snez`, `sltz`, `sgtz`, `j`, `jr`, `jal`
|
||||
*target* (saving `ra`), `jalr` *register*, `ret`, `call`, `beqz`, `bnez`,
|
||||
`blez`, `bgez`, `bltz`, `bgtz`, `bgt`, `ble`, `bgtu`, `bleu`, `csrr`,
|
||||
`csrw`, `csrs`, `csrc`, `csrwi`, `csrsi`, `csrci`.
|
||||
- **Floating point:** `flw`, `fsw`, `fadd.s`, `fsub.s`, `fmul.s`, `fdiv.s`,
|
||||
`fsqrt.s`, `fmadd.s`, `fmsub.s`, `fnmsub.s`, `fnmadd.s`, `fmin.s`, `fmax.s`,
|
||||
`fsgnj.s`, `fsgnjn.s`, `fsgnjx.s` (and `fmv.s`, `fneg.s`, `fabs.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`. Float registers are `f0`–`f31` or
|
||||
`ft0`–`ft11`, `fs0`–`fs11`, `fa0`–`fa7`; a rounding mode (`rne`, `rtz`,
|
||||
`rdn`, `rup`, `rmm`) may follow the operands, `fadd.s fa0, fa1, fa2, rtz`,
|
||||
and without one the instruction uses FCSR's (round to nearest unless
|
||||
changed). CSRs `fflags`, `frm` and `fcsr`, and the short forms `frcsr`,
|
||||
`fscsr`, `frrm`, `fsrm`, `frflags`, `fsflags`.
|
||||
- **`li`** is one instruction (`addi`) for a value from −2048 to 2047 that is
|
||||
known by then, `lui` alone for one whose low 12 bits are 0, otherwise two
|
||||
(`lui`, `addi`); **`la`** is always two (`auipc`, `addi`). **`call`** is
|
||||
`jal ra`, which reaches 1 MB either way (GNU as makes it `auipc` and
|
||||
`jalr`, 8 bytes, to reach anywhere).
|
||||
- **Where code can go:** RAM from 0xA000 up. 0x12000–0x12FFF is the safe
|
||||
place, since BASIC doesn't use it.
|
||||
- **Labels:** up to 200.
|
||||
- **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 REGISTER`, `MISSING OPERAND`, `TOO
|
||||
MANY OPERANDS`, `OFFSET(REGISTER)`, `UNDEFINED LABEL`, `DUPLICATE LABEL`,
|
||||
`VALUE OUT OF RANGE` (an immediate, offset or `LUI` value that doesn't fit),
|
||||
`VALUE OUT OF RANGE` (an immediate, offset or `lui` value that doesn't fit),
|
||||
`BRANCH TOO FAR` (beyond ±4 KB), `JUMP TOO FAR` (beyond ±1 MB), `NOT ON A
|
||||
WORD: USE ALIGN 4`, `CAN'T WRITE THERE`, `BAD NUMBER`, `BAD EXPRESSION`.
|
||||
WORD: USE .ALIGN 2`, `CAN'T WRITE THERE`, `BAD NUMBER`, `BAD EXPRESSION`,
|
||||
`BAD STRING` (`.ascii` or `.string` without a string), `ALIGN TO A POWER OF
|
||||
2`, `EQU NEEDS A NAME`.
|
||||
|
||||
### How it works
|
||||
|
||||
|
||||
Reference in new issue
Block a user