Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright-free! For any use!
- KickAssembler Substring Matching Subroutine.
- Finds the index of the first occurence of a substring (can be any encoding, just make sure both the
- string and substring are the same encoding!)
- Both strings MUST have a final NULL ($00 byte) character.
- Store the address of the string to search at $fb
- Store the address of the substring at $fd
- $c000 - $c002 are used as temp
- A & Y registers will be used.
- A <- boolean (0 or 1) on whether a match was found
- X <- index of the first occurence of the substring (if found, 0 if not found
- Y <- 0
- __$fb - $fc, $c000 - $c002 and Y are set to zero!__
- */
- find_substr: {
- .label stri = $3b
- .label subi = $3c
- .label finalindex = $2a
- .label stradd = $fb
- .label subadd = $fd
- lda #0 // index of the searched string
- sta stri
- lda #0 // index of the substring
- sta subi
- mainloop:
- ldy stri
- lda (stradd),y
- ldy subi
- cmp (subadd),y
- beq letter_match
- bne letter_mismatch
- letter_match:
- // letter matches, increment substring index
- inc subi
- jmp check
- letter_mismatch:
- inc finalindex
- lda #0
- sta subi
- check:
- ldy subi // check substring first, to prevent substring matches at the end of the
- // searched string from coming back false
- lda (subadd),y
- beq endsubreached // return true
- ldy stri
- lda (stradd),y
- beq endstrreached // return false
- inc stri
- jmp mainloop
- endsubreached:
- lda #1 // substring found
- ldx finalindex
- jmp end
- endstrreached:
- lda #0
- ldx #0
- jmp end
- end:
- ldy #0
- sty stradd
- sty subadd
- sty finalindex
- sty stri
- sty subi
- rts
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement