决定用asm和mmx指令编制一个快速进行内存复制的函数来代替C运行库的memcpy(),
请高手指点有没有错误.代码如下:void FastMemcpy(unsigned char * Src, unsigned char * Dest, unsigned int Len)
{
  __asm{
mov esi, [Src]
mov edi, [Dest]
mov ecx, Len mov     eax, ecx        
sub     ecx, edi        
sub     ecx, eax        
and     ecx, 7          
sub     eax, ecx        
jle     L2 

emms
rep movsb
mov     ecx, eax
and     eax, 7
shr     ecx, 3
jz      L2
sub     edi, esi

L1:  movq    mm0, [esi]
movq    [edi+esi], mm0
add     esi, 8
dec     ecx
jnz     L1
add     edi, esi
emmsL2: add     ecx, eax
rep movsb
    }//end asm}

解决方案 »

  1.   

    VC++中的memcpy也是汇编写的,不过是32位的.LOOK LOOK 你的
      

  2.   

    下面是PTC的汇编代码,我测试了,速度可提高10%左右.
    各位老大,还有没有潜能可挖.
    ;
    ; TinyPTC by Gaffer
    ; www.gaffer.org/tinyptc
    ; Modifications by Gladius
    ;bits 32global _mmx_memcpysection .data

    align 16section .textalign 16_mmx_memcpy:    push ebp
        mov ebp,esp    pushad    mov edi,[ebp+8]       ; destination
        mov esi,[ebp+12]      ; source
        mov ecx,[ebp+16]      ; bytes    mov eax,ecx
        shr ecx,6
        mov ebx,ecx
        shl ebx,6
        sub eax,ebx cmp ecx,0
    je .donealign 16
                 
        .loop        movq mm0,[esi]
            movq mm1,[esi+8]
            movq mm2,[esi+16]
            movq mm3,[esi+24]
            movq mm4,[esi+32]
            movq mm5,[esi+40]
            movq mm6,[esi+48]
            movq mm7,[esi+56]
            movq [edi],mm0
            movq [edi+8],mm1
            movq [edi+16],mm2
            movq [edi+24],mm3
            movq [edi+32],mm4
            movq [edi+40],mm5
            movq [edi+48],mm6
            movq [edi+56],mm7
            add esi,8*8
            add edi,8*8
            dec ecx
    jnz .loop .done
        mov ecx,eax
        rep movsb    emms    popad
        
        pop ebp
        ret
      

  3.   

    更正一下代码:  
     
    void  FastMemcpy(unsigned  char  *  Src,  unsigned  char  *  Dest,  unsigned  int  Len)  
    {  
       __asm{  
               mov            esi,  Src
               mov            edi,  Dest
               mov            ecx,  Len  
     
               mov          eax,  ecx                  
               sub          ecx,  edi                  
               sub          ecx,  eax                  
               and          ecx,  7                      
               sub          eax,  ecx                  
               jle          L2    
                                         
               emms  
               rep            movsb  
               mov          ecx,  eax  
               and          eax,  7  
               shr          ecx,  3  
               jz            L2  
               sub          edi,  esi  
                                         
    L1:              movq        mm0,  [esi]  
               movq        [edi+esi],  mm0  
               add          esi,  8  
               dec          ecx  
               jnz          L1  
               add          edi,  esi  
               emms  
     
    L2:            add          ecx,  eax  
               rep            movsb  
           }//end  asm  
     
    }
      

  4.   

    这一段是什么意思?           mov          eax,  ecx   ; eax = Len
               sub          ecx,  edi   ; ecx = Len - (INT)Dest
               sub          ecx,  eax   ; ecx = ecx - Len等价与 ecx = (Len - (INT)Dest) - Len = 0 - (INT)Dest,直接用下面代码的不是更直观吗?           mov          ecx,  edi
               neg          ecx