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

  1. ;
  2. ; *** Listing 11-24 ***
  3. ;
  4. ; Determines whether two zero-terminated strings differ, and
  5. ; if so where, using REP SCASB to find the terminating zero
  6. ; to determine one string length, and then using REPZ CMPSW
  7. ; to compare the strings.
  8. ;
  9.     jmp    Skip
  10. ;
  11. TestString1    label    byte
  12.     db    'This is a test string that is '
  13.     db    'z'
  14.     db    'terminated with a zero byte...',0
  15. TestString2    label    byte
  16.     db    'This is a test string that is '
  17.     db    'a'
  18.     db    'terminated with a zero byte...',0
  19. ;
  20. ; Compares two zero-terminated strings.
  21. ;
  22. ; Input:
  23. ;    DS:SI = first zero-terminated string
  24. ;    ES:DI = second zero-terminated string
  25. ;
  26. ; Output:
  27. ;    DS:SI = pointer to first differing location in
  28. ;        first string, or 0 if the byte wasn't found
  29. ;    ES:DI = pointer to first differing location in
  30. ;        second string, or 0 if the byte wasn't found
  31. ;
  32. ; Registers altered: AL, CX, DX, SI, DI
  33. ;
  34. ; Direction flag cleared
  35. ;
  36. ; Note: Does not handle strings that are longer than 64K
  37. ;    bytes or cross segment boundaries.
  38. ;
  39. ; Note: If there is no terminating zero in the first 64K-1
  40. ;    bytes of a string, the string is treated as if byte
  41. ;    64K is a zero without checking, since if it isn't
  42. ;    the string isn't zero-terminated at all.
  43. ;
  44. CompareStrings:
  45.     mov    dx,di    ;set aside the start of the second
  46.             ; string
  47.     sub    al,al    ;we'll search for zero in the second
  48.             ; string to see how long it is
  49.     mov    cx,0ffffh ;long enough to handle any string
  50.             ; up to 64K-1 bytes in length. Any
  51.             ; longer string will be treated as
  52.             ; if byte 64K is zero
  53.     cld
  54.     repnz    scasb    ;find the terminating zero
  55.     not    cx    ;length of string in bytes, including
  56.             ; the terminating zero except in the
  57.             ; case of a string that's exactly 64K
  58.             ; long including the terminating zero
  59.     mov    di,dx    ;get back the start of the second
  60.             ; string
  61.     shr    cx,1    ;get count in words
  62.     jnc    CompareStringsWord
  63.             ;if there's no odd byte, go directly
  64.             ; to comparing a word at a time
  65.     cmpsb        ;compare the odd bytes of the
  66.             ; strings
  67.     jnz    CompareStringsDifferentByte
  68.             ;we've already found a difference
  69. CompareStringsWord:
  70.             ;there's no need to guard against
  71.             ; CX=0 here, since we know that if
  72.             ; CX=0 here, the preceding CMPSB
  73.             ; must have successfully compared
  74.             ; the terminating zero bytes of the
  75.             ; strings (which are the only bytes
  76.             ; of the strings), and the Zero flag
  77.             ; setting of 1 from CMPSB will be
  78.             ; preserved by REPZ CMPSW if CX=0,
  79.             ; resulting in the correct
  80.             ; conclusion that the strings are
  81.             ; identical
  82.     repz    cmpsw    ;compare the rest of the strings a
  83.             ; word at a time for speed
  84.     jnz    CompareStringsDifferent    ;they're not the same
  85.     sub    si,si    ;return 0 pointers indicating that
  86.     mov    di,si    ; the strings are identical
  87.     ret
  88. CompareStringsDifferent:
  89.             ;the strings are different, so we
  90.             ; have to figure which byte in the
  91.             ; word just compared was the first
  92.             ; difference
  93.     dec    si    ;point back to the second byte of
  94.     dec    di    ; the differing word in each string
  95.     dec    si    ;point back to the differing byte in
  96.     dec    di    ; each string
  97.     lodsb
  98.     scasb        ;compare that first byte again
  99.     jz    CompareStringsDone
  100.             ;if the first bytes are the same,
  101.             ; then it must have been the second
  102.             ; bytes that differed. That's where
  103.             ; we're pointing, so we're done
  104. CompareStringsDifferentByte:
  105.     dec    si    ;the first bytes differed, so point
  106.     dec    di    ; back to them
  107. CompareStringsDone:
  108.     ret
  109. ;
  110. Skip:
  111.     call    ZTimerOn
  112.     mov    si,offset TestString1 ;point to one string
  113.     mov    di,seg TestString2
  114.     mov    es,di
  115.     mov    di,offset TestString2 ;point to other string
  116.     call    CompareStrings    ;and compare the strings
  117.     call    ZTimerOff
  118.