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

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