Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in return value of strcmp/strncmp #12

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/libc/setjmp.src
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
_setjmp:
ld iy,0
add iy,sp
push iy ; replacing ld hl,iy ZDS pseudo op
pop hl
lea hl,iy ; replacing ld hl,iy ZDS pseudo op
ld iy,(iy+3) ;get jmp_buf
ld (iy+3),ix ;save ix
ld (iy+6),hl ;save sp
Expand Down
15 changes: 7 additions & 8 deletions lib/libc/strcmp.src
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; (c) Copyright 2007-2008 Zilog, Inc.
; int strcmp(register char *s1, register char *s2)
; Changed to remove bug of typecasting difference value to signed char - should be difference of unsigned chars
; Brendan Fletcher 19/03/2024

assume ADL=1

Expand All @@ -14,22 +16,19 @@ _strcmp:
inc hl
ld hl, (hl)

_loop:
.loop:
ld a, (de)
or a, a
jr z, _done
jr z, .done
cpi
inc de
jr z, _loop
jr z, .loop

dec hl
_done:
.done:
sub a, (hl)

ld b, a
rla
sbc hl, hl
ld l, b
ld l, a
ret


63 changes: 29 additions & 34 deletions lib/libc/strncmp.src
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
; (c) Copyright 2007-2008 Zilog, Inc.
; int strncmp(char *s1,char *s2,size_t n)
; Changed to remove bug of typecasting difference value to signed char - should be difference of unsigned chars
; Brendan Fletcher 19/03/2024

ASSUME ADL=1

section .text
public _strncmp
_strncmp:
ld iy, 0
add iy, sp

ld bc, (iy+9)
ld a, (iy+11)
or a, c
or a, b
jr z, _done ; n==0 ? return(0)

ld hl, (iy+6)
ld de, (iy+3)

_cloop:
ld a, (de)
ld iy, 0
add iy, sp

ld bc, (iy+9)
ld a, (iy+11)
or a, c
or a, b
jr z, .done ; n==0 ? return(0)

ld hl, (iy+6)
ld de, (iy+3)

.cloop:
ld a, (de)
or a, a
jr z, .diffnul ; return the difference value if *s1=='\0'
cpi
jr nz, _diff ; return the difference value if mismatch occurs
jp po, _done ; if all the chars over then return(0)
or a, a
jr z, _done ; if *s1=='\0' return(0)
inc de
jr _cloop

_diff:
dec hl
sub a, (hl)
ld b, a
rla
sbc hl, hl
ld l, b
jr nz, .diff ; return the difference value if mismatch occurs
inc de
jp pe, .cloop ; if all the chars not over then loop

.diff:
dec hl
.diffnul:
sub a, (hl)
.done:
sbc hl, hl
ld l, a
ret

_done:
or a, a
sbc hl, hl
ret

11 changes: 5 additions & 6 deletions lib/libc/strstr.src
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public _strstr
_strstr:
ld hl, 6
add hl, sp
add hl, sp
ld iy, (hl) ; bc = s2
dec hl
dec hl
Expand All @@ -22,21 +22,20 @@ _L0:
add hl,de
cp a,(hl)
jr z,_L3 ; *s1 == '\0'
push iy ; replace ZDS pseudo op ld bc,iy
pop bc
lea bc,iy ; replace ZDS pseudo op ld bc,iy
inc de
_L1:
ld a,(bc)
or a,a
jr z,_L2 ; *s2 == '\0'
cp a,(hl) ; *s1 == *s2
inc hl ; s1 ++
inc bc ; s2 ++
inc hl ; s1 ++
inc bc ; s2 ++
jr z,_L1
jr _L0
_L2:
ex de,hl
dec hl
dec hl
ret
_L3:
sbc hl,hl
Expand Down