-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix return value of strcmp and strncmp, optimize strncmp loop
- Loading branch information
1 parent
7abe3b5
commit 8ef6850
Showing
2 changed files
with
36 additions
and
42 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 |
---|---|---|
@@ -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 | ||
|