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

  1. ;
  2. ; *** Listing 13-10 ***
  3. ;
  4. ; Finds the first occurrence of the letter 'z' in
  5. ; a zero-terminated string, with a less-than-ideal
  6. ; conditional jump followed by an unconditional jump at
  7. ; the end of the loop.
  8. ;
  9.     jmp    Skip
  10. ;
  11. TestString    label    byte
  12.     db    'This is a test string that is '
  13.     db    'z'
  14.     db    'terminated with a zero byte...',0
  15. ;
  16. ; Finds the first occurrence of the specified byte in the
  17. ; specified zero-terminated string.
  18. ;
  19. ; Input:
  20. ;    AL = byte to find
  21. ;    DS:SI = zero-terminated string to search
  22. ;
  23. ; Output:
  24. ;    SI = pointer to first occurrence of byte in string,
  25. ;        or 0 if the byte wasn't found
  26. ;
  27. ; Registers altered: AX, SI
  28. ;
  29. ; Direction flag cleared
  30. ;
  31. ; Note: Do not pass a string that starts at offset 0 (SI=0),
  32. ;    since a match on the first byte and failure to find
  33. ;    the byte would be indistinguishable.
  34. ;
  35. ; Note: Does not handle strings that are longer than 64K
  36. ;    bytes or cross segment boundaries.
  37. ;
  38. FindCharInString:
  39.     mov    ah,al    ;we'll need AL since that's the
  40.             ; only register LODSB can use
  41.     cld
  42. FindCharInStringLoop:
  43.     lodsb        ;get the next string byte
  44.     cmp    al,ah    ;is this the byte we're
  45.             ; looking for?
  46.     jz    FindCharInStringFound
  47.             ;yes, so we're done with a match
  48.     and    al,al    ;is this the terminating zero?
  49.     jz    FindCharInStringNotFound
  50.             ;yes, so we're done with no match
  51.     jmp    FindCharInStringLoop
  52.             ;check the next byte
  53. FindCharInStringFound:
  54.     dec    si    ;point back to the matching byte
  55.     ret
  56. FindCharInStringNotFound:
  57.     sub    si,si    ;we didn't find a match, so return
  58.             ; 0 in SI
  59.     ret
  60. ;
  61. Skip:
  62.     call    ZTimerOn
  63.     mov    al,'z'        ;byte value to find
  64.     mov    si,offset TestString
  65.                 ;string to search
  66.     call    FindCharInString ;search for the byte
  67.     call    ZTimerOff
  68.