The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: StarShaper on June 02, 2009, 05:45:20 PM

Title: Copy mem to mem
Post by: StarShaper on June 02, 2009, 05:45:20 PM
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
Title: Re: Copy mem to mem
Post by: Vortex on June 02, 2009, 06:14:37 PM
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
Title: Re: Copy mem to mem
Post by: dedndave on June 02, 2009, 06:31:33 PM
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
Title: Re: Copy mem to mem
Post by: Mark Jones on June 02, 2009, 06:35:45 PM
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.
Title: Re: Copy mem to mem
Post by: Vortex on June 02, 2009, 06:48:23 PM
\masm32\m32lib\memcopy.asm
Title: Re: Copy mem to mem
Post by: StarShaper on June 02, 2009, 08:19:43 PM
Thanks @all!   :thumbu
Title: Re: Copy mem to mem
Post by: BlackVortex on June 02, 2009, 08:27:41 PM
You should generally prefer working with al/ax for operations like that, these instructions are more optimized for size+speed.
Title: Re: Copy mem to mem
Post by: jj2007 on June 02, 2009, 11:56:26 PM
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 (http://www.masm32.com/board/index.php?topic=11432.0).

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