News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Copy mem to mem

Started by StarShaper, June 02, 2009, 05:45:20 PM

Previous topic - Next topic

StarShaper

Hi,

is there a way to copy a memory operand directly to another? Since in a mov instruction both operands cannot be memory operands, I have to write this.

mov dl,[edx+4]
mov [ecx+02h],dl


Is there a faster way to do this, saving cpu cycles. Maybe with MMX instructions?   :thumbu

Best regards,
starshaper

Vortex

Hi StarShaper,

Welcome to the forum.

If you are coding in a 32-bit environment, you could use the full 32-bit registers to copy data depending on your code. Have a look at Mark's assembly optimization tips :

http://www.mark.masmcode.com

dedndave

the string "movs" instructions can do it, but require other registers to be set up, first
if they had a "mov Item1, Item2" instruction, it would probably be slower that the 2 instruction method

Mark Jones

REP MOVSD is qute fast for dwords. There are many such algorithms here, be sure to use the search box near the top-left of the page.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Vortex

\masm32\m32lib\memcopy.asm

StarShaper

Thanks @all!   :thumbu

BlackVortex

You should generally prefer working with al/ax for operations like that, these instructions are more optimized for size+speed.

jj2007

Quote from: StarShaper on June 02, 2009, 05:45:20 PM

Is there a faster way to do this, saving cpu cycles. Maybe with MMX instructions?   :thumbu


See e.g. Unsafe at any speed: Memcpy() banished in Redmond.

As Mark wrote, rep movsd is very fast, especially if source and destination are 16-byte aligned (which is not guaranteed for HeapAlloc'ed memory...).

MMX is not faster than rep movsd, and it trashes your FPU.
XMM does not trash the FPU, but movdqa requires 16-byte alignment (and is not significantly faster than rep movsd).

Finally, for everyday use with single dwords, there is the m2m macro:
m2m MyGlobVar1, MyGlobVar2

Your choices :bg