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

  1. ;
  2. ; *** Listing 11-31 ***
  3. ;
  4. ; Compares two arrays of 16-bit signed values in order to
  5. ; find the first point at which the arrays cross, using
  6. ; non-repeated CMPSW.
  7. ;
  8.     jmp    Skip
  9. ;
  10. ; The two arrays that we'll compare.
  11. ;
  12. ARRAY_LENGTH    equ    200
  13. ;
  14. Array1    label    byte
  15. TEMP=-100
  16.     rept    ARRAY_LENGTH
  17.     dw    TEMP
  18. TEMP=TEMP+1
  19.     endm
  20. ;
  21. Array2    label    byte
  22. TEMP=100
  23.     rept    ARRAY_LENGTH
  24.     dw    TEMP
  25. TEMP=TEMP-1
  26.     endm
  27. ;
  28. ; Compares two buffers to find the first point at which they
  29. ; cross. Points at which the arrays become equal are
  30. ; considered to be crossing points.
  31. ;
  32. ; Input:
  33. ;    CX = length of arrays in words (they must be of
  34. ;        equal length)
  35. ;    DS:SI = start of first array
  36. ;    ES:DI = start of second array
  37. ;
  38. ; Output:
  39. ;    DS:SI = pointer to crossing point in first array,
  40. ;        or SI=0 if there is no crossing point
  41. ;    ES:DI = pointer to crossing point in second array,
  42. ;        or DI=0 if there is no crossing point
  43. ;
  44. ; Registers altered: AX, CX, SI, DI
  45. ;
  46. ; Direction flag cleared
  47. ;
  48. ; Note: Does not handle arrays that are longer than 64K
  49. ;    bytes or cross segment boundaries.
  50. ;
  51. FindCrossing:
  52.     cld
  53.     jcxz    FindCrossingNotFound
  54.             ;if there's nothing to compare, we
  55.             ; certainly can't find a crossing
  56.     mov    ax,[si]    ;compare the first two points to
  57.     cmp    ax,es:[di] ; make sure that the first array
  58.             ; doesn't start out below the second
  59.             ; array
  60.     pushf        ;remember the original relationship
  61.             ; of the arrays, so we can put the
  62.             ; pointers back at the end (can't
  63.             ; use LAHF because it doesn't save
  64.             ; the Overflow flag)
  65.     jnl    FindCrossingLoop ;the first array is above
  66.                 ; the second array
  67.     xchg    si,di    ;swap the array pointers so that
  68.             ; SI points to the initially-
  69.             ; greater array
  70. FindCrossingLoop:
  71.     cmpsw        ;compare the next element in each
  72.             ; array
  73.     jng    FindCrossingFound ;if SI doesn't point to a
  74.                 ; greater value, we've found
  75.                 ; the first crossing
  76.     loop    FindCrossingLoop ;check the next element in
  77.                 ; each array
  78. FindCrossingNotFound:
  79.     popf        ;clear the flags we pushed earlier
  80.     sub    si,si    ;return 0 pointers to indicate that
  81.     mov    di,si    ; no crossing was found
  82.     ret
  83. FindCrossingFound:
  84.     dec    si
  85.     dec    si    ;point back to the crossing point
  86.     dec    di    ; in each array
  87.     dec    di
  88.     popf        ;get back the original relationship
  89.             ; of the arrays
  90.     jnl    FindCrossingDone
  91.             ;SI pointed to the initially-
  92.             ; greater array, so we're all set
  93.     xchg    si,di    ;SI pointed to the initially-
  94.             ; less array, so swap SI and DI to
  95.             ; undo our earlier swap
  96. FindCrossingDone:
  97.     ret
  98. ;
  99. Skip:
  100.     call    ZTimerOn
  101.     mov    si,offset Array1 ;point to first array
  102.     mov    di,seg Array2
  103.     mov    es,di
  104.     mov    di,offset Array2 ;point to second array
  105.     mov    cx,ARRAY_LENGTH    ;length to compare
  106.     call    FindCrossing    ;find the first crossing, if
  107.                 ; any
  108.     call    ZTimerOff
  109.