home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / zendisk2.zip / LST11-30.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  4KB  |  128 lines

  1. ;
  2. ; *** Listing 11-30 ***
  3. ;
  4. ; Searches a text buffer for a sequence of bytes by checking
  5. ; for the sequence with non-string instructions starting at
  6. ; each byte of the buffer that potentially could start the
  7. ; sequence.
  8. ;
  9.     jmp    Skip
  10. ;
  11. ; Text buffer that we'll search.
  12. ;
  13. TextBuffer    label    byte
  14.     db    'This is a sample text buffer, suitable '
  15.     db    'for a searching text of any sort... '
  16.     db    'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '
  17.     db    'End of text... '
  18. TEXT_BUFFER_LENGTH    equ    ($-TextBuffer)
  19. ;
  20. ; Sequence of bytes that we'll search for.
  21. ;
  22. SearchSequence    label    byte
  23.     db    'text...'
  24. SEARCH_SEQUENCE_LENGTH    equ    ($-SearchSequence)
  25. ;
  26. ; Searches a buffer for the first occurrence of a specified
  27. ; sequence of bytes.
  28. ;
  29. ; Input:
  30. ;    CX = length of sequence of bytes to search for
  31. ;    DX = length of buffer to search in
  32. ;    DS:SI = start of sequence of bytes to search for
  33. ;    ES:DI = start of buffer to search
  34. ;
  35. ; Output:
  36. ;    ES:DI = pointer to start of first occurrence of
  37. ;        desired sequence of bytes in the buffer, or
  38. ;        0:0 if the sequence wasn't found
  39. ;
  40. ; Registers altered: AX, BX, CX, DX, SI, DI, BP
  41. ;
  42. ; Note: Does not handle search sequences or text buffers
  43. ;    that are longer than 64K bytes or cross segment
  44. ;    boundaries.
  45. ;
  46. ; Note: Assumes non-zero length of search sequence (CX > 0),
  47. ;    and search sequence shorter than 64K (CX <= 0ffffh).
  48. ;
  49. ; Note: Assumes buffer is longer than search sequence
  50. ;    (DX > CX). Zero length of buffer is taken to mean
  51. ;    that the buffer is 64K bytes long.
  52. ;
  53. FindSequence:
  54.     mov    bp,si    ;set aside the sequence start
  55.             ; offset
  56.     mov    bx,cx    ;set aside the sequence length
  57.     sub    dx,cx    ;difference between buffer and
  58.             ; search sequence lengths
  59.     inc    dx    ;# of possible sequence start bytes
  60.             ; to check in the buffer
  61. FindSequenceLoop:
  62.     push    di    ;remember the address of the current
  63.             ; byte in case it's needed
  64.     mov    cx,bx    ;sequence length
  65.     shr    cx,1    ;convert to word for faster search
  66.     jnc    FindSequenceWord ;do word search if no odd
  67.                 ; byte
  68.     mov    al,[si]
  69.     cmp    es:[di],al    ;compare the odd byte
  70.     jnz    FindSequenceNoMatch ;odd byte doesn't match,
  71.                 ; so we havent' found the
  72.                 ; search sequence here
  73.     inc    si        ;odd byte matches, so point
  74.     inc    di        ; to the next byte in the
  75.                 ; buffer and sequence
  76. FindSequenceWord:
  77.     jcxz    FindSequenceFound
  78.                 ;since we're guaranteed to
  79.                 ; have a non-zero length,
  80.                 ; the sequence must be 1
  81.                 ; byte long and we've
  82.                 ; already found that it
  83.                 ; matched
  84. FindSequenceWordCompareLoop:
  85.     mov    ax,[si]        ;compare the remainder of
  86.     cmp    es:[di],ax    ; the search sequence to
  87.     jnz    FindSequenceNoMatch ; this part of the
  88.     inc    si        ; buffer a word at a time
  89.     inc    si        ; for speed
  90.     inc    di
  91.     inc    di
  92.     loop    FindSequenceWordCompareLoop
  93. FindSequenceFound:    ;it's a match
  94.     pop    di    ;point to the buffer location at
  95.             ; which the first occurrence of the
  96.             ; sequence was found (remember that
  97.             ; earlier we pushed the address of
  98.             ; the potential sequence start)
  99.     ret
  100. FindSequenceNoMatch:
  101.     pop    di    ;get back the pointer to the current
  102.             ; byte
  103.     inc    di    ;point to the next buffer start
  104.             ; search location
  105.     mov    si,bp    ;point to the start of the search
  106.             ; sequence again
  107.     dec    dx    ;count down the remaining bytes to
  108.             ; search in the buffer
  109.     jnz    FindSequenceLoop
  110.     sub    di,di    ;return 0 pointer indicating that
  111.     mov    es,di    ; the sequence was not found
  112.     ret
  113. ;
  114. Skip:
  115.     call    ZTimerOn
  116.     mov    si,offset SearchSequence
  117.                 ;point to search sequence
  118.     mov    cx,SEARCH_SEQUENCE_LENGTH
  119.                 ;length of search sequence
  120.     mov    di,seg TextBuffer
  121.     mov    es,di
  122.     mov    di,offset TextBuffer
  123.                 ;point to buffer to search
  124.     mov    dx,TEXT_BUFFER_LENGTH
  125.                 ;length of buffer to search
  126.     call    FindSequence    ;search for the sequence
  127.     call    ZTimerOff
  128.