]> git.sur5r.net Git - cc65/blob - libsrc/common/strrchr.s
Changed most "backticks" (grave accents) into apostrophes.
[cc65] / libsrc / common / strrchr.s
1 ;
2 ; Ullrich von Bassewitz, 31.05.1998
3 ; Christian Krueger: 2013-Aug-01, optimization
4 ;
5 ; char* strrchr (const char* s, int c);
6 ;
7
8         .export         _strrchr
9         .import         popax
10         .importzp       ptr1, tmp1, tmp2
11
12 _strrchr:
13         sta tmp1        ; Save c
14         jsr popax       ; get s
15         tay             ; low byte to y
16         stx ptr1+1
17         ldx #0          ; default function result is NULL, X is high byte...
18         stx tmp2        ; tmp2 is low-byte
19         stx ptr1        ; low-byte of source string is in Y, so clear real one... 
20    
21 testChar:
22         lda (ptr1),y    ; get char
23         beq finished    ; jump if end of string
24         cmp tmp1        ; found?
25         bne nextChar    ; jump if no
26
27 charFound:
28         sty tmp2        ; y has low byte of location, save it
29         ldx ptr1+1      ; x holds high-byte of result
30
31 nextChar:
32         iny
33         bne testChar
34         inc ptr1+1
35         bne testChar    ; here like bra...
36
37 ; return the pointer to the last occurrence
38
39 finished:
40         lda tmp2        ; high byte in X is already correct...
41         rts