Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XDEF _NeedleInHaystack
- XREF _DOSBase
- SECTION CODE,CODE_P
- incdir VINCLUDEOS3ASM:
- include exec/libraries.i
- include dos/dos.i
- include dos/dos_lib.i
- include macros.i
- DEBUG_ENABLE = 1
- ;dos.library equates
- PutStr = -$3b4
- VPrintf = -$3ba
- _NeedleInHaystack:
- ;Haystack = a0
- ;Needle = a1
- ;Returns 0 in d0 if found, -1 if not found
- ;preserve a2/d3
- PUSHL a2
- PUSHL d3
- ;Debugging.
- IF DEBUG_ENABLE
- PUSHL a0-a6
- PUSHL d0-d7
- ;Print Haystack
- PUSHL a1
- move.l _DOSBase,a6 ;Get dos.library addr
- move.l #dbg_Haystack,d1 ;d1 = format string
- move.l a0,dbg_str_ptr
- move.l #dbg_str_ptr,d2 ;d2 = ptr to argument list (in this case just a ptr to a string)
- jsr VPrintf(a6)
- ;Print Needle
- POPL a1
- move.l #dbg_Needle,d1
- move.l a1,dbg_str_ptr
- move.l #dbg_str_ptr,d2
- jsr VPrintf(a6)
- POPL d0-d7
- POPL a0-a6
- ENDIF
- move.l a1,a2 ;copy Needle into a2
- move.b #-1,d0
- move.b (a1)+,d2 ;d2 = current needle
- jsr LowerNeedleChar
- .outer:
- move.b (a0)+,d1 ;d1 = current haystack
- cmp.b #$0,d1 ;is this a terminator?
- beq .failure ;fail if so
- jsr LowerHaystackChar
- cmp.b d2,d1 ;same, caps-insensitive?
- beq .found ;yes, advance Needle ptr
- jsr ResetNeedle ;no, reset to the start of needle
- cmp.b d2,d1 ;try again
- beq .found ;match the start of Needle
- bra .outer ;no match. go back to the outer loop
- .found:
- move.b (a1)+,d2 ;advance Needle ptr
- cmp #0,d2 ;is it a null?
- beq .success ;yes, we found the whole substring
- bne .outer ;no, still more to look for
- .failure: ;needle is not in haystack
- move.b #-1,d0
- bra .done
- .success: ;needle is in haystack
- move.b #0,d0
- bra .done
- .done:
- ;retrieve a2/d3
- IF DEBUG_ENABLE
- move.l #dbg_Separator,d1
- move.l #0,d2
- jsr VPrintf(a6)
- ENDIF
- POPL d3
- POPL a2
- rts
- ;----------------------
- ResetNeedle: ;reset the Needle ptr and load d2 w/starting char
- move.l a2,a1
- move.b (a1)+,d2
- rts
- LowerHaystackChar:
- cmp #$5A,d1 ;$5A = Z
- bhi .abort
- cmp #$41,d1 ;$41 = A
- blo .abort
- ;d2 is in the range $41 to $5A
- add.b #$20,d1
- rts
- .abort:
- rts
- LowerNeedleChar:
- cmp #$5A,d2
- bhi .abort
- cmp #$41,d2
- blo .abort
- add.b #$20,d1
- rts
- .abort:
- rts
- SECTION DATA,DATA_P
- dbg_Haystack dc.b 'Haystack: %s',10,0
- dbg_Needle dc.b 'Needle : %s',10,0
- dbg_Separator dc.b '********',10,0
- dbg_str_ptr dc.l 0
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement