forked from Zeal8bit/Zeal-8-bit-OS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new trivial initial versions of expr and echo, and prepares parse + strutils to be able to do more.
- Loading branch information
Showing
6 changed files
with
324 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
; SPDX-FileCopyrightText: 2024 Shawn Sijnstra <shawn@sijnstra.com> | ||
; | ||
; SPDX-License-Identifier: Apache-2.0 | ||
|
||
SECTION TEXT | ||
|
||
;------------------------------------------------------------------------ | ||
; | ||
; | ||
; 16-bit compare of HL and BC | ||
; Equivalent to expected behaviour of "CP HL,BC" | ||
; Parameters: | ||
; HL - Base | ||
; BC - Target | ||
; Returns: | ||
; Z and C flags set according to comparison | ||
; Alters: | ||
; F | ||
|
||
PUBLIC cphlbc | ||
cphlbc: push hl | ||
and a | ||
sbc hl,bc | ||
pop hl | ||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
; SPDX-FileCopyrightText: 2024 Shawn Sijnstra <shawn@sijnstra.com> | ||
; | ||
; SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
INCLUDE "zos_sys.asm" | ||
|
||
SECTION TEXT | ||
|
||
EXTERN error_print | ||
EXTERN strlen | ||
|
||
MACRO ERR_CHECK goto_label | ||
or a | ||
jp nz, goto_label | ||
ENDM | ||
|
||
; "echo" command main function | ||
; Parameters: | ||
; HL - ARGV | ||
; BC - ARGC | ||
; Returns: | ||
; A - 0 on success | ||
PUBLIC echo_main | ||
echo_main: | ||
; Check that argc is at 2 (command itself is part of argc) | ||
ld a, c | ||
cp 2 | ||
jp nz, _echo_usage | ||
; Retrieve the number given as a parameter | ||
inc hl | ||
inc hl ; skip the first pointer | ||
ld e,(hl) ;grab 2nd pointer | ||
inc hl | ||
ld d,(hl) | ||
ex de,hl ;pointer in HL | ||
call strlen ;returns answer in BC | ||
ex de,hl ;DE has the string address | ||
S_WRITE1(DEV_STDOUT) ;DE has address, BC has length | ||
S_WRITE3(DEV_STDOUT, str_usage_end -1,1) ;newline | ||
xor a ;success | ||
ret | ||
|
||
|
||
_echo_usage: | ||
S_WRITE3(DEV_STDOUT, str_usage, str_usage_end - str_usage) | ||
ld a, 1 | ||
ret | ||
|
||
_echo_error: | ||
; Give NULL to error_print to have a default error message | ||
ld de, 0 | ||
call error_print | ||
ld a, 2 | ||
ret | ||
|
||
str_usage: DEFM "usage:\n" | ||
DEFM " echo <string> sends a copy of <string> to STDOUT\n" | ||
str_usage_end: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
; SPDX-FileCopyrightText: 2024 Shawn Sijnstra <shawn@sijnstra.com> | ||
; | ||
; SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
INCLUDE "zos_sys.asm" | ||
INCLUDE "zos_err.asm" | ||
|
||
SECTION TEXT | ||
|
||
EXTERN error_print | ||
EXTERN parse_int | ||
EXTERN strlen | ||
EXTERN byte_to_ascii | ||
EXTERN word_to_ascii_dec_u16 | ||
|
||
MACRO ERR_CHECK goto_label | ||
or a | ||
jp nz, goto_label | ||
ENDM | ||
|
||
; "expr" command main function | ||
; Parameters: | ||
; HL - ARGV | ||
; BC - ARGC | ||
; Returns: | ||
; A - 0 on success | ||
PUBLIC expr_main | ||
expr_main: | ||
|
||
; Retrieve the number given as a parameter | ||
inc hl | ||
inc hl ; skip the first pointer | ||
ld e,(hl) ;grab 2nd pointer | ||
inc hl | ||
ld d,(hl) | ||
ex de,hl ;pointer in HL | ||
; Check that argc is at least 2 (command itself is part of argc) | ||
; This may need to change in future as we allow to evaluate full expressions | ||
ld a, c | ||
sub 2 | ||
jp c, _expr_usage | ||
jp nz, _expr_do_string ;can only be string related function now | ||
|
||
_expr_do_math: | ||
call parse_int ;strutils - accepts 16 bit dec or hex input at HL | ||
or a ;0 is ok, 1 is overflow, 2 is bad digit | ||
jr nz,_expr_usage | ||
; Routine displaying the value of HL in decimal then hex | ||
; Parameters: | ||
; HL - number to display | ||
; Returns: | ||
; A - zero | ||
; successful return to ZealOS | ||
|
||
_expr_print_result: | ||
push hl | ||
ld de,_expr_buff | ||
call word_to_ascii_dec_u16 | ||
ld hl,_expr_buff | ||
ex de,hl | ||
or a | ||
sbc hl,de | ||
ld b,h | ||
ld c,l | ||
S_WRITE2(DEV_STDOUT, _expr_buff) | ||
S_WRITE3(DEV_STDOUT, str_gap,3) | ||
pop hl | ||
ld a,h | ||
call byte_to_ascii | ||
ld a,d | ||
ld d,e | ||
ld e,a | ||
ld (_expr_buff),de | ||
ld a,l | ||
call byte_to_ascii | ||
ld a,d | ||
ld d,e | ||
ld e,a | ||
ld (_expr_buff+2),de | ||
ld a,'\n' | ||
ld (_expr_buff+4),a | ||
S_WRITE3(DEV_STDOUT, _expr_buff,5) | ||
xor a | ||
ret | ||
|
||
_expr_do_string: | ||
; Check that argc is 3 (command itself is part of argc) | ||
; format is "expr l string" which outputs the length of the string | ||
dec a | ||
jp nz, _expr_usage | ||
ld a,(hl) | ||
cp 'l' | ||
jp nz,_expr_error ;it's not the 'length' command so invalid parameter. | ||
ex de,hl ;hl back to pointer table | ||
inc hl ; we didn't inc before | ||
ld e,(hl) ;grab 3rd pointer | ||
inc hl | ||
ld d,(hl) | ||
ex de,hl ;pointer in HL | ||
call strlen ;returns answer in BC | ||
ld h,b | ||
ld l,c | ||
jp _expr_print_result | ||
|
||
|
||
_expr_usage: | ||
S_WRITE3(DEV_STDOUT, str_usage, str_usage_end - str_usage) | ||
ld a, 1 | ||
ret | ||
|
||
_expr_error: | ||
; Give NULL to error_print to have a default error message | ||
ld de, 0 | ||
ld a, ERR_INVALID_PARAMETER | ||
call error_print | ||
ld a,2 | ||
ret | ||
|
||
str_usage: DEFM "usage:\n" | ||
DEFM " expr <X> evaluate the hex or decimal number <X>\n" ;this will be ((expression)) when ready | ||
DEFM " expr l <string> evaluate the length of string\n" | ||
DEFM " Results are shown in decimal and hex\n" | ||
str_usage_end: | ||
str_gap: DEFM " 0x" | ||
|
||
SECTION DATA | ||
_expr_buff: defs 5 ;largest number '65535' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters