Programs/CosmOS/Assembler/ is an assembler written in SplitBit assembly. It
runs under CosmOS, reads source off a SplitBit disk, and writes a binary back
to it with no host involved anywhere:
> run Asm.sbx hello.asm
wrote hello.bin: program 17, data 14, labels 2
THE ACCEPTANCE TEST IS THE BYTES. Tests/native.sh assembles Programs/hello.asm
both ways and compares the two files byte for byte, then runs the one the
machine built. "It ran" and "the sizes look right" both pass for a binary with
a label one byte out, which is a program that jumps into the middle of an
instruction - so the only honest test is the one SplitDisk and sbfs.asm
already work under: two implementations of one written specification, each
checking the other. The files are identical and the result prints Hello,
World! in 70 cycles.
hello.asm is the target because it is the oldest program in the repository.
The first thing this machine ever ran is now the first thing it assembles for
itself.
TWO PASSES OVER STREAMED SOURCE. The C assembler reads every token of every
file into one array; that cannot port, because cosmos.asm alone is 56,047
bytes against 64K of Data Memory. The native one streams through a 256 byte
window, twice, and keeps only the label table between the passes. Two passes
suffice because every length is known without resolving anything - an
instruction's from its shape, a value's is one, a string's is its characters
and a zero - so the first pass fixes every address and the second never needs
a fixup list. A forward reference stops being a special case and becomes the
reason there are two passes at all.
The parts, each checked before anything was built on it:
source.asm characters out of a file of any size, with a line number
token.asm tokens out of characters, one character of lookahead
classify.asm what a token is, in the C assembler's order, which IS the
language: keyword, instruction, value, string, label
labels.asm names packed in an arena, four bytes of index each
numbers.asm sixteen bit arithmetic, since sbfs.asm's cannot be reached
table.asm the instruction set, generated by the same script the
monitor's copy is, and now BOTH are checked by docs.sh
readTest.asm and tokenTest.asm check the reader and the tokenizer on their
own, recorded as cosmosSource and cosmosTokens. A wrong classification does
not produce a wrong byte somewhere obvious; it produces a right looking
program of the wrong length, so it is worth catching where it happens.
WHAT IT REFUSES: #Include, #Base, #Align, #Reserve and #Vectors are refused
by name rather than ignored. Skipping a directive would produce a file that
looked right and was the wrong length, which is the worst thing an assembler
can do.
Two traps worth recording, both already known to this project and both hit
again: CALL restores A, B and DP0-DP2, so three routines returning an answer
in A had it undone by their own return; and numStep works on DP0, so three
sites that set DP1 left a pointer that never advanced.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
102 lines
2.1 KiB
NASM
102 lines
2.1 KiB
NASM
; Sixteen bit arithmetic, for an assembler that counts in addresses.
|
|
;
|
|
; sbfs.asm has routines like these and the assembler cannot use them: it does not include
|
|
; the filesystem, because it reaches the disk through the system's services instead. That
|
|
; is the no-linker tax, paid in about a hundred and fifty bytes, and it is cheaper than the
|
|
; two and a half kilobytes including sbfs.asm would cost.
|
|
;
|
|
; Everything here works on numbers in memory rather than in registers, because a CALL puts
|
|
; A, B and Data Pointers 0 to 2 back as it found them. Only memory survives a return.
|
|
;
|
|
; Numbers are stored most significant byte first, the way every number on this machine is.
|
|
;
|
|
; Written by Anachronaut
|
|
|
|
#Program
|
|
|
|
; The two byte number at DP0 becomes the one at DP2. Destination first, so a call reads
|
|
; the way an assignment does.
|
|
numSet:
|
|
LDA.2
|
|
STA.0
|
|
INCD.2
|
|
INCD.0
|
|
LDA.2
|
|
STA.0
|
|
RET
|
|
|
|
; The two byte number at DP0 becomes itself plus the one at DP2.
|
|
numAdd:
|
|
DPUP.0 0d01
|
|
DPUP.2 0d01
|
|
LDA.0
|
|
LDB.2
|
|
CCF
|
|
ADD
|
|
MVQA
|
|
STA.0
|
|
DPDN.0 0d01
|
|
DPDN.2 0d01
|
|
LDA.0
|
|
LDB.2
|
|
ADD ; Carries in from the low half. Nothing between touches it.
|
|
MVQA
|
|
STA.0
|
|
RET
|
|
|
|
; Adds the byte in A to the two byte number at DP0.
|
|
numAddByte:
|
|
DPUP.0 0d01
|
|
LDB.0
|
|
CCF
|
|
ADD
|
|
MVQA
|
|
STA.0
|
|
BNC numAddByteDone
|
|
DPDN.0 0d01
|
|
LDA.0
|
|
INCA
|
|
STA.0
|
|
numAddByteDone:
|
|
RET
|
|
|
|
; Adds one to the two byte number at DP0.
|
|
numStep:
|
|
DPUP.0 0d01
|
|
LDA.0
|
|
INCA
|
|
STA.0
|
|
BNC numStepDone ; It did not wrap, so the high byte is untouched.
|
|
DPDN.0 0d01
|
|
LDA.0
|
|
INCA
|
|
STA.0
|
|
numStepDone:
|
|
RET
|
|
|
|
; Compares the two byte number at DP0 with the one at DP2. Q is zero if they are equal,
|
|
; and the Carry Flag is set if the one at DP0 is the smaller. Both come back, because
|
|
; neither Q nor the Status register is put back by a return.
|
|
numCompare:
|
|
LDA.0
|
|
LDB.2
|
|
CCF
|
|
SUB ; The high bytes settle it unless they are the same.
|
|
BNQ numCompareDone
|
|
INCD.0
|
|
INCD.2
|
|
LDA.0
|
|
LDB.2
|
|
CCF
|
|
SUB
|
|
numCompareDone:
|
|
RET
|
|
|
|
; The two byte number at DP0 becomes zero.
|
|
numZero:
|
|
RSTA
|
|
STA.0
|
|
INCD.0
|
|
STA.0
|
|
RET
|