Advertisement
Guest User

Untitled

a guest
Jan 13th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     XDEF _NeedleInHaystack
  2.     XREF _DOSBase
  3.  
  4.     SECTION CODE,CODE_P
  5.  
  6.     incdir  VINCLUDEOS3ASM:
  7.     include exec/libraries.i
  8.     include dos/dos.i
  9.     include dos/dos_lib.i
  10.  
  11.     include macros.i
  12.  
  13. DEBUG_ENABLE = 1
  14.  
  15. ;dos.library equates
  16. PutStr = -$3b4
  17. VPrintf = -$3ba
  18.  
  19. _NeedleInHaystack:
  20.     ;Haystack = a0
  21.     ;Needle   = a1
  22.     ;Returns 0 in d0 if found, -1 if not found
  23.  
  24.     ;preserve a2/d3
  25.     PUSHL   a2
  26.     PUSHL   d3
  27.  
  28.     ;Debugging.
  29.     IF DEBUG_ENABLE
  30.  
  31.     PUSHL   a0-a6
  32.     PUSHL   d0-d7
  33.  
  34.     ;Print Haystack
  35.     PUSHL   a1
  36.     move.l  _DOSBase,a6      ;Get dos.library addr
  37.     move.l  #dbg_Haystack,d1 ;d1 = format string
  38.     move.l  a0,dbg_str_ptr
  39.     move.l  #dbg_str_ptr,d2  ;d2 = ptr to argument list (in this case just a ptr to a string)
  40.     jsr     VPrintf(a6)
  41.  
  42.     ;Print Needle
  43.     POPL    a1
  44.     move.l  #dbg_Needle,d1
  45.     move.l  a1,dbg_str_ptr
  46.     move.l  #dbg_str_ptr,d2
  47.     jsr     VPrintf(a6)
  48.  
  49.     POPL    d0-d7
  50.     POPL   a0-a6
  51.  
  52.     ENDIF
  53.  
  54.     move.l  a1,a2 ;copy Needle into a2 
  55.  
  56.     move.b  #-1,d0
  57.     move.b  (a1)+,d2 ;d2 = current needle
  58.     jsr LowerNeedleChar
  59.  
  60. .outer:
  61.     move.b  (a0)+,d1 ;d1 = current haystack
  62.     cmp.b   #$0,d1   ;is this a terminator?
  63.     beq .failure ;fail if so
  64.     jsr LowerHaystackChar
  65.  
  66.     cmp.b   d2,d1       ;same, caps-insensitive?
  67.     beq .found      ;yes, advance Needle ptr
  68.     jsr ResetNeedle     ;no, reset to the start of needle
  69.     cmp.b   d2,d1       ;try again
  70.     beq .found      ;match the start of Needle
  71.     bra .outer      ;no match. go back to the outer loop
  72.  
  73. .found:
  74.     move.b  (a1)+,d2    ;advance Needle ptr
  75.     cmp #0,d2       ;is it a null?
  76.     beq .success    ;yes, we found the whole substring
  77.     bne .outer      ;no, still more to look for
  78.  
  79. .failure:   ;needle is not in haystack
  80.     move.b  #-1,d0
  81.     bra .done
  82.  
  83. .success:   ;needle is in haystack
  84.     move.b  #0,d0
  85.     bra .done
  86.  
  87. .done:
  88.    ;retrieve a2/d3
  89.  
  90.     IF DEBUG_ENABLE
  91.  
  92.     move.l  #dbg_Separator,d1
  93.     move.l  #0,d2
  94.     jsr     VPrintf(a6)
  95.  
  96.     ENDIF
  97.  
  98.     POPL    d3
  99.     POPL    a2
  100.     rts
  101.  
  102. ;----------------------
  103. ResetNeedle:    ;reset the Needle ptr and load d2 w/starting char
  104.     move.l  a2,a1
  105.     move.b  (a1)+,d2
  106.     rts
  107.  
  108. LowerHaystackChar:
  109.     cmp #$5A,d1 ;$5A = Z
  110.     bhi .abort
  111.     cmp #$41,d1 ;$41 = A
  112.     blo .abort
  113.  
  114.     ;d2 is in the range $41 to $5A
  115.     add.b   #$20,d1
  116.     rts
  117.  
  118. .abort:
  119.     rts
  120.  
  121. LowerNeedleChar:
  122.     cmp #$5A,d2
  123.     bhi .abort
  124.     cmp #$41,d2
  125.     blo .abort
  126.  
  127.     add.b   #$20,d1
  128.     rts
  129.  
  130. .abort:
  131.     rts
  132.  
  133.     SECTION DATA,DATA_P
  134. dbg_Haystack   dc.b    'Haystack: %s',10,0
  135. dbg_Needle     dc.b    'Needle  : %s',10,0
  136. dbg_Separator  dc.b    '********',10,0
  137.  
  138. dbg_str_ptr    dc.l    0
  139.  
  140.     END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement