Implementation of memcpy and memmove Functions

Source: Internet
Author: User
Memcpy

Code:
;***
; 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;

}

 

Another implementation:

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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.