]> git.sur5r.net Git - cc65/blob - libsrc/common/strchr.s
Optimization of two string functions (size & speed).
[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         lda #0
18         sta ptr1        ; ptr access page wise
19
20 Loop:   lda (ptr1),y    ; Get next char
21         beq EOS         ; Jump on end of string
22         cmp tmp1        ; Found?
23         beq Found       ; Jump if yes
24         iny
25         bne Loop
26         inc ptr1+1
27         bne Loop        ; Branch always                 
28
29 ; End of string. Check if we're searching for the terminating zero
30
31 EOS:
32         lda tmp1        ; Get the char we're searching for
33         bne NotFound    ; Jump if not searching for terminator
34
35 ; Found. Set pointer to c.
36
37 Found:
38         ldx ptr1+1      ; Load high byte of pointer
39         tya             ; low byte is in y
40         rts
41
42 ; Not found, return NULL
43
44 NotFound:
45         lda #0
46         tax
47         rts