home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / MOVMEM.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  3.1 KB  |  118 lines

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