⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 代码优化.txt

📁 会变语言实现的一些程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:
                popad                                ;1 byte 

        优化为: 
        4)      inc dword ptr [ebx+4*ecx+1000h]      ;7 bytes 

        节省了8字节! 
        

        看一下lea指令能为我们干点什么呢? 
                lea eax, [12345678h] 

        eax的最后结果是什么呢?正确答案是12345678h. 

        假设 EBP = 1 
                lea eax, [ebp + 12345678h] 
        结果是123456789h....呵呵比较一下: 
                lea eax, [ebp + 12345678h]            ;6 bytes 
                ========================== 
                mov eax, 12345678h                    ;5 bytes 
                add eax, ebp                          ;2 bytes 

        5) 看看: 
                mov eax, 12345678h                    ;5 bytes 
                add eax, ebp                          ;2 bytes 
                imul ecx, 4                          ;3 bytes 
                add eax, ecx                          ;2 bytes 

        6) 用lea来进行一些计算我门将从体积上得到好处: 

                lea eax, [ebp+ecx*4+12345678h]        ;7 bytes 

        速度上一条lea指令更快!不影响标志位...记住下面的格式,在许多地方善用他们你可以节省时间和空间. 

OPCODE [BASE + INDEX*SCALE + DISPLACEMENT]


10.下面是关于病毒重定位优化的,惧毒人士请绕行... 
        
        下面的代码你不应该陌生 
        1)      call gdelta 
        gdelta: pop ebp 
                sub ebp, offset gdelta 

        在以后的代码中我们这样使用delta来避免重定位问题 
                lea eax, [ebp + variable] 

        这样的指令在应用内存数据的时候是不可避免的,如果能优化一下,我门将会得到数倍收益,打开你的sice或者trw或者ollydbg等调试器,看看: 

        3)      lea eax, [ebp + 401000h]              ;6 bytes 
        
        假如是下面这样      
        4)      lea eax, [ebp + 10h]                  ;3 bytes 

        也就是说如果ebp后面变量是1字节的话,总的指令就只有3字节        
        修改一下最初的格式变为: 

        5)      call gdelta 
        gdelta: pop ebp 

        在某些情况下我们的指令就只有3字节了,可以节省3字节,嘿嘿,让我们看看: 
        6)      lea eax, [ebp + variable - gdelta]    ;3 bytes 

        和上面的是等效的,但是我们可以节省3字节,看看CIH... 

11.其他技巧: 
      如果EAX小于80000000h,edx清0: 
        -------------------------------------------------- 

        1)      xor edx, edx                          ;2 bytes, but faster 

        2)      cdq                                  ;1 byte, but slower 

        我一直使用cdq,为什么不呢?体积更小... 


        下面这种情况一般不要使用esp和ebp,使用其他寄存器. 
        ----------------------------------------------------------- 

        1)      mov eax, [ebp]                        ;3 bytes 
        2)      mov eax, [esp]                        ;3 bytes 
        3)      mov eax, [ebx]                        ;2 bytes 


        交换寄存器中4个字节的顺序?用bswap 
        --------------------------------------------------------- 
                mov eax, 12345678h                    ;5 bytes 

                bswap eax                            ;2 bytes 

                ;eax = 78563412h now      

        Wanna save some bytes replacin' CALL ? 
        --------------------------------------- 

        1)      call _label_                          ;5 bytes 
                ret                                  ;1 byte 

        2)      jmp _label_                          ;2/5 (SHORT/NEAR) 

        如果仅仅是优化,并且不需要传递参数,请尽量用jmp代替call 
        

        比较 reg/mem 时如何节省时间: 
        ------------------------------------------ 

        1)      cmp reg, [mem]                        ;slower 

        2)      cmp [mem], reg                        ;1 cycle faster 


        乘2除2如何节省时间和空间? 
        ------------------------------------------------------------ 
        1)      mov eax, 1000h 
                mov ecx, 4                            ;5 bytes 
                xor edx, edx                          ;2 bytes 
                div ecx                              ;2 bytes 

        2)      shr eax, 4                            ;3 bytes 

        3)      mov ecx, 4                            ;5 bytes 
                mul ecx                              ;2 bytes 

        4)      shl eax, 4                            ;3 bytes 
        

        loop指令 
        ------------------------ 

        1)      dec ecx                              ;1 byte 
                jne _label_                          ;2/6 bytes (SHORT/NEAR) 

        2)      loop _label_                          ;2 bytes 

        再看: 
        3)      je $+5                                ;2 bytes 
                dec ecx                              ;1 byte 
                jne _label_                          ;2 bytes 

        4)      loopXX _label_ (XX = E, NE, Z or NZ)  ;2 bytes 
        loop体积小,但486以上的cpu上执行速度会慢一点... 


      比较: 
        --------------------------------------------------------- 
        1)      push eax                              ;1 byte 
                push ebx                              ;1 byte 
                pop eax                              ;1 byte 
                pop ebx                              ;1 byte 
      
      
        2)      xchg eax, ebx                        ;1 byte 

        3)      xchg ecx, edx                        ;2 bytes 
        如果仅仅是想移动数值,用mov,在pentium上会有较好的执行速度: 
        4)      mov ecx, edx                          ;2 bytes 


        比较: 
        -------------------------------------------- 

        1) 未优化: 
        lbl1:  mov al, 5                            ;2 bytes 
                stosb                                ;1 byte 
                mov eax, [ebx]                        ;2 bytes 
                stosb                                ;1 byte 
                ret                                  ;1 byte 
        lbl2:  mov al, 6                            ;2 bytes 
                stosb                                ;1 byte 
                mov eax, [ebx]                        ;2 bytes 
                stosb                                ;1 byte 
                ret                                  ;1 byte 
                                                      --------- 
                                                      ;14 bytes 
        2) 优化了: 
        lbl1:  mov al, 5                            ;2 bytes 
        lbl:    stosb                                ;1 byte 
                mov eax, [ebx]                        ;2 bytes 
                stosb                                ;1 byte 
                ret                                  ;1 byte 
        lbl2:  mov al, 6                            ;2 bytes 
                jmp lbl                              ;2 bytes 
                                                      --------- 
                                                      ;11 bytes 

      读取常数变量,试试在指令中直接定义: 
      -----------------------------    
                mov eax, [ebp + variable]            ;6 bytes 
                ... 

      mov [ebp + variable], eax            ;6 bytes 
                ... 
                ... 
      variable dd      12345678h                    ;4 bytes 

        2) 优化为: 

                mov eax, 12345678h                    ;5 bytes 
      variable = dword ptr $ - 4 
                ... 
                ... 
                mov [ebp + variable], eax            ;6 bytes 

        呵呵,好久没看到这么有趣的代码了,前提是编译的时候支持代码段的写入属性要被设置. 
        
        最后介绍未公开指令SALC,现在的调试器都支持...什么含义呢:就是CF位置1的话就将al置为0xff 
        ------------------------------------------------------------------ 

        1)      jc _lbl1                              ;2 bytes 
                mov al, 0                            ;2 bytes 
                jmp _end                              ;2 bytes 
          _lbl: mov al, 0ffh                          ;2 bytes 
          _end: ... 

        2)      SALC  db    0d6h                    ;1 byte ;) 
------------------------------------------------------------------>over... 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -