【问题标题】:Can't understand disassembled code, any ideas?看不懂反汇编代码,有什么想法吗?
【发布时间】:2018-02-04 19:38:13
【问题描述】:

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

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 ... 这样工作,但我不确定。

【问题讨论】:

  • ecxedxedieaxregisterstest 指令集cflagsjle 等条件指令使用。
  • 我的意思是,ecxedxedi 寄存器包含此代码块的输入值。
  • ecx 通常是一个循环计数寄存器,edi 是一个目标寄存器,所以它很可能在一个数组上循环。您需要确定每个寄存器的用途,以确定代码实际在做什么。
  • 您知道mov eax, 2AAAAAABh 指令的含义吗?我不认为这是立即有意义的。
  • 这个常量在代码中使用了几千次。我不知道这意味着什么。也许,这是某种混淆或数学优化。

标签: assembly disassembly ida


【解决方案1】:

2AAAAAAB 是一个“幻数”。顺序

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") { ... }.

【讨论】:

【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我认为它正在检查未知用途的计算是否溢出。

    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
    

    我不确定“符号标志 = 溢出标志”部分的意义是什么。 ecx 值较小时可能不会发生。

    【讨论】:

    • 我仍然不知道这段代码的用途,但无论如何,感谢您的解释。
    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2021-01-05
    • 1970-01-01
    • 2021-08-22
    • 2017-10-13
    相关资源
    最近更新 更多