]> git.sur5r.net Git - cc65/blob - libsrc/common/memchr.s
un-remove TABs in doc/using-make.sgml
[cc65] / libsrc / common / memchr.s
1 ;
2 ; Ullrich von Bassewitz, 2003-05-05
3 ;
4 ; void* __fastcall__ memchr (const void* p, int c, size_t n);
5 ;
6
7         .export         _memchr
8         .import         popax, popptr1, return0
9         .importzp       ptr1, ptr2
10
11
12 .proc   _memchr
13
14         eor     #$FF
15         sta     ptr2
16         txa
17         eor     #$FF
18         sta     ptr2+1          ; Save ones complement of n
19         jsr     popax           ; get c
20         pha
21
22         jsr     popptr1         ; get p
23
24         ; ldy     #$00            is guaranteed by popptr1
25         pla                     ; Get c
26         ldx     ptr2            ; Use X as low counter byte
27
28 L1:     inx
29         beq     L3
30 L2:     cmp     (ptr1),y
31         beq     found
32         iny
33         bne     L1
34         inc     ptr1+1
35         bne     L1              ; Branch always
36
37 L3:     inc     ptr2+1          ; Bump counter high byte
38         bne     L2
39
40 ; Not found, return NULL
41
42 notfound:
43         jmp     return0
44
45 ; Found, return pointer to char
46
47 found:  ldx     ptr1+1          ; get high byte of pointer
48         tya                     ; low byte offset
49         clc
50         adc     ptr1
51         bcc     L9
52         inx
53 L9:     rts
54
55 .endproc
56