mirror of
https://github.com/hsoft/collapseos.git
synced 2024-11-23 22:48:05 +11:00
basic: add while command
This commit is contained in:
parent
51c977f2ed
commit
3db38b0d89
@ -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
|
an expression. If invoked in direct mode, `run` must be called to actually
|
||||||
run the line (followed by the next, and so on).
|
run the line (followed by the next, and so on).
|
||||||
|
|
||||||
`if <cond> <cmd>`: If specified condition is true, execute the rest of the
|
`if <cond> <cmds>`: 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
|
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
|
2<1 print 12` does nothing. The argument for this command is a "thruth
|
||||||
expression".
|
expression".
|
||||||
|
|
||||||
|
`while <cond> <cmds>`: As long as specified condition is true, execute specified
|
||||||
|
commands repeatedly.
|
||||||
|
|
||||||
`input [<prompt>]`: Prompts the user for a numerical value and puts that
|
`input [<prompt>]`: Prompts the user for a numerical value and puts that
|
||||||
value in `A`. The prompted value is evaluated as an expression and then stored.
|
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
|
The command takes an optional string literal parameter. If present, that string
|
||||||
|
@ -267,14 +267,20 @@ basGOTO:
|
|||||||
ld (BAS_PNEXTLN), de
|
ld (BAS_PNEXTLN), de
|
||||||
ret
|
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
|
push hl ; --> lvl 1. original arg
|
||||||
ld de, SCRATCHPAD
|
ld de, SCRATCHPAD
|
||||||
call rdWord
|
call rdWord
|
||||||
ex de, hl
|
ex de, hl
|
||||||
call parseTruth
|
call parseTruth
|
||||||
pop hl ; <-- lvl 1. restore
|
pop hl ; <-- lvl 1. restore
|
||||||
ret nz
|
ret
|
||||||
|
|
||||||
|
basIF:
|
||||||
|
call _basEvalCond
|
||||||
|
ret nz ; error
|
||||||
or a
|
or a
|
||||||
ret z
|
ret z
|
||||||
; expr is true, execute next
|
; expr is true, execute next
|
||||||
@ -285,6 +291,26 @@ basIF:
|
|||||||
ld de, basCmds2
|
ld de, basCmds2
|
||||||
jp basCallCmds
|
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:
|
basINPUT:
|
||||||
; If our first arg is a string literal, spit it
|
; If our first arg is a string literal, spit it
|
||||||
call spitQuoted
|
call spitQuoted
|
||||||
@ -478,6 +504,8 @@ basCmds2:
|
|||||||
.dw basGOTO
|
.dw basGOTO
|
||||||
.db "if", 0
|
.db "if", 0
|
||||||
.dw basIF
|
.dw basIF
|
||||||
|
.db "while", 0
|
||||||
|
.dw basWHILE
|
||||||
.db "input", 0
|
.db "input", 0
|
||||||
.dw basINPUT
|
.dw basINPUT
|
||||||
.db "peek", 0
|
.db "peek", 0
|
||||||
|
@ -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:
|
could do to copy memory around:
|
||||||
|
|
||||||
> m=0xe000
|
> m=0xe000
|
||||||
> 10 getc
|
> while m<0xe004 getc:poke m a:m=m+1
|
||||||
> 20 poke m a
|
|
||||||
> 30 m=m+1
|
|
||||||
> 40 if m<0xe004 goto 10
|
|
||||||
> run
|
|
||||||
[enter "abcd"]
|
[enter "abcd"]
|
||||||
> bsel 3
|
> bsel 3
|
||||||
> clear
|
> i=0
|
||||||
> 10 getb
|
> while i<4 getb:puth a:i=i+1
|
||||||
> 20 puth a
|
61626364> bseek 2
|
||||||
> run
|
> getb:puth a
|
||||||
61> run
|
63> getb:puth a
|
||||||
62> run
|
|
||||||
63> run
|
|
||||||
64> bseek 2
|
|
||||||
> run
|
|
||||||
63> run
|
|
||||||
64>
|
64>
|
||||||
|
@ -21,9 +21,7 @@ increase a number at memory address `0xa100`. First, compile it:
|
|||||||
Now, we'll send that code to address `0xa000`:
|
Now, we'll send that code to address `0xa000`:
|
||||||
|
|
||||||
> m=0xa000
|
> m=0xa000
|
||||||
> 10 getc
|
> while m<0xa008 getc:poke m a:m=m+1
|
||||||
> 20 poke m a
|
|
||||||
> 30 if m<0xa008 goto 10
|
|
||||||
(resulting binary is 8 bytes long)
|
(resulting binary is 8 bytes long)
|
||||||
|
|
||||||
Now, at this point, it's a bit delicate. To pipe your binary to your serial
|
Now, at this point, it's a bit delicate. To pipe your binary to your serial
|
||||||
|
@ -61,6 +61,7 @@
|
|||||||
.equ STDIO_RAMSTART BLOCKDEV_RAMEND
|
.equ STDIO_RAMSTART BLOCKDEV_RAMEND
|
||||||
.equ STDIO_GETC emulGetC
|
.equ STDIO_GETC emulGetC
|
||||||
.equ STDIO_PUTC emulPutC
|
.equ STDIO_PUTC emulPutC
|
||||||
|
.equ STDIO_BUFSIZE 0x40 ; override
|
||||||
.inc "stdio.asm"
|
.inc "stdio.asm"
|
||||||
|
|
||||||
.equ FS_RAMSTART STDIO_RAMEND
|
.equ FS_RAMSTART STDIO_RAMEND
|
||||||
@ -70,7 +71,7 @@
|
|||||||
; *** BASIC ***
|
; *** BASIC ***
|
||||||
|
|
||||||
; RAM space used in different routines for short term processing.
|
; RAM space used in different routines for short term processing.
|
||||||
.equ SCRATCHPAD_SIZE 0x20
|
.equ SCRATCHPAD_SIZE STDIO_BUFSIZE
|
||||||
.equ SCRATCHPAD FS_RAMEND
|
.equ SCRATCHPAD FS_RAMEND
|
||||||
.inc "lib/util.asm"
|
.inc "lib/util.asm"
|
||||||
.inc "lib/ari.asm"
|
.inc "lib/ari.asm"
|
||||||
|
Loading…
Reference in New Issue
Block a user