无法理解反汇编代码,任何想法?

时间:2014-10-09 05:46:03

标签: assembly disassembly ida

我试图理解一些反汇编的代码,但我无法理解这里发生了什么。你能解释它的作用吗?

sub     ecx, edi    
sar     edx, 1
mov     eax, 2AAAAAABh
imul    ecx
mov     eax, edx
shr     eax, 31
add     eax, edx
test    eax, eax
jle     ...

ecxedxedi包含此代码的某种输入值。

我只能假设最后两行可以像if(eax <= 0) goto ...一样工作,但我不确定。

3 个答案:

答案 0 :(得分:2)

2AAAAAAB是一个&#34;幻数&#34;。序列

MOV    EAX, 2AAAAAABh
IMUL   dividend
MOV    EAX, dividend
SHR    EAX, 31
ADD    EDX, EAX

是这个签名的部门而不使用IDIV

EDX = dividend/6

指令sar edx, 1无用,因为EDX将覆盖imul ecx。在C中,发布的序列可以写为

if ((ECX-EDI)/6 > 0) { ... } else ("jle") { ... }

答案 1 :(得分:1)

我认为它是为了未知目的检查计算溢出。

sub  ecx,edi       ; ecx = ??? no idea where these come from or what they mean

sar  edx,1         ; edx changed but value is lost, as are flags, no idea why this is done

mov  eax,2AAAAAABh ; eax = 715827883, no idea why this number is important
imul ecx           ; edx:eax = (original ecx-edi) * 715827883

mov  eax,edx       ; eax = high-dword of product
shr  eax,31        ; eax = high-bit of high-dword of product
add  eax,edx       ; eax = high-dword of product + high-bit of high-dword of product
                   ; assuming 0 <= ecx < ~10, eax will be zero if the result did not carry into edx
                   ; assuming ~-10 < ecx < 0, eax will be zero if the result did not carry into edx
                   ; therefore, |ecx|<~10, eax = overflow-from-multiplication

test eax,eax 
jle ...            ; taken if eax=0 or SF=OF

我不确定“sign flag = overflow flag”部分的意义是什么意思。对于较小的ecx值,可能无法发生。

答案 2 :(得分:1)

代码是优化分区的一种形式,代码中使用的常量是Wagstaff prime