File: strnicmp.s

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (99 lines) | stat: -rw-r--r-- 3,034 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
;
; Christian Groessler, 10.02.2009
; derived from strncmp.s and stricmp.s
;
; int __fastcall__ strnicmp (const char* s1, const char* s2, size_t count);
; int __fastcall__ strncasecmp (const char* s1, const char* s2, size_t count);
;

        .export         _strnicmp, _strncasecmp
        .import         popax, popptr1
        .importzp       ptr1, ptr2, ptr3, tmp1, tmp2
        .import         ctypemaskdirect
        .include        "ctype.inc"

_strnicmp:
_strncasecmp:

; 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.

        eor     #$FF
        sta     ptr3
        txa
        eor     #$FF
        sta     ptr3+1

; Get the remaining arguments

        jsr     popax           ; get s2
        sta     ptr2
        stx     ptr2+1
        jsr     popptr1         ; get s1

; Loop setup

        ; ldy     #0            Y=0 guaranteed by popptr1

; Start of compare loop. Check the counter.

Loop:   inc     ptr3
        beq     IncHi           ; increment high byte

; Compare a byte from the strings

Comp:   lda     (ptr2),y
        sta     tmp2            ; remember original char
        jsr     ctypemaskdirect ; get character classification
        and     #CT_LOWER       ; lower case char?
        beq     L1              ; jump if no
        lda     #<('A'-'a')     ; make upper case char
        adc     tmp2            ; ctypemaskdirect ensures carry clear!
        sta     tmp2            ; remember upper case equivalent

L1:     lda     (ptr1),y        ; get character from first string
        sta     tmp1            ; remember original char
        jsr     ctypemaskdirect ; get character classification
        and     #CT_LOWER       ; lower case char?
        beq     L2              ; jump if no
        lda     #<('A'-'a')     ; make upper case char
        adc     tmp1            ; ctypemaskdirect ensures carry clear!
        sta     tmp1            ; remember upper case equivalent

L2:     ldx     tmp1
        cpx     tmp2            ; compare characters
        bne     NotEqual        ; jump if strings different
        txa                     ; end of strings?
        beq     Equal1          ; jump if EOS reached, a/x == 0

; Increment the pointers

        iny
        bne     Loop
        inc     ptr1+1
        inc     ptr2+1
        bne     Loop            ; branch always

; Increment hi byte

IncHi:  inc     ptr3+1
        bne     Comp            ; jump if counter not zero

; Exit code if strings are equal. a/x not set

Equal:  lda     #$00
        tax
Equal1: rts

; Exit code if strings not equal

NotEqual:
        bcs     L3
        ldx     #$FF            ; make result negative
        rts

L3:     ldx     #$01            ; make result positive
        rts