2 ; Ullrich von Bassewitz, 11.12.1998
4 ; char* strstr (const char* haystack, const char* needle);
9 .importzp ptr1, ptr2, ptr3, ptr4, tmp1
12 sta ptr2 ; Save needle
14 sta ptr4 ; Setup temp copy for later
16 jsr popax ; Get haystack
18 stx ptr1+1 ; Save haystack
20 ; If needle is empty, return haystack
23 lda (ptr2),y ; Get first byte of needle
24 beq @Found ; Needle is empty --> we're done
26 ; Search for the beginning of the string (this is not an optimal search
27 ; strategy [in fact, it's pretty dumb], but it's simple to implement).
29 sta tmp1 ; Save start of needle
30 @L1: lda (ptr1),y ; Get next char from haystack
31 beq @NotFound ; Jump if end
32 cmp tmp1 ; Start of needle found?
36 inc ptr1+1 ; Bump high byte
37 bne @L1 ; Branch always
39 ; We found the start of needle in haystack
44 sta ptr1 ; Make ptr1 point to start
48 ; ptr1 points to the start of needle now. Setup temporary pointers for the
49 ; search. The low byte of ptr4 is already set.
56 ldy #1 ; First char is identical, so start on second
60 @L4: lda (ptr4),y ; Get char from needle
61 beq @Found ; Jump if end of needle (-> found)
62 cmp (ptr3),y ; Compare with haystack
63 bne @L5 ; Jump if not equal
67 inc ptr4+1 ; Bump hi byte of pointers
68 bne @L4 ; Next char (branch always)
70 ; The strings did not compare equal, search next start of needle
72 @L5: ldy #1 ; Start after this char
73 bne @L1 ; Branch always
75 ; We found the start of needle
81 ; We reached end of haystack without finding needle
84 lda #$00 ; return NULL