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, popptr1
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     popptr1         ; get s1
32
33; Loop setup
34
35        ;ldy     #0               Y=0 guaranteed by popptr1
36
37; Start of compare loop. Check the counter.
38
39Loop:   inc     ptr3
40        beq     IncHi           ; Increment high byte
41
42; Compare a byte from the strings
43
44Comp:   lda     (ptr1),y
45        cmp     (ptr2),y
46        bne     NotEqual        ; Jump if strings different
47        tax                     ; End of strings?
48        beq     Equal1          ; Jump if EOS reached, a/x == 0
49
50; Increment the pointers
51
52        iny
53        bne     Loop
54        inc     ptr1+1
55        inc     ptr2+1
56        bne     Loop            ; Branch always
57
58; Increment hi byte
59
60IncHi:  inc     ptr3+1
61        bne     Comp            ; Jump if counter not zero
62
63; Exit code if strings are equal. a/x not set
64
65Equal:  lda     #$00
66        tax
67Equal1: rts
68
69; Exit code if strings not equal
70
71NotEqual:
72        bcs     L1
73        ldx     #$FF            ; Make result negative
74        rts
75
76L1:     ldx     #$01            ; Make result positive
77        rts
78
79
80