31

How does the standard C function 'memcpy' work? It has to copy a (large) chunk of RAM to another area in the RAM. Since I know you cannot move straight from RAM to RAM in assembly (with the mov instruction) so I am guessing it uses a CPU register as the intermediate memory when copying?

But how does it copy? By blocks (how would it copy by blocks?), by individual bytes (char) or the largest data type they have (copy in long long double's - which is 12 bytes on my system).

EDIT: Ok apparently you can move data from RAM to RAM directly, I am not an assembly expert and all I have learnt about assembly is from this document (X86 assembly guide) which mentions in the section about the mov instruction that you cannot move from RAM to RAM. Apparently this isn't true.

PersonWithName
  • 848
  • 2
  • 13
  • 19

3 Answers3

26

Depends. In general, you couldn't physically copy anything larger than the largest usable register in a single cycle, but that's not really how machines work these days. In practice, you really care less about what the CPU is doing and more about the characteristics of DRAM. The memory hierarchy of the machine is going to play a crucial determining role in performing this copy in the fastest possible manner (e.g., are you loading whole cache-lines? What's the size of a DRAM row with respect to the copy operation?). An implementation might instead choose to use some kind of vector instructions to implement memcpy. Without reference to a specific implementation, it's effectively a byte-for-byte copy with a one-place buffer.

Here's a fun article that describes one person's adventure into optimizing memcpy. The main take-home point is that it is always going to be targeted to a specific architecture and environment based on the instructions you can execute inexpensively.

Gian
  • 13,735
  • 44
  • 51
15

The implementation of memcpy is highly specific to the system in which it is implemented. Implementations are often hardware-assisted.

Memory-to-memory mov instructions are not that uncommon - they have been around since at least PDP-11 times, when you could write something like this:

    MOV FROM, R2
    MOV TO,   R3
    MOV R2,   R4
    ADD LEN,  R4
CP: MOV (R2+), (R3+) ; "(Rx+)" means "*Rx++" in C
    CMP R2, R4
    BNE CP

The commented line is roughly equivalent to C's

*to++ = *from++;

Contemporary CPUs have instructions that implement memcpy directly: you load special registers with the source and destination addresses, invoke a memory copy command, and let CPU do the rest.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    "they have been around since at least PDP-11 times" -- far longer. – Jim Balter Jul 06 '13 at 07:47
  • 1
    @JimBalter This does not surprise me at all :) – Sergey Kalinichenko Jul 06 '13 at 09:51
  • 1
    to and from are void pointers, and I thought you can't dereference void pointers. Would you first type cast them to `(unsigned char*)` – Rockstar5645 Sep 12 '19 at 02:30
  • 1
    @Rockstar5645 Assembly has no concept of type, so it's happy to dereference whatever address you pass as a `void*`. Of course if you are writing an implementation in C, you'd have to typecast these pointers to something that you can dereference, such as `unsigned char*`. – Sergey Kalinichenko Sep 12 '19 at 02:41
8

A trivial implementation of memcpy is:

 while (n--) *s2++ = *s1++;

But glibc usually uses some clever implementations in assembly code. memcpy calls are usually inlined.

On x86, the code checks if the size parameter is a literal multiple of 2 or a multiple of 4 (using gcc builtins functions) and uses a loop with movl instruction (copy 4 bytes) otherwise it calls the general case.

The general case uses the fast block copy assembly using rep and movsl instructions.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 2
    but s2 and s1 are void pointers, and I thought you couldn't dereference void pointers. – Rockstar5645 Sep 12 '19 at 02:29
  • 1
    @ouah - why use `movl` only on sizes that are multiples of 4 and not always try to use `movl`? if you have to copy a total of 50 bytes you can't you copy using 12 `movl` and 2 `mov`? – joepol May 13 '21 at 14:27
  • 1
    @Rockstar5645 - you must cast before, I believe ouah referenced this : [gcc memcpy implementation](https://github.com/gcc-mirror/gcc/blob/master/libgcc/memcpy.c) – joepol May 13 '21 at 14:35