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

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