]> git.sur5r.net Git - cc65/blob - libsrc/common/strchr.s
Added further optimizations and unit tests.
[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
12 _strchr:
13         sta tmp1        ; Save c
14         jsr popax       ; get s
15         tay             ; low byte of pointer to y
16         stx ptr1+1
17 .if (.cpu .bitand ::CPU_ISET_65SC02)
18         stz ptr1
19 .else
20         lda #0
21         sta ptr1        ; access from page start, y contains low byte
22 .endif        
23
24 Loop:   lda (ptr1),y    ; Get next char
25         beq EOS         ; Jump on end of string
26         cmp tmp1        ; Found?
27         beq Found       ; Jump if yes
28         iny
29         bne Loop
30         inc ptr1+1
31         bne Loop        ; Branch always                 
32
33 ; End of string. Check if we're searching for the terminating zero
34
35 EOS:
36         lda tmp1        ; Get the char we're searching for
37         bne NotFound    ; Jump if not searching for terminator
38
39 ; Found. Set pointer to c.
40
41 Found:
42         ldx ptr1+1      ; Load high byte of pointer
43         tya             ; low byte is in y
44         rts
45
46 ; Not found, return NULL
47
48 NotFound:
49         lda #0
50         tax
51         rts