From 3db38b0d89e5efb6896d93e694e1d09f07a982b2 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Thu, 12 Dec 2019 11:17:10 -0500 Subject: [PATCH] basic: add while command --- apps/basic/README.md | 5 ++++- apps/basic/main.asm | 32 ++++++++++++++++++++++++++++++-- doc/blockdev.md | 21 ++++++--------------- doc/load-run-code.md | 4 +--- tools/emul/shell/glue.asm | 3 ++- 5 files changed, 43 insertions(+), 22 deletions(-) diff --git a/apps/basic/README.md b/apps/basic/README.md index 8af3b08..9a6d705 100644 --- a/apps/basic/README.md +++ b/apps/basic/README.md @@ -126,11 +126,14 @@ specified as an argument. Errors out if line doesn't exist. Argument can be an expression. If invoked in direct mode, `run` must be called to actually run the line (followed by the next, and so on). -`if `: If specified condition is true, execute the rest of the +`if `: If specified condition is true, execute the rest of the line. Otherwise, do nothing. For example, `if 2>1 print 12` prints `12` and `if 2<1 print 12` does nothing. The argument for this command is a "thruth expression". +`while `: As long as specified condition is true, execute specified +commands repeatedly. + `input []`: Prompts the user for a numerical value and puts that value in `A`. The prompted value is evaluated as an expression and then stored. The command takes an optional string literal parameter. If present, that string diff --git a/apps/basic/main.asm b/apps/basic/main.asm index f4559f7..a1f8ef2 100644 --- a/apps/basic/main.asm +++ b/apps/basic/main.asm @@ -267,14 +267,20 @@ basGOTO: ld (BAS_PNEXTLN), de ret -basIF: +; evaluate truth condition at (HL) and set A to its value +; Z for success (but not truth!) +_basEvalCond: push hl ; --> lvl 1. original arg ld de, SCRATCHPAD call rdWord ex de, hl call parseTruth pop hl ; <-- lvl 1. restore - ret nz + ret + +basIF: + call _basEvalCond + ret nz ; error or a ret z ; expr is true, execute next @@ -285,6 +291,26 @@ basIF: ld de, basCmds2 jp basCallCmds +basWHILE: + push hl ; --> lvl 1 + call _basEvalCond + jr nz, .stop ; error + or a + jr z, .stop + ret z + ; expr is true, execute next + ; (HL) back to beginning of args, skip to next arg + call toSepOrEnd + call rdSep + ret nz + ld de, basCmds2 + call basCallCmds + pop hl ; <-- lvl 1 + jr basWHILE +.stop: + pop hl ; <-- lvl 1 + ret + basINPUT: ; If our first arg is a string literal, spit it call spitQuoted @@ -478,6 +504,8 @@ basCmds2: .dw basGOTO .db "if", 0 .dw basIF + .db "while", 0 + .dw basWHILE .db "input", 0 .dw basINPUT .db "peek", 0 diff --git a/doc/blockdev.md b/doc/blockdev.md index ddb0ad4..cf3d949 100644 --- a/doc/blockdev.md +++ b/doc/blockdev.md @@ -52,21 +52,12 @@ Let's try an example: You glue yourself a Collapse OS with a mmap starting at could do to copy memory around: > m=0xe000 - > 10 getc - > 20 poke m a - > 30 m=m+1 - > 40 if m<0xe004 goto 10 - > run + > while m<0xe004 getc:poke m a:m=m+1 [enter "abcd"] > bsel 3 - > clear - > 10 getb - > 20 puth a - > run - 61> run - 62> run - 63> run - 64> bseek 2 - > run - 63> run + > i=0 + > while i<4 getb:puth a:i=i+1 + 61626364> bseek 2 + > getb:puth a + 63> getb:puth a 64> diff --git a/doc/load-run-code.md b/doc/load-run-code.md index e0eb477..9766473 100644 --- a/doc/load-run-code.md +++ b/doc/load-run-code.md @@ -21,9 +21,7 @@ increase a number at memory address `0xa100`. First, compile it: Now, we'll send that code to address `0xa000`: > m=0xa000 - > 10 getc - > 20 poke m a - > 30 if m<0xa008 goto 10 + > while m<0xa008 getc:poke m a:m=m+1 (resulting binary is 8 bytes long) Now, at this point, it's a bit delicate. To pipe your binary to your serial diff --git a/tools/emul/shell/glue.asm b/tools/emul/shell/glue.asm index 80b0c97..6bf7ee8 100644 --- a/tools/emul/shell/glue.asm +++ b/tools/emul/shell/glue.asm @@ -61,6 +61,7 @@ .equ STDIO_RAMSTART BLOCKDEV_RAMEND .equ STDIO_GETC emulGetC .equ STDIO_PUTC emulPutC +.equ STDIO_BUFSIZE 0x40 ; override .inc "stdio.asm" .equ FS_RAMSTART STDIO_RAMEND @@ -70,7 +71,7 @@ ; *** BASIC *** ; RAM space used in different routines for short term processing. -.equ SCRATCHPAD_SIZE 0x20 +.equ SCRATCHPAD_SIZE STDIO_BUFSIZE .equ SCRATCHPAD FS_RAMEND .inc "lib/util.asm" .inc "lib/ari.asm"