memcpy:
/*-----------------------------------------------------------------------* * filename -
memcpy.cas * * function(s) *
memcpy - copy a block
of n bytes from src to dst
*-----------------------------------------------------------------------*/ #pragma inline #include <asmrules.h> #include <mem.h>
#undef memcpy
/* not an intrinsic */
/*-----------------------------------------------------------------------*
Name
memcpy - copy
a block of n bytes from src
to dst
Usage
void *memcpy(void *dst,
const void *src, size_t n);
Prototype in
mem.h & string.h
Description
memcpy copies a block of n
bytes from src to dst.
No
overlap checking is performed.
Return value
memcpy returns dst
*------------------------------------------------------------------------*/
#if defined(__FARFUNCS__) #include <_farfunc.h> #endif
#if 1
/* No overlap
checking version */
void * _FARFUNC memcpy(void
*dst, const void *src, size_t n) { #if !(LDATA)
_ES = _DS; #endif #if
defined(__LARGE__) || defined(__COMPACT__) asm mov
dx,ds
/* save ds */ #endif asm LES_
di, dst asm LDS_
si, src asm mov
cx,n asm shr
cx,1 asm cld asm rep
movsw asm jnc
cpy_end asm movsb cpy_end: #if
defined(__LARGE__) || defined(__COMPACT__)
asm mov
ds,dx
/* restore */ #endif
return(dst); }
#else
/* Overlap checking
version */
void * _FARFUNC memcpy(void
*dst, const void *src, size_t n) {
movmem(src,dst,n);
return(dst); } #endif
memmove:
/*-----------------------------------------------------------------------* * filename -
movmem.cas * * function(s) *
movmem - move a block
of bytes *
memmove - move a block
of bytes
*-----------------------------------------------------------------------*/ #pragma inline #include <asmrules.h> #include <mem.h>
/*-----------------------------------------------------------------------*
Name
movmem - move
a block of bytes
Usage
void movmem(void *src,
void *dst, unsigned len);
Prototype in
mem.h
Description
movmem copies a block of len
bytes from src to dst. If the
source
and destination arrays overlap, the
copy direction
is
chosen so that the data is always
copied correctly.
Return value
There is no return value
*------------------------------------------------------------------------*/ #if defined(__FARFUNCS__) #include <_farfunc.h> #endif
void _FARFUNC movmem(const
void *src, void *dst, unsigned len) { #if LDATA #if
!defined(__HUGE__)
asm push
ds #endif
if (((void huge
*)src) < ((void huge *)dst)) #else
_ES = _DS;
if (src < dst) #endif
{ /* travel
backward, need to adjust ptrs later
*/
asm
std
asm
mov ax,
1
}
else
{ /* travel
forward, no need to adjust ptrs
*/
asm
cld
asm
xor ax,
ax
}
asm LDS_
si, src asm LES_
di, dst asm mov
cx, len
asm or
ax, ax asm jz
movit asm add
si, cx /*
backward move, adjust ptrs to end-1
*/ asm dec
si asm add
di, cx asm dec
di movit: asm test
di, 1 asm jz
isAligned asm jcxz
done asm movsb asm dec
cx isAligned: asm sub
si, ax /*
compensate for word moves */ asm sub
di, ax asm shr
cx, 1 asm rep
movsw asm jnc
noOdd asm add
si, ax /*
compensate for final byte */ asm add
di, ax asm movsb noOdd: done: asm cld
#if
defined(__LARGE__) || defined(__COMPACT__)
asm pop
ds #endif }
/*-----------------------------------------------------------------------*
Name
memmove - move
a block of bytes
Usage
void memmove(void *dst,
const void *scr, unsigned len);
Prototype in
mem.h
Description
memmove copies a block of len
bytes from src to dst. If
the
source
and destination arrays overlap, the
copy direction
is
chosen so that the data is always
copied correctly.
Return value
dst
*------------------------------------------------------------------------*/ void *_CType _FARFUNC
memmove( void *dst, const void *src,
size_t n ) { movmem( (void
*)src, dst, n ); return( dst
); }
|