]> git.sur5r.net Git - cc65/blob - libsrc/common/strcspn.s
Merge pull request #310 from groessler/atari-exec-devel
[cc65] / libsrc / common / strcspn.s
1 ;
2 ; Ullrich von Bassewitz, 11.06.1998
3 ;
4 ; size_t strcspn (const char* s1, const char* s2);
5 ;
6
7         .export         _strcspn
8         .import         popax
9         .importzp       ptr1, ptr2, tmp1, tmp2, tmp3
10
11 _strcspn:
12         sta     ptr2            ; Save s2
13         stx     ptr2+1
14         jsr     popax           ; Get s1
15         sta     ptr1
16         stx     ptr1+1
17         ldx     #0              ; low counter byte
18         stx     tmp1            ; high counter byte
19         ldy     #$00
20
21 L1:     lda     (ptr1),y        ; get next char from s1
22         beq     L6              ; jump if done
23         sta     tmp2            ; save char
24         iny
25         bne     L2
26         inc     ptr1+1
27 L2:     sty     tmp3            ; save index into s1
28
29         ldy     #0              ; get index into s2
30 L3:     lda     (ptr2),y        ;
31         beq     L4              ; jump if done
32         cmp     tmp2
33         beq     L6
34         iny
35         bne     L3
36
37 ; The character was not found in s2. Increment the counter and start over
38
39 L4:     ldy     tmp3            ; reload index
40         inx
41         bne     L1
42         inc     tmp1
43         bne     L1
44
45 ; The character was found, or we reached the end of s1. Return count of
46 ; characters
47
48 L6:     txa                     ; get low counter byte
49         ldx     tmp1            ; get high counter byte
50         rts
51
52
53
54