]> git.sur5r.net Git - cc65/blob - libsrc/common/strchr.s
un-remove TABs in doc/using-make.sgml
[cc65] / libsrc / common / strchr.s
1 ;
2 ; Ullrich von Bassewitz, 31.05.1998
3 ; Christian Krueger, 2013-Aug-04, minor optimization
4 ;
5 ; const char* strchr (const char* s, int c);
6 ;
7
8         .export         _strchr
9         .import         popax
10         .importzp       ptr1, tmp1
11         .macpack        cpu
12
13 _strchr:
14         sta tmp1        ; Save c
15         jsr popax       ; get s
16         tay             ; low byte of pointer to y
17         stx ptr1+1
18 .if (.cpu .bitand ::CPU_ISET_65SC02)
19         stz ptr1
20 .else
21         lda #0
22         sta ptr1        ; access from page start, y contains low byte
23 .endif
24
25 Loop:   lda (ptr1),y    ; Get next char
26         beq EOS         ; Jump on end of string
27         cmp tmp1        ; Found?
28         beq Found       ; Jump if yes
29         iny
30         bne Loop
31         inc ptr1+1
32         bne Loop        ; Branch always
33
34 ; End of string. Check if we're searching for the terminating zero
35
36 EOS:
37         lda tmp1        ; Get the char we're searching for
38         bne NotFound    ; Jump if not searching for terminator
39
40 ; Found. Set pointer to c.
41
42 Found:
43         ldx ptr1+1      ; Load high byte of pointer
44         tya             ; low byte is in y
45         rts
46
47 ; Not found, return NULL
48
49 NotFound:
50         lda #0
51         tax
52         rts