]> git.sur5r.net Git - cc65/blob - libsrc/common/memchr.s
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / libsrc / common / memchr.s
1 ;
2 ; Ullrich von Bassewitz, 09.06.1998
3 ;
4 ; void* memchr (const void* p, int c, size_t n);
5 ;
6
7         .export         _memchr
8         .import         popax, return0
9         .importzp       ptr1, ptr2, tmp1
10
11
12 _memchr:
13         sta     ptr2            ; Save n
14         stx     ptr2+1
15         jsr     popax           ; get c
16         sta     tmp1
17         jsr     popax           ; get p
18         sta     ptr1
19         stx     ptr1+1
20
21         ldy     #0
22         lda     tmp1            ; get c
23         ldx     ptr2            ; use X as low counter byte
24         beq     L3              ; check high byte
25
26 ; Search for the char
27
28 L1:     cmp     (ptr1),y
29         beq     L5              ; jump if found
30         iny
31         bne     L2
32         inc     ptr1+1
33 L2:     cpx     #0
34         beq     L3
35         dex
36         jmp     L1
37 L3:     ldx     ptr2+1          ; Check high byte
38         beq     L4              ; Jump if counter off
39         dec     ptr2+1
40         ldx     #$FF
41         bne     L1              ; branch always
42
43 ; Character not found, return zero
44
45 L4:     jmp     return0
46
47 ; Character found, calculate pointer
48
49 L5:     ldx     ptr1+1          ; get high byte of pointer
50         tya                     ; low byte offset
51         clc
52         adc     ptr1
53         bcc     L6
54         inx
55 L6:     rts
56
57