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

  1. ;
  2. ; *** Listing 11-13 ***
  3. ;
  4. ; Finds the first occurrence of the letter 'z' in
  5. ; a zero-terminated string, using non-string instructions.
  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: AH, SI
  26. ;
  27. ; Note: Do not pass a string that starts at offset 0 (SI=0),
  28. ;    since a match on the first byte and failure to find
  29. ;    the byte would be indistinguishable.
  30. ;
  31. ; Note: Does not handle strings that are longer than 64K
  32. ;    bytes or cross segment boundaries.
  33. ;
  34. FindCharInString:
  35. FindCharInStringLoop:
  36.     mov    ah,[si]    ;get the next string byte
  37.     cmp    ah,al    ;is this the byte we're
  38.             ; looking for?
  39.     jz    FindCharInStringDone
  40.             ;yes, so we're done
  41.     inc    si    ;point to the following byte
  42.     and    ah,ah    ;is this the terminating zero?
  43.     jnz    FindCharInStringLoop
  44.             ;no, so check the next byte
  45.     sub    si,si    ;we didn't find a match, so return
  46.             ; 0 in SI
  47. FindCharInStringDone:
  48.     ret
  49. ;
  50. Skip:
  51.     call    ZTimerOn
  52.     mov    al,'z'        ;byte value to find
  53.     mov    si,offset TestString
  54.                 ;string to search
  55.     call    FindCharInString ;search for the byte
  56.     call    ZTimerOff
  57.