2014-10-09 80 views
1

我想了解一些反彙編的代碼,但我無法理解這裏發生了什麼。你能解釋它的作用嗎?無法理解反彙編代碼,有什麼想法?

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 ...那樣工作,但我不確定。

+0

'ecx','edx','edi'和'eax'是[寄存器(HTTPS: //en.wikipedia.org/wiki/X86_assembly_language)。 'test'指令設置'jle'這樣的條件指令使用的'cflags'。 – Jason 2014-10-09 05:54:49

+0

我的意思是,'ecx','edx'和'edi'寄存器包含這個代碼塊的輸入值。 – 2014-10-09 05:56:01

+0

'ecx'通常是一個循環計數寄存器,'edi'是一個目標寄存器,所以它可能在一個數組上循環。您需要確定每個寄存器的用途,以確定代碼實際上在做什麼。 – Jason 2014-10-09 06:09:15

回答

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值可能不會發生。

+0

而我仍然不知道這段代碼的目的,但無論如何,謝謝你的解釋。 – 2014-10-09 09:10:04

2

2AAAAAAB是一個「幻數」。序列

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

是這個師簽署不使用IDIV

EDX = dividend/6

指令sar edx, 1是沒用的,因爲EDXimul ecx被覆蓋。在C中,發佈的序列可以寫爲

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

+0

multiplicative inverses的全部細節:[爲什麼GCC在實現整數除法時使用奇數乘法?](https://stackoverflow.com/questions/41183935/why-does-gcc-use-multiplication-by-a-陌生的號碼,在-實施-整數迪維)。這個問題不是相當重複的,因爲它也詢問了分支,並有神祕的死亡'sar'。在反編譯的代碼中非常令人驚訝。可能它不是編譯器生成的或被錯誤複製的。 – 2018-02-04 13:06:35

1

的代碼是一個優化的分割的一種形式,在該代碼中使用的常數是Wagstaff prime