32 bit optimization

来源:互联网 发布:韩国加工中心编程 编辑:程序博客网 时间:2024/05/17 00:42

(1)测试寄存器是否为0
    

    cmp     eax,00000000h                   ; 6 bytes
    jz      bribriblibli                    ; 2 bytes (if jz is short)
    

 
 optimization:
    
    or      eax,eax                         ; 2 bytes
    jz      bribriblibli                       ; 2 bytes (if jz is short)



    xchg    eax,ecx                         ; 1 byte
    jecxz   bribriblibli                   ; 2 bytes (if it is short)



(2)测试寄存器是否为-1

    cmp     eax,0FFFFFFFFh                  ; 6 bytes
    jz      insumision                    ; 2 bytes (if short)

optimization:

    inc     eax                                   ; 1 byte
    xchg    eax,ecx                            ; 1 byte
    jecxz   insumision                         ; 2 bytes (if short)
    dec     ecx                                 ; 1 byte 


    inc     eax                                  ; 1 byte
    jz      insumision                        ; 2 bytes
    dec     eax                                ; 1 byte


(3)寄存器清0并传送低位字数值

    xor     eax,eax                         ; 2 bytes
    mov     ax,word ptr [esi+6]             ; 4 bytes

optimization:

    movzx   eax,word ptr [esi+6]            ; 4 bytes

(4) 关于push的优化

    mov   eax, 50h                   ; 5 bytes

optimization:
    push  50h                        ; 2 bytes 
    pop      eax                        ; 1 bytes
    


    push   0                         ; 2 bytes 
    push   0                         ; 2 bytes 
    push   0                         ; 2 bytes 
    push   0                         ; 2 bytes 
    push   0                         ; 2 bytes 
    push   0                         ; 2 bytes 
    push   0                         ; 2 bytes 

optimization:

    xor   eax, eax                           ; 2 bytes 
    push   eax                     ; 1 byte 
    push   eax                     ; 1 byte 
    push   eax                     ; 1 byte 
    push   eax                     ; 1 byte 
    push   eax                     ; 1 byte 
    push   eax                     ; 1 byte 
    push   eax                     ; 1 byte 


    push   7                         ; 2 bytes 
    pop   ecx                     ; 1 byte 
_loop: 
    push   0                         ; 2 bytes 
    loop   _loop                    ; 2 bytes 

(5) 操作FS寄存器相关优化

    push    dword ptr fs:[00000000h]        ; 6 bytes
    mov     fs:[0],esp                      ; 6 bytes
    [...]
    pop     dword ptr fs:[00000000h]        ; 6 bytes
    
optimization:

    xor     eax,eax                         ; 2 bytes
    push    dword ptr fs:[eax]              ; 3 bytes
    mov     fs:[eax],esp                    ; 3 bytes
    [...]
    pop     dword ptr fs:[eax]              ; 3 bytes

(6) 字符串操作

    mov al/ax/eax, [esi]                ; 2/3/2 bytes 
    inc esi                          ; 1 byte 

optimization:
    lodsb/w/d                          ; 1 or 2 byte 

 到达字符串尾部。

    lea     edi,[ebp+ASCIIz_variable]       ; 6 bytes
 @@1:   
    cmp     byte ptr [edi],00h              ; 3 bytes
    inc     edi                             ; 1 byte
    jz      @@2                             ; 2 bytes
    jmp     @@1                             ; 2 bytes
 @@2:   
    inc     edi                             ; 1 byte

optimization:
    lea     edi,[ebp+ASCIIz_variable]       ; 6 bytes
    xor     al,al                           ; 2 bytes
 @@1:   
    scasb                                   ; 1 byte
    jnz     @@1                             ; 2 bytes



(7)乘法 

    mov     ecx,28h                         ; 5 bytes
    mul     ecx                             ; 2 bytes


optimization:
    imul    eax,eax,28h                     ; 3 bytes



(8)置edx寄存器为0.
    xor  edx, edx                      ; 2 bytes

optimization:
    cdq                              ; 1 bytes



(9)交换寄存器4字节的顺序

    mov eax, 00200000h                   ; 5 bytes 
    bswap eax                         ; 2 bytes 
    ;eax = 00002000h now 



(10)乘2、除2

    shl   eax, 1                     ; 2 bytes
    ;*2
    
    shr  eax, 1                        ; 2 bytes
    ;/2      

(11)分配堆栈空间

    push    ebp                             ; 1 byte
    mov     ebp,esp                         ; 2 bytes
    sub     esp,20h                         ; 3 bytes

optimization:
    enter   20h,00h                         ; 4 bytes



(12)压入字符串指针时尽量使用
    call  @f
    db  'string', 0
    @@:

原创粉丝点击