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?
-
\$\begingroup\$ If you wanted to make RAM more expensive, sure. \$\endgroup\$– Ignacio Vazquez-AbramsSep 11, 2014 at 1:59
-
3\$\begingroup\$ Or you could use DMA to do memory-to-memory copying. \$\endgroup\$– marktSep 11, 2014 at 3:03
-
3\$\begingroup\$ Memory-to-memory copying is wasteful and to be avoided anyway. Unless you need to modify the data in some way, it's far better to put it into memory once and then pass around pointers to it. In systems with MMUs, you can use the MMU to map the physical data to different places in the logical address space. \$\endgroup\$– Dave TweedSep 11, 2014 at 4:44
2 Answers
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.
-
\$\begingroup\$ You could do a memset under DMA control by pointing the source to a memory location holding the value to be set, then not increment the source address between transfers. Comparisons are another matter though. \$\endgroup\$– marktSep 11, 2014 at 9:12
This has been done, mainly in graphics context.
One early variant was the Blitter chips used in several video games and computers in the 80ies, which allows copying and simple processing without involving the CPU. These chips also had masking operations that allowed leaving out some elements from the operation.
The drawback is that since these share access to the RAM, this would change timing while the operation was running, slowing down execution on the main CPU and making execution times unpredictable, so timing sensitive code would stop working.
Any graphics card with dedicated memory essentially behaves like special RAM with included blitter operations from the point of view of the CPU. Several vendors, most famously Tseng Labs (later bought by ATI) also made special RAM (SGRAM) that could run simple operations like XOR (used for drawing window borders during drag operations) or fill blocks in a single operation.
For system RAM, having a predictable access pattern is more important for good system performance than offloading work, so it is actually more efficient to perform copies from the CPU, where they can be scheduled and merged together with other accesses in a single RAM controller, than in a separate unit.