Skip to content

Commit

Permalink
Fix/optimize str[n]casecmp, remove buggy OS tolower/toupper
Browse files Browse the repository at this point in the history
  • Loading branch information
calc84maniac committed Feb 27, 2024
1 parent f979fd6 commit 40e668e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 70 deletions.
2 changes: 0 additions & 2 deletions src/libc/os.src
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ _strlen := 0000D4h
_strncat := 0000D8h
public _strncmp
_strncmp := 0000DCh
public _strcasecmp
_strcasecmp := 021E3Ch
public _strncpy
_strncpy := 0000E0h
public _strpbrk
Expand Down
95 changes: 95 additions & 0 deletions src/libc/strcasecmp.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
assume adl=1

section .text
public _strcasecmp

if PREFER_OS_LIBC

_strcasecmp := 021E3Ch

else

_strcasecmp:
pop iy
pop de
ex (sp), hl
push de
ld b, 1+'z'-'a'
ld c, 'a'-'A'
jr .loop_entry
.loop:
cp a, (hl)
jr z, .done
.loop_nonnull:
inc hl
inc de
.loop_entry:
ld a, (de)
xor a, (hl)
jr z, .loop
cp a, c
jr nz, .mismatch
or a, (hl)
sub a, 'a'
cp a, b
jr c, .loop_nonnull

end if

section .text
public _strcasecmp.mismatch
public _strcasecmp.done
_strcasecmp.mismatch:
ld l, (hl)
call _tolower.internal
ex de, hl
ld l, (hl)
call _tolower.internal
ld a, l
sub a, e
_strcasecmp.done:
sbc hl, hl
ld l, a
jp (iy)

section .text
public _strncasecmp
_strncasecmp:
pop iy
pop de
pop hl
pop bc
push bc
push de
push hl

scf
sbc hl, hl
add hl, bc
ex (sp), hl

call c, .loop_entry
jr c, _strcasecmp.mismatch
sbc hl, hl
jp (iy)

.checkcase:
or a, (hl)
sub a, 'a'
add a, -(1+'z'-'a')
ret c
.loop:
cpi
ret po
ret z
inc de
.loop_entry:
ld a, (de)
xor a, (hl)
jr z, .loop
cp a, 'a'-'A'
jr z, .checkcase
scf
ret

extern _tolower.internal
48 changes: 0 additions & 48 deletions src/libc/strncasecmp.src

This file was deleted.

16 changes: 6 additions & 10 deletions src/libc/tolower.src
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
section .text
public _tolower

if PREFER_OS_LIBC

_tolower := 021E34h

else

_tolower:
pop de
ex (sp), hl
push de
require _tolower.internal

section .text
public _tolower.internal
; ASM interface: input/output char in L, destroys AF
_tolower.internal:
ld a, l
sub a, 'A'
cp a, 1+'Z'-'A'
ret nc

add a, 'a'
ld l, a
set 5, l
ret

end if
16 changes: 6 additions & 10 deletions src/libc/toupper.src
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
section .text
public _toupper

if PREFER_OS_LIBC

_toupper := 021E38h

else

_toupper:
pop de
ex (sp), hl
push de
require _toupper.internal

section .text
public _toupper.internal
; ASM interface: input/output char in L, destroys AF
.internal:
ld a, l
sub a, 'a'
cp a, 1+'z'-'a'
ret nc

add a, 'A'
ld l, a
res 5, l
ret

end if

0 comments on commit 40e668e

Please sign in to comment.