]> git.sur5r.net Git - cc65/blob - libsrc/common/strchr.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / common / strchr.s
1 ;
2 ; Ullrich von Bassewitz, 31.05.1998
3 ;
4 ; const char* strchr (const char* s, int c);
5 ;
6
7         .export         _strchr
8         .import         popax
9         .importzp       ptr1, tmp1
10
11 _strchr:
12         sta     tmp1            ; Save c
13         jsr     popax           ; get s
14         sta     ptr1
15         stx     ptr1+1
16         ldy     #0
17
18 Loop:   lda     (ptr1),y        ; Get next char
19         beq     EOS             ; Jump on end of string
20         cmp     tmp1            ; Found?
21         beq     Found           ; Jump if yes
22         iny
23         bne     Loop
24         inc     ptr1+1
25         bne     Loop            ; Branch always
26
27 ; End of string. Check if we're searching for the terminating zero
28
29 EOS:    lda     tmp1            ; Get the char we're searching for
30         bne     NotFound        ; Jump if not searching for terminator
31
32 ; Found. Calculate pointer to c.
33
34 Found:  ldx     ptr1+1          ; Load high byte of pointer
35         tya                     ; Low byte offset          
36         clc
37         adc     ptr1
38         bcc     Found1
39         inx
40 Found1: rts
41
42 ; Not found, return NULL
43
44 NotFound:
45         lda     #0
46         tax
47         rts
48