]> git.sur5r.net Git - cc65/blob - libsrc/common/strncmp.s
Use 'return0' for default 'doesclrscrafterexit()' implementation in libsrc/common.
[cc65] / libsrc / common / strncmp.s
1 ;
2 ; Ullrich von Bassewitz, 25.05.2000
3 ;
4 ; int strncmp (const char* s1, const char* s2, unsigned n);
5 ;
6
7         .export         _strncmp
8         .import         popax
9         .importzp       ptr1, ptr2, ptr3
10
11
12 _strncmp:
13
14 ; Convert the given counter value in a/x from a downward counter into an
15 ; upward counter, so we can increment the counter in the loop below instead
16 ; of decrementing it. This adds some overhead now, but is cheaper than
17 ; executing a more complex test in each iteration of the loop. We do also
18 ; correct the value by one, so we can do the test on top of the loop.
19
20         eor     #$FF
21         sta     ptr3
22         txa
23         eor     #$FF
24         sta     ptr3+1
25
26 ; Get the remaining arguments
27
28         jsr     popax           ; get s2
29         sta     ptr2
30         stx     ptr2+1
31         jsr     popax           ; get s1
32         sta     ptr1
33         stx     ptr1+1
34
35 ; Loop setup
36
37         ldy     #0
38
39 ; Start of compare loop. Check the counter.
40
41 Loop:   inc     ptr3
42         beq     IncHi           ; Increment high byte
43
44 ; Compare a byte from the strings
45
46 Comp:   lda     (ptr1),y
47         cmp     (ptr2),y
48         bne     NotEqual        ; Jump if strings different
49         tax                     ; End of strings?
50         beq     Equal1          ; Jump if EOS reached, a/x == 0
51
52 ; Increment the pointers
53
54         iny
55         bne     Loop
56         inc     ptr1+1
57         inc     ptr2+1
58         bne     Loop            ; Branch always
59
60 ; Increment hi byte
61
62 IncHi:  inc     ptr3+1
63         bne     Comp            ; Jump if counter not zero
64
65 ; Exit code if strings are equal. a/x not set
66
67 Equal:  lda     #$00
68         tax
69 Equal1: rts
70
71 ; Exit code if strings not equal
72
73 NotEqual:
74         bcs     L1
75         ldx     #$FF            ; Make result negative
76         rts
77
78 L1:     ldx     #$01            ; Make result positive
79         rts
80
81