2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 25.05.2000
|
|
|
|
;
|
|
|
|
; int strncmp (const char* s1, const char* s2, unsigned n);
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _strncmp
|
2018-05-20 13:30:18 +00:00
|
|
|
.import popax, popptr1
|
2013-05-09 11:56:54 +00:00
|
|
|
.importzp ptr1, ptr2, ptr3
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
_strncmp:
|
|
|
|
|
|
|
|
; Convert the given counter value in a/x from a downward counter into an
|
|
|
|
; upward counter, so we can increment the counter in the loop below instead
|
|
|
|
; of decrementing it. This adds some overhead now, but is cheaper than
|
|
|
|
; executing a more complex test in each iteration of the loop. We do also
|
|
|
|
; correct the value by one, so we can do the test on top of the loop.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
eor #$FF
|
|
|
|
sta ptr3
|
|
|
|
txa
|
|
|
|
eor #$FF
|
|
|
|
sta ptr3+1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Get the remaining arguments
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popax ; get s2
|
|
|
|
sta ptr2
|
|
|
|
stx ptr2+1
|
2018-05-20 13:30:18 +00:00
|
|
|
jsr popptr1 ; get s1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Loop setup
|
|
|
|
|
2018-05-20 13:30:18 +00:00
|
|
|
;ldy #0 Y=0 guaranteed by popptr1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Start of compare loop. Check the counter.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
Loop: inc ptr3
|
|
|
|
beq IncHi ; Increment high byte
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Compare a byte from the strings
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
Comp: lda (ptr1),y
|
|
|
|
cmp (ptr2),y
|
|
|
|
bne NotEqual ; Jump if strings different
|
|
|
|
tax ; End of strings?
|
|
|
|
beq Equal1 ; Jump if EOS reached, a/x == 0
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Increment the pointers
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
iny
|
|
|
|
bne Loop
|
|
|
|
inc ptr1+1
|
|
|
|
inc ptr2+1
|
|
|
|
bne Loop ; Branch always
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Increment hi byte
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
IncHi: inc ptr3+1
|
|
|
|
bne Comp ; Jump if counter not zero
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Exit code if strings are equal. a/x not set
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
Equal: lda #$00
|
|
|
|
tax
|
|
|
|
Equal1: rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Exit code if strings not equal
|
|
|
|
|
|
|
|
NotEqual:
|
2013-05-09 11:56:54 +00:00
|
|
|
bcs L1
|
|
|
|
ldx #$FF ; Make result negative
|
|
|
|
rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
L1: ldx #$01 ; Make result positive
|
|
|
|
rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|