【IDA分析学习0x02】除法的优化汇编代码分析

在进行除法运算的时候,编译器可能对除法优化,采用以下的方式运算

 1 .text:00401000                 push    ecx
 2 .text:00401001                 lea     eax, [esp+4+var_4] ; eax指向变量a
 3 .text:00401005                 push    eax
 4 .text:00401006                 push    offset aD       ; "%d"
 5 .text:0040100B                 call    _scanf
 6 .text:00401010                 mov     ecx, [esp+0Ch+var_4] ; ecx为读取到的数
 7 .text:00401014                 mov     eax, 2E8BA2E9h
 8 .text:00401019                 imul    ecx             ; ecx*2e8ba2e9h
 9 .text:0040101B                 sar     edx, 1          ; 优化后的除法 (a*2e8ba2e9h)>>(32+1) = a/11
10 .text:0040101D                 mov     ecx, edx        ; 把商放到ecx中
11 .text:0040101F                 shr     ecx, 1Fh        ; 右移31位
12 .text:00401022                 add     edx, ecx        ; 如果是负数就对结果+1
13 .text:00401024                 push    edx
14 .text:00401025                 push    offset unk_408030
15 .text:0040102A                 call    sub_401040
16 .text:0040102F                 xor     eax, eax
17 .text:00401031                 add     esp, 14h
18 .text:00401034                 retn

另外还有其他好几种优化的方式:http://blog.csdn.net/warlice/article/details/7803626

原文地址:https://www.cnblogs.com/driedfish/p/5435236.html