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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - memccpy.cas
  3.  *
  4.  * function(s)
  5.  *        memccpy - copy bytes from src to dst
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #pragma inline
  18. #include <asmrules.h>
  19. #include <mem.h>
  20.  
  21. /*-----------------------------------------------------------------------*
  22.  
  23. Name            memccpy - copy bytes from src to dst
  24.  
  25. Usage           void *memccpy(void *dst, const void *src,
  26.                               int val, size_t n);
  27.  
  28. Prototype in    string.h & mem.h
  29.  
  30. Description     memccpy copies characters  from src to dst, until  either n
  31.                 characters are  moved or the  character val is  matched and
  32.                 copied,  without checks  and as  fast as  possible. If  the
  33.                 source  and  target  arrays  overlap,  the  effect  is  not
  34.                 defined.
  35.  
  36. Return value    If val  was matched then the  return value is a  pointer to
  37.                 the character  position following val in  dst, otherwise it
  38.                 is NULL.
  39.  
  40. *------------------------------------------------------------------------*/
  41. #if defined(__FARFUNCS__)
  42. #include <_farfunc.h>
  43. #endif
  44.  
  45. void * _FARFUNC memccpy(void *dst, const void *src, int val, size_t n)
  46. {
  47. #if !(LDATA)
  48.         _ES = _DS;
  49. #endif
  50.  
  51. asm     LES_    di, src         /* first check for any occurrence of val */
  52. asm     mov     cx, n
  53. asm     jcxz    ccp_NULL
  54. asm     mov     bx, cx
  55. asm     mov     al, val
  56. asm     cld
  57. asm     repne   scasb
  58.  
  59.         pushDS_
  60. asm     sub     bx, cx          /* span includes ch if it was seen */
  61. asm     mov     cx, bx          /* CX = span to be copied */
  62. asm     LES_    di, dst
  63. asm     LDS_    si, src
  64. asm     shr     cx, 1
  65. asm     rep     movsw
  66. asm     jnc     ccp_end
  67. asm     movsb
  68. ccp_end:
  69.         popDS_
  70.  
  71. #if (LDATA)
  72. asm     mov     dx, es
  73. #endif
  74. asm     cmp     al, ES_ [di-1]  /* was a ch match found ? */
  75. asm     mov     ax,di
  76. asm     je      ccp_exit
  77.  
  78. ccp_NULL:
  79. asm     xor     ax, ax
  80. #if (LDATA)
  81. asm     cwd
  82. #endif
  83.  
  84. ccp_exit:
  85. #if (LDATA)
  86.         return( (void *)(MK_LONG) );
  87. #else
  88.         return( (void *)_AX );
  89. #endif
  90. }
  91.  
  92.