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

  1. ;
  2. ; *** Listing 11-7 ***
  3. ;
  4. ; Copies overlapping blocks of memory with MOVS.
  5. ; To the greatest possible extent, the copy is
  6. ; performed a word at a time.
  7. ;
  8.     jmp    Skip
  9. ;
  10. TEST_LENGTH1    equ    501    ;sample copy length #1
  11. TEST_LENGTH2    equ    1499    ;sample copy length #2
  12. TestArray    db    1500 dup (0)
  13. ;
  14. ; Copies a block of memory CX bytes in length. A value
  15. ; of 0 means "copy zero bytes," since it wouldn't make
  16. ; much sense to copy one 64K block to another 64K block
  17. ; in the same segment, so the maximum length that can
  18. ; be copied is 64K-1 bytes and the minimum length
  19. ; is 0 bytes. Note that both blocks must be in DS. Note
  20. ; also that overlap handling is not guaranteed if either
  21. ; block wraps at the end of the segment.
  22. ;
  23. ; Input:
  24. ;    CX = number of bytes to clear
  25. ;    DS:SI = start of block to copy
  26. ;    DS:DI = start of destination block
  27. ;
  28. ; Output:
  29. ;    none
  30. ;
  31. ; Registers altered: CX, DX, SI, DI, ES
  32. ;
  33. ; Direction flag cleared
  34. ;
  35. BlockCopyWithOverlap:
  36.     mov    dx,ds    ;source and destination are in the
  37.     mov    es,dx    ; same segment
  38.     cmp    si,di    ;which way do the blocks overlap, if
  39.             ; they do overlap?
  40.     jae    LowToHigh
  41.             ;source is not below destination, so
  42.             ; we can copy from low to high
  43.  
  44.             ;source is below destination, so we
  45.             ; must copy from high to low
  46.     add    si,cx    ;point to the end of the source
  47.     dec    si    ; block
  48.     add    di,cx    ;point to the end of the destination
  49.     dec    di    ; block
  50.     std        ;copy from high addresses to low
  51.     shr    cx,1    ;divide by 2, copying the odd-byte
  52.             ; status to the Carry flag
  53.     jnc    CopyWordHighToLow ;no odd byte to copy
  54.     movsb        ;copy the odd byte
  55. CopyWordHighToLow:
  56.     dec    si    ;point one word lower in memory, not
  57.     dec    di    ; one byte
  58.     rep    movsw    ;move the rest of the block
  59.     cld
  60.     ret
  61. ;
  62. LowToHigh:
  63.     cld        ;copy from low addresses to high
  64.     shr    cx,1    ;divide by 2, copying the odd-byte
  65.             ; status to the Carry flag
  66.     jnc    CopyWordLowToHigh    ;no odd byte to copy
  67.     movsb        ;copy the odd byte
  68. CopyWordLowToHigh:
  69.     rep    movsw    ;move the rest of the block
  70.     ret
  71. ;
  72. Skip:
  73.     call    ZTimerOn
  74. ;
  75. ; First run the case where the destination overlaps & is
  76. ; higher in memory.
  77. ;
  78.     mov    si,offset TestArray
  79.     mov    di,offset TestArray+1
  80.     mov    cx,TEST_LENGTH1
  81.     call    BlockCopyWithOverlap
  82. ;
  83. ; Now run the case where the destination overlaps & is
  84. ; lower in memory.
  85. ;
  86.     mov    si,offset TestArray+1
  87.     mov    di,offset TestArray
  88.     mov    cx,TEST_LENGTH2
  89.     call    BlockCopyWithOverlap
  90.     call    ZTimerOff
  91.