X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libsrc%2Fcommon%2Fmemchr.s;h=1b60cbf73e5fee9db3dc394b3fb0be5477750a52;hb=934a78731fe1b7fc8d08814fa2321534c20fbc62;hp=59186ae453aeec31d289d3dc1d9a239d5edc679b;hpb=53dd513176425872128ef26031d00952ef7a0628;p=cc65 diff --git a/libsrc/common/memchr.s b/libsrc/common/memchr.s index 59186ae45..1b60cbf73 100644 --- a/libsrc/common/memchr.s +++ b/libsrc/common/memchr.s @@ -1,57 +1,57 @@ ; -; Ullrich von Bassewitz, 09.06.1998 +; Ullrich von Bassewitz, 2003-05-05 ; -; void* memchr (const void* p, int c, size_t n); +; void* __fastcall__ memchr (const void* p, int c, size_t n); ; - .export _memchr - .import popax, return0 - .importzp ptr1, ptr2, tmp1 - - -_memchr: - sta ptr2 ; Save n - stx ptr2+1 - jsr popax ; get c - sta tmp1 - jsr popax ; get p - sta ptr1 - stx ptr1+1 - - ldy #0 - lda tmp1 ; get c - ldx ptr2 ; use X as low counter byte - beq L3 ; check high byte - -; Search for the char - -L1: cmp (ptr1),y - beq L5 ; jump if found - iny - bne L2 - inc ptr1+1 -L2: cpx #0 - beq L3 - dex - jmp L1 -L3: ldx ptr2+1 ; Check high byte - beq L4 ; Jump if counter off - dec ptr2+1 - ldx #$FF - bne L1 ; branch always - -; Character not found, return zero - -L4: jmp return0 - -; Character found, calculate pointer - -L5: ldx ptr1+1 ; get high byte of pointer - tya ; low byte offset - clc - adc ptr1 - bcc L6 - inx -L6: rts + .export _memchr + .import popax, return0 + .importzp ptr1, ptr2 +.proc _memchr + + eor #$FF + sta ptr2 + txa + eor #$FF + sta ptr2+1 ; Save ones complement of n + jsr popax ; get c + pha + jsr popax ; get p + sta ptr1 + stx ptr1+1 + + ldy #$00 + pla ; Get c + ldx ptr2 ; Use X as low counter byte + +L1: inx + beq L3 +L2: cmp (ptr1),y + beq found + iny + bne L1 + inc ptr1+1 + bne L1 ; Branch always + +L3: inc ptr2+1 ; Bump counter high byte + bne L2 + +; Not found, return NULL + +notfound: + jmp return0 + +; Found, return pointer to char + +found: ldx ptr1+1 ; get high byte of pointer + tya ; low byte offset + clc + adc ptr1 + bcc L9 + inx +L9: rts + +.endproc +