From 4c076398089a62faab7999fd918d3d3782929b82 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 30 Nov 2019 21:36:34 -0500 Subject: [PATCH] basic: make cmd table more compact This shaves off quite a few bytes from the binary. --- apps/basic/blk.asm | 10 ++++---- apps/basic/fs.asm | 12 ++++----- apps/basic/main.asm | 60 +++++++++++++++++++++++---------------------- apps/lib/util.asm | 13 ++++++++++ 4 files changed, 55 insertions(+), 40 deletions(-) diff --git a/apps/basic/blk.asm b/apps/basic/blk.asm index 3df4383..c156bd1 100644 --- a/apps/basic/blk.asm +++ b/apps/basic/blk.asm @@ -38,12 +38,12 @@ basPUTB: jp blkPutB basBLKCmds: + .db "bsel", 0 .dw basBSEL - .db "bsel", 0, 0 - .dw basBSEEK .db "bseek", 0 + .dw basBSEEK + .db "getb", 0 .dw basGETB - .db "getb", 0, 0 + .db "putb", 0 .dw basPUTB - .db "putb", 0, 0 - .db 0xff, 0xff, 0xff ; end of table + .db 0xff ; end of table diff --git a/apps/basic/fs.asm b/apps/basic/fs.asm index 1f9502c..008a490 100644 --- a/apps/basic/fs.asm +++ b/apps/basic/fs.asm @@ -127,14 +127,14 @@ basPgmHook: ret basFSCmds: + .db "fls", 0 .dw basFLS - .db "fls", 0, 0, 0 - .dw basLDBAS .db "ldbas", 0 - .dw basFOPEN + .dw basLDBAS .db "fopen", 0 + .dw basFOPEN + .db "fnew", 0 .dw basFNEW - .db "fnew", 0, 0 + .db "fdel", 0 .dw basFDEL - .db "fdel", 0, 0 - .db 0xff, 0xff, 0xff ; end of table + .db 0xff ; end of table diff --git a/apps/basic/main.asm b/apps/basic/main.asm index 15c4958..81ba5a3 100644 --- a/apps/basic/main.asm +++ b/apps/basic/main.asm @@ -63,19 +63,18 @@ basLoop: ; Destroys HL. ; Z is set if found, unset otherwise. basFindCmd: - ; cmd table starts with routine pointer, skip - inc hl \ inc hl .loop: call strcmp - jr z, .found - ld a, 8 - call addHL + call strskip + inc hl ; point to routine + jr z, .found ; Z from strcmp + inc hl \ inc hl ; skip routine ld a, (hl) - cp 0xff - jr nz, .loop - jp unsetZ + inc a ; was it 0xff? + jr nz, .loop ; no + dec a ; unset Z + ret .found: - dec hl \ dec hl call intoHL push hl \ pop ix ret @@ -436,46 +435,49 @@ basR2Var: ; Just send reg to vars. Used in basPgmHook cp a ; USR never errors out ret +; Command table format: Null-terminated string followed by a 2-byte routine +; pointer. + ; direct only basCmds1: + .db "bye", 0 .dw basBYE - .db "bye", 0, 0, 0 + .db "list", 0 .dw basLIST - .db "list", 0, 0 + .db "run", 0 .dw basRUN - .db "run", 0, 0, 0 - .dw bufInit .db "clear", 0 + .dw bufInit ; statements basCmds2: - .dw basPRINT .db "print", 0 + .dw basPRINT + .db "goto", 0 .dw basGOTO - .db "goto", 0, 0 + .db "if", 0 .dw basIF - .db "if", 0, 0, 0, 0 - .dw basINPUT .db "input", 0 + .dw basINPUT + .db "peek", 0 .dw basPEEK - .db "peek", 0, 0 + .db "poke", 0 .dw basPOKE - .db "poke", 0, 0 + .db "deek", 0 .dw basDEEK - .db "deek", 0, 0 + .db "doke", 0 .dw basDOKE - .db "doke", 0, 0 + .db "out", 0 .dw basOUT - .db "out", 0, 0, 0 + .db "in", 0 .dw basIN - .db "in", 0, 0, 0, 0 + .db "getc", 0 .dw basGETC - .db "getc", 0, 0 + .db "putc", 0 .dw basPUTC - .db "putc", 0, 0 - .dw basSLEEP .db "sleep", 0 + .dw basSLEEP + .db "addr", 0 .dw basADDR - .db "addr", 0, 0 + .db "usr", 0 .dw basUSR - .db "usr", 0, 0, 0 - .db 0xff, 0xff, 0xff ; end of table + .db 0xff ; end of table diff --git a/apps/lib/util.asm b/apps/lib/util.asm index 3cad42a..58160f8 100644 --- a/apps/lib/util.asm +++ b/apps/lib/util.asm @@ -52,6 +52,19 @@ strcmp: ; early, set otherwise) ret +; Given a string at (HL), move HL until it points to the end of that string. +strskip: + push af + xor a ; look for null char +.loop: + cp (hl) + jp z, .found + inc hl + jr .loop +.found: + pop af + ret + ; Returns length of string at (HL) in A. ; Doesn't include null termination. strlen: