home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / MOVMEM.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  3.5 KB  |  116 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - movmem.cas
  3.  *
  4.  * function(s)
  5.  *        movmem - move a block of bytes
  6.  *        memmov - move a block of bytes
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*[]------------------------------------------------------------[]*/
  10. /*|                                                              |*/
  11. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  12. /*|                                                              |*/
  13. /*|                                                              |*/
  14. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  15. /*|     All Rights Reserved.                                     |*/
  16. /*|                                                              |*/
  17. /*[]------------------------------------------------------------[]*/
  18.  
  19.  
  20. #pragma inline
  21. #include <asmrules.h>
  22. #include <mem.h>
  23.  
  24. /*-----------------------------------------------------------------------*
  25.  
  26. Name            movmem - move a block of bytes
  27.  
  28. Usage           void movmem(void *src, void *dst, unsigned len);
  29.  
  30. Prototype in    mem.h
  31.  
  32. Description     movmem copies a block of len  bytes from src to dst. If the
  33.                 source and  destination arrays overlap, the  copy direction
  34.                 is chosen so that the data is always copied correctly.
  35.  
  36. Return value    There is no return value
  37.  
  38. *------------------------------------------------------------------------*/
  39. void movmem(void *src, void *dst, unsigned len)
  40. {
  41. #if     LDATA
  42. #if     !defined(__HUGE__)
  43.         asm     push    ds
  44. #endif
  45.         if      (((void huge *)src) < ((void huge *)dst))
  46. #else
  47.         _ES = _DS;
  48.         if      (src < dst)
  49. #endif
  50.         {       /* travel backward, need to adjust ptrs later */
  51.                 asm     std
  52.                 asm     mov     ax, 1
  53.         }
  54.         else
  55.         {       /* travel forward, no need to adjust ptrs */
  56.                 asm     cld
  57.                 asm     xor     ax, ax
  58.         }
  59.  
  60. asm     LDS_    si, src
  61. asm     LES_    di, dst
  62. asm     mov     cx, len
  63.  
  64. asm     or      ax, ax
  65. asm     jz      movit
  66. asm     add     si, cx  /* backward move, adjust ptrs to end-1 */
  67. asm     dec     si
  68. asm     add     di, cx
  69. asm     dec     di
  70. movit:
  71. asm     test    di, 1
  72. asm     jz      isAligned
  73. asm     jcxz    done
  74. asm     movsb
  75. asm     dec     cx
  76. isAligned:
  77. asm     sub     si, ax  /* compensate for word moves */
  78. asm     sub     di, ax
  79. asm     shr     cx, 1
  80. asm     rep     movsw
  81. asm     jnc     noOdd
  82. asm     add     si, ax  /* compensate for final byte */
  83. asm     add     di, ax
  84. asm     movsb
  85. noOdd:
  86. done:
  87. asm     cld
  88.  
  89. #if     defined(__LARGE__) ||  defined(__COMPACT__)
  90.         asm     pop     ds
  91. #endif
  92. }
  93.  
  94.  
  95. /*-----------------------------------------------------------------------*
  96.  
  97. Name            memmove - move a block of bytes
  98.  
  99. Usage           void memmove(void *dst, const void *scr, unsigned len);
  100.  
  101. Prototype in    mem.h
  102.  
  103. Description     memmove copies a block of len  bytes from src to dst. If the
  104.                 source and  destination arrays overlap, the  copy direction
  105.                 is chosen so that the data is always copied correctly.
  106.  
  107. Return value    dst
  108.  
  109. *------------------------------------------------------------------------*/
  110. void *_CType memmove( void *dst, const void *src, size_t n )
  111.   {
  112.   movmem( (void *)src, dst, n );
  113.   return( dst );
  114.   }
  115.  
  116.