打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
memcpy和memmove函数的实现
                                          memcpy和memmove函数的实现


memcpy

代码:
;***
;memcpy.asm - contains memcpy and memmove routines
;
;       Copyright (c) 1986-1997, Microsoft Corporation. All right reserved.
;
;Purpose:
;       memcpy() copies a source memory buffer to a destination buffer.
;       Overlapping buffers are not treated specially, so propogation may occur.
;       memmove() copies a source memory buffer to a destination buffer.
;       Overlapping buffers are treated specially, to avoid propogation.
;
;*******************************************************************************
;***
;memcpy - Copy source buffer to destination buffer
;
;Purpose:
;       memcpy() copies a source memory buffer to a destination memory buffer.
;       This routine does NOT recognize overlapping buffers, and thus can lead
;       to propogation.
;       For cases where propogation must be avoided, memmove() must be used.
;
;       Algorithm:

       void* memcpy(void* dest, void* source, size_t count)

      {

           void* ret = dest;

          //copy from lower address to higher address

          while (count--)

                  *dest++ = *source;

 

           return ret;

      }

 

 

memmove

memmove - Copy source buffer to destination buffer
;
;Purpose:
;       memmove() copies a source memory buffer to a destination memory buffer.
;       This routine recognize overlapping buffers to avoid propogation.
;       For cases where propogation is not a problem, memcpy() can be used.
;
;   Algorithm:

    void* memmove(void* dest, void* source, size_t count)

   {

       void* ret = dest;

 

       if (dest <= source || dest >= (source + count))

       {

          //Non-Overlapping Buffers
         //copy from lower addresses to higher addresses
    

         while (count --)

               *dest++ = *source++;

     }

     else

     {

        //Overlapping Buffers
       //copy from higher addresses to lower addresses

 

       dest += count - 1;

       source += count - 1;

       while (count--)

                *dest-- = *source--;l

      }

      return ret;

   }

 

另一种实现:

void* mymemcpy( void* dest, const void* src, size_t count ) 

    char* d = (char*)dest; 
    const char* s = (const char*)src; 
  //  int n = (count + 7) / 8; // count > 0 assumed 
    int n = count >> 3; 
    switch( count & 7 ) 
    { 
              do {  *d++ = *s++; 
    case 7:        *d++ = *s++; 
    case 6:        *d++ = *s++; 
    case 5:        *d++ = *s++; 
    case 4:        *d++ = *s++; 
    case 3:        *d++ = *s++; 
    case 2:        *d++ = *s++; 
    case 1:        *d++ = *s++; 
    case 0          } //while (--n > 0); 
                 while (n-- > 0) 
    } 

    return dest; 
}

 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
memcpy, memmove的实现
memcpy&memmove的比较
strcpy,memcpy,memmove,memset,strncpy
字符串库函数strcpy strcmp strstr memcpy memmove等的实现
memmove 和 memcpy的区别
【笔试】字符串常见笔试题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服