Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!uw-beaver!cornell!batcomputer!sun.soe.clarkson.edu!gateway!PEAR.ECS.CLARKSON.EDU!nelson@PEAR.ECS.CLARKSON.EDU From: nelson@PEAR.ECS.CLARKSON.EDU Newsgroups: comp.sys.ibm.pc Subject: Fastest .ASM memory move Message-ID: <36@pear.ecs.clarkson.edu> Date: 3 Feb 89 16:00:30 GMT Sender: daemon@sun.soe.clarkson.edu Reply-To: nelson@clutx.clarkson.edu Lines: 27 What follows is the fastest way to move memory on an 80x86 machine. After writing it, I looked at Borland's movmem and memmove routines, and they do essentially the same thing, except that they will move in reverse if the source and destination overlap. -russ movemem: ;dos the same thing as "cld;rep movsb", only 50% faster. ;moves words instead of bytes, and handles the case of both addresses odd ;efficiently. There is no way to handle one address odd efficiently. ;This routine always aligns the source address in the hopes that the ;destination address will also get aligned. cld jcxz movemem_cnte ; If zero, we're done already. test si,1 ; Does source start on odd byte? jz movemem_adre ; Go if not movsb ; Yes, move the first byte dec cx ; Count tht byte movemem_adre: mov dx,cx ; save for later test shr cx,1 ; convert to word count rep movsw ; Move the bulk as words jnc movemem_cnte ; Go if the count was even movsb ; Move leftover last byte movemem_cnte: ret