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

  1. ;
  2. ; *** Listing 11-25 ***
  3. ;
  4. ; Determines whether two zero-terminated strings differ, and
  5. ; if so where, using LODS/SCAS.
  6. ;
  7.     jmp    Skip
  8. ;
  9. TestString1    label    byte
  10.     db    'This is a test string that is '
  11.     db    'z'
  12.     db    'terminated with a zero byte...',0
  13. TestString2    label    byte
  14.     db    'This is a test string that is '
  15.     db    'a'
  16.     db    'terminated with a zero byte...',0
  17. ;
  18. ; Compares two zero-terminated strings.
  19. ;
  20. ; Input:
  21. ;    DS:SI = first zero-terminated string
  22. ;    ES:DI = second zero-terminated string
  23. ;
  24. ; Output:
  25. ;    DS:SI = pointer to first differing location in
  26. ;        first string, or 0 if the byte wasn't found
  27. ;    ES:DI = pointer to first differing location in
  28. ;        second string, or 0 if the byte wasn't found
  29. ;
  30. ; Registers altered: AX, SI, DI
  31. ;
  32. ; Direction flag cleared
  33. ;
  34. ; Note: Does not handle strings that are longer than 64K
  35. ;    bytes or cross segment boundaries.
  36. ;
  37. CompareStrings:
  38.     cld
  39. CompareStringsLoop:
  40.     lodsw        ;get the next 2 bytes
  41.     and    al,al    ;is the first byte the terminating
  42.             ; zero?
  43.     jz    CompareStringsFinalByte
  44.             ;yes, so there's only one byte left
  45.             ; to check
  46.     scasw        ;compare this word
  47.     jnz    CompareStringsDifferent ;the strings differ
  48.     and    ah,ah    ;is the second byte the terminating
  49.             ; zero?
  50.     jnz    CompareStringsLoop ;no, continue comparing
  51.             ;the strings are the same
  52. CompareStringsSame:
  53.     sub    si,si    ;return 0 pointers indicating that
  54.     mov    di,si    ; the strings are identical
  55.     ret
  56. CompareStringsFinalByte:
  57.     scasb        ;does the terminating zero match in
  58.             ; the 2 strings?
  59.     jz    CompareStringsSame ;yes, the strings match
  60.     dec    si    ;point back to the differing byte
  61.     dec    di    ; in each string
  62.     ret
  63. CompareStringsDifferent:
  64.             ;the strings are different, so we
  65.             ; have to figure which byte in the
  66.             ; word just compared was the first
  67.             ; difference
  68.     dec    si
  69.     dec    si    ;point back to the first byte of the
  70.     dec    di    ; differing word in each string
  71.     dec    di
  72.     lodsb
  73.     scasb        ;compare that first byte again
  74.     jz    CompareStringsDone
  75.             ;if the first bytes are the same,
  76.             ; then it must have been the second
  77.             ; bytes that differed. That's where
  78.             ; we're pointing, so we're done
  79.     dec    si    ;the first bytes differed, so point
  80.     dec    di    ; back to them
  81. CompareStringsDone:
  82.     ret
  83. ;
  84. Skip:
  85.     call    ZTimerOn
  86.     mov    si,offset TestString1 ;point to one string
  87.     mov    di,seg TestString2
  88.     mov    es,di
  89.     mov    di,offset TestString2 ;point to other string
  90.     call    CompareStrings    ;and compare the strings
  91.     call    ZTimerOff
  92.