Electronic – RAM and data copying/moving

cpuram

I was wondering about the copying/moving of data in RAM and I couldn't help but thing about the possibility of having the RAM process basic memory operations such as copying/moving of memory, if there's no need for the CPU to know about the data that's being copied/moved, wouldn't we benefit from having a single instruction that could communicate with the RAM like this?

Best Answer

Memory to memory copying may be considered wasteful by some (as mentioned in a comment above) but it is done all the time, for example the memcpy anmd strcpy functions in C programs. Also memset, to fill memory and strcmp to compare strings.

I think the closest you are going to find to a single instruction move memory is the MOVS (move string family) in the 8086 instruction set.

In all cases, the source address is setup in the [DS:SI] or [DS:ESI] registers, and the to destination address is set up in the [ES:DI] or [ES:EDI] registers.

Once set up, the single instruction MOVSB copies the byte at [DS:SI] or [DS:ESI] to [ES:DI] or [ES:EDI], and then increments or decrements (depending on the direction flag: increments if the flag is clear, decrements if it is set) SI and DI (or ESI and EDI).

What makes this instruction so powerful is that it can be prefixed by a REP instruction, which repeats the following instruction the number of times contained in the CX register. Variations allow it to be terminated early based on a zero flag (gee, like the zero at the end of a C string).

So you can think of REP/MOVSB combo as being a two instruction implementation of the loop portion of memcpy function. Granted, the work is still being performed by the CPU, not the memory controller itself, but it is much simpler than having to write out a loop of instructions.

Besides MOVSB, there are other variations to copy words (2 bytes), and double words (4 bytes) at a time.

There is also a STOS instruction which, prefixed with REP stores a constant value in memory (guess what, memset) and CMPS / REP combo which compares two areas of memory (i.e. strcmp).

As someone pointed out, in processors with DMA, you can set up source, destination, and count registers and then let the DMA do the work much like the REP/MOVSB combo discussed above, to perform a memcpy function without the processor being involved. But I don't think DMA's have the ability to stop a copy on a zero byte (correct me if I'm wrong), so they would be unable to do a strcpy. Same for memset and strcmp.