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

  1. ;
  2. ; *** Listing 14-15 ***
  3. ;
  4. ; For comparison with the in-line-code-branched-to-via-a-
  5. ; jump-table approach of Listing 14-14, this is a loop-based
  6. ; string-search routine that searches at most the specified
  7. ; number of bytes of a zero-terminated string for the
  8. ; specified character.
  9. ;
  10.     jmp    Skip
  11. TestString    label    byte
  12.     db    'This is a string containing the letter '
  13.     db    'z but not containing capital q', 0
  14. ;
  15. ; Searches a zero-terminated string for a character.
  16. ; Searches until a match is found, the terminating zero
  17. ; is found, or the specified number of characters have been
  18. ; checked.
  19. ;
  20. ; Input:
  21. ;    AL = character to search for
  22. ;    BX = maximum # of characters to search
  23. ;    DS:SI = string to search
  24. ;
  25. ; Output:
  26. ;    SI = pointer to character, or 0 if character not
  27. ;        found
  28. ;
  29. ; Registers altered: AX, CX, SI
  30. ;
  31. ; Direction flag cleared
  32. ;
  33. ; Note: Don't pass a string starting at offset 0, since a
  34. ;    match there couldn't be distinguished from a failure
  35. ;    to match.
  36. ;
  37. SearchNBytes    proc    near
  38.     mov    ah,al        ;we'll need AL for LODSB
  39.     mov    cx,bx        ;for LOOP
  40. SearchNBytesLoop:
  41.     lodsb
  42.     and    al,al
  43.     jz    NoMatch        ;terminating 0, so no match
  44.     cmp    ah,al
  45.     jz    MatchFound    ;match, so we're done
  46.     loop    SearchNBytesLoop
  47. ;
  48. ; No match was found.
  49. ;
  50. NoMatch:
  51.     sub    si,si    ;return no-match status
  52.     ret
  53. ;
  54. ; A match was found.
  55. ;
  56. MatchFound:
  57.     dec    si        ;point back to matching
  58.                 ; location
  59.     ret
  60. SearchNBytes    endp
  61. ;
  62. Skip:
  63.     call    ZTimerOn
  64.     mov    al,'Q'
  65.     mov    bx,20            ;search up to the
  66.     mov    si,offset TestString    ; first 20 bytes of
  67.     call    SearchNBytes        ; TestString for 'Q'
  68.     mov    al,'z'
  69.     mov    bx,80            ;search up to the
  70.     mov    si,offset TestString    ; first 80 bytes of
  71.     call    SearchNBytes        ; TestString for 'z'
  72.     mov    al,'a'
  73.     mov    bx,10            ;search up to the
  74.     mov    si,offset TestString    ; first 10 bytes of
  75.     call    SearchNBytes        ; TestString for 'a'
  76.     call    ZTimerOff
  77.