Files
SplitBit-Emulator/Programs/CosmOS/Assembler/tokenTest.asm
T
AnachronautandClaude Opus 5 fb335681d2 M4: SplitBit assembles SplitBit, and then assembles itself
> load Asm.sbx
    > run cosmos.asm
    wrote cosmos.bin: program 7036, data 2448, labels 475
    > run Asm.asm
    wrote Asm.sbx: program 7533, data 4099, labels 555

Both byte for byte identical to what the host assembler builds from the
same source. The machine now builds the operating system it is running on,
and builds the thing that built it.

THE CHECK THAT MATTERS MOST IS THE THIRD ONE. A binary that matches could
still have come from an assembler wrong in some way this particular source
happens not to exercise. So Tests/native.sh boots the CosmOS that CosmOS
built and has THAT assemble CosmOS again - and the second generation is
identical to the first, down to the cycle count. It is a fixed point: the
machinery has been through itself. After this the host is a convenience
rather than a necessity.

WHAT STOOD IN THE WAY was not the assembler. It loaded, faulted at 7,780
cycles, and the fault was in CosmOS: a loaded program is staged at 0x8000
before being blitted into place, so the whole FILE has to fit in the 32,768
bytes above it. The assembler's file was 33,983, and 22K of that was
zeroed scratch buffers - because #Reserve emits what it reserves.

None of that is initialised data. It is scratch, wanted only while the
assembler runs, and while it runs everything above its own data is free.
So the buffers are a MAP now rather than declarations - Assembler/scratch.asm
writes down six addresses and the file carries none of it. 33,983 bytes
became 11,648, and the assembler could load itself.

The map has a file of its own because the reader and the label table both
need addresses out of it while neither includes the other.

The sizes are cut to the largest thing it is asked to build, and that turns
out not to be the operating system: the assembler is 555 labels and 11,648
bytes of output against CosmOS's 475 and 9,564. The hardest thing this
assembles is itself.

Also: sizing it for CosmOS meant raising the label table, and raising the
label table is what pushed the file over the staging limit. The two facts
only met because the first one was tried.

Speed, measured rather than guessed: CosmOS takes 80,168,646 cycles, which
is eighty seconds of emulated time and under a second under --fast. Most of
it is a straight walk of 475 label names, several thousand times. Sorting
or bucketing that is easy and was deliberately not written before there was
something to measure.

make run-cosmos now puts every source file on the disk, so the whole thing
can be done rather than read about.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
2026-08-21 12:03:31 -04:00

178 lines
3.1 KiB
NASM

; The assembler's front end on its own, one line per token.
;
; Prints the line each token started on, what the token turned out to be, how many bytes
; it will come to, and the token itself between brackets so that whitespace at either end
; would show if any ever leaked in.
;
; WHAT A TOKEN IS is the part worth checking here rather than at the far end. A wrong
; classification does not produce a wrong byte in an obvious place - it produces a right
; looking program of the wrong length, with everything after it shifted, and by then the
; only symptom is that a label points at the middle of an instruction.
;
; Written by Anachronaut
#Include services.asm
#Program
#Base 0x2000
start:
SETD.0 Wanted
INIB 0d23
SWI osArgument
SETD.0 Wanted
LDA.0
BRA nothingAsked
SETD.0 Wanted
CALL srcOpen
BNQ noFile
tokenLoop:
CALL tokNext
BNQ tokensDone
SETD.0 TokLine
LDA.0
INCD.0
LDB.0
SWI osPrintNumber
CALL clsToken
BNQ badToken
; Which of the six it turned out to be. The names are four characters and a space, so
; the columns line up without any counting.
SETD.0 TypeNames
SETD.2 ClsType
LDA.2
CALL nameOfType
SETD.1 NamePointer
LDD.0.1
SWI osPrintString
SETD.0 ClsLength
LDA.0
INCD.0
LDB.0
SWI osPrintNumber ; Sixteen bits: a string of 255 characters is 256 bytes long.
SETD.0 OpenMark
SWI osPrintString
SETD.0 TokText
SWI osPrintString
SETD.0 CloseMark
SWI osPrintString
BRI tokenLoop
badToken:
SETD.0 StoppedText
SWI osPrintString
SWI osExit
; The name of type A, out of a table of fixed width entries so that no pointer arithmetic
; is needed beyond a multiply by the width.
nameOfType:
SETD.0 TypeWidth
LDB.0
RSTA
SETD.0 NameLeft
STA.0
SETD.2 ClsType
LDA.2
SETD.0 NameOffset
CALL numZero
nameLoop:
SETD.2 ClsType
LDA.2
SETD.0 NameLeft
LDB.0
CCF
SUB
BRQ nameFound
SETD.0 TypeWidth
LDA.0
SETD.0 NameOffset
CALL numAddByte
SETD.0 NameLeft
LDA.0
INCA
STA.0
BRI nameLoop
nameFound:
; The answer is left in NamePointer rather than in DP0, because a RET puts Data Pointers
; 0 to 2 back as they were: a routine cannot hand back a pointer, only write one down.
SETD.0 TypeNames
SETD.1 NamePointer
STD.0.1
SETD.0 NamePointer
SETD.2 NameOffset
CALL numAdd
RET
tokensDone:
SETD.0 DoneText
SWI osPrintString
SWI osExit
nothingAsked:
SETD.0 AskText
SWI osPrintString
SWI osExit
noFile:
SETD.0 NoFileText
SWI osPrintString
SWI osExit
#Data
#Base 0x1000
Wanted:
#Reserve 0d23
OpenMark:
" ["
CloseMark:
"]
"
; Six names of eleven characters each, counting the zero the assembler puts on the end of
; every string. Written one to a line so that adding a type is adding a line.
TypeNames:
" keyword "
" instr "
" value "
" string "
" label: "
" label "
TypeWidth:
0d11
NameLeft:
0x00
NameOffset:
0x00 0x00
NamePointer:
0x00 0x00
StoppedText:
"---- stopped: the assembler does not understand that
"
DoneText:
"---- no more tokens
"
AskText:
"say which file
"
NoFileText:
"no such file
"
#Include scratch.asm
#Include numbers.asm
#Include source.asm
#Include token.asm
#Include classify.asm
#Include table.asm