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

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