2016-08-30 42 views
-5

我給了下面的代碼,現在試着理解它。彙編程序中的代碼是做什麼的?

數組poiting到足夠大的內存,在.bss段代碼爲0

1 fib : file format elf 32−i386 
2 
3 Disassembly of section.text : 
4 
5 0 x08048080 <start>: 
6 0 x08048080 : mov eax , 0x80490d0 <array> 
7 0 x08048085 : push eax 
8 0 x08048086 : mov eax , 0 x3 
9 0 x0804808b : push eax 
10 0 x0804808c : call 0 x8048098 <fib> 
11 0 x08048091 : pop eax 
12 0 x08048092 : pop eax 
13 0 x08048093 : jmp 0 x80480c1 <Lend> 
14 
15 0 x08048098 <fib>: 
16 0 x08048098 : mov ebx , [esp+0x8] 
17 0 x0804809c : mov ecx , 0 x2 
18 0 x080480a1 : xor edx , edx 
19 0 x080480a3 : mov [ebx] , edx 
20 0 x080480a5 : mov eax , 0x1 
21 0 x080480aa : mov [ebx+0x4] , eax 
22 0 x080480ad : jmp 0 x080480ba <Lloop_cond> 
23 
24 0 x080480b2 <Lloop >: 
25 0 x080480b2 : push eax 
26 0 x080480b3 : add eax , edx 
27 0 x080480b5 : mov [ebx+ecx∗4] , eax 
28 0 x080480b8 : pop edx 
29 0 x080480b9 : inc ecx 
30 
31 0 x080480ba <Lloop_cond>: 
32 0 x080480ba : cmp ecx , [esp+0x4] 
33 0 x080480be : jle 0x080480b2 <Lloop> 
34 0 x080480c0 : ret 
35 
36 0 x080480c1 <Lend>: 
37 0 x080480c1 : mov ebx , 0 x0 ; Exit code 0 = success 
38 0 x080480c6 : mov eax , 0 x1 ; Select System call exit 
39 0 x080480cb : int 0 x80 ; System call 
40 
41 Disassembly of section.bss : 
42 
43 0 x080490d0 <array>: 
44 . . . 

它要求或neccessary預先初始化,我可以提供我的想法,但擔心它混淆了更多比它好。 謝謝你的建設性幫助。

+3

我注意到了這個問題詢問被用作[家庭作業(http://www3.informatik.uni-erlangen.de/Lehre/GRa/Klausuren/ klausur-gra-2012-04-13.pdf)在幾年前的德國大學。如果沒有你的想法和你對代碼的理解或者你對某些你不明白的問題的具體問題 - 你現在就會問我們做你的功課。 –

+1

@MichaelPetch它似乎已經從您鏈接的PDF中字面複製。 – fuz

+0

@MichaelPetch如果你非常擅長研究,那麼你也知道德國目前有假期,假期也沒有作業,所以我問,因爲我想知道的不是因爲我需要對下一個作業做好評論。但是無所謂。通常,人們對SO沒有得到有用的評論(甚至沒有談論答案),但是有時候這些評論仍然足以進行逆向工程並理解事情是如何工作的。 – blauerschluessel

回答

2

該函數被稱爲fib它正在製作一個Fibonacci序列的數組。工作的主體是在標籤Lloop後面的幾條指令完成的。

Lloop: 
    push eax    ; save current term 
    add eax , edx   ; add previous term 
    mov [ebx+ecx*4] , eax ; write to array 
    pop edx     ; retrieve previous term for next loop 
    inc ecx     ; loop control and array index 

Lloop_cond: 
    cmp ecx , [esp+0x4]  ; end test 
    jle Lloop    ; repeat 
    ret      ; done 

我會把剩下的給你。

+0

啊。一個綠色的滴答快速撤回並由downvote取代。多好!我已經回答了頁面頂部的問題,並註釋了代碼的重要部分。 –

+0

這就是你所期望的?你不會在這裏得到感謝或感謝。但是無所謂。我感謝你的迴應。我使用它,重新編輯我的問題,並用我的評論標記我的進步。 – blauerschluessel

+0

那你爲什麼要撤回綠色的勾號?你期望有一個完整的反向工程師嗎? –

1

所以已經給出了正確的答案,問題的原因可能是陰暗的,但我需要在我的程序集上工作,這是一個有趣的練習。

我的逆向工程FIB歸結爲這樣的功能:

void fib_simple(int* array, int n) 
{ 
    int i, j, tmp; 
    int count = 2; 

    array[0] = i = 0; 
    array[1] = j = 1; 

    for (count = 2; count <= n; count++) { 
     tmp = j; 
     j = array[2] = i + j; 
     i = tmp; 
    } 
} 

現在當然這只是一個簡潔的音譯。實際的逆向工程功能看起來更像是這樣的:

void fib(int* array, int n) 
{ 
    int ecx, edx, tmp; 

    /* mov ebx , [esp+0x8]; ebx is the array */ 
    /* mov ecx , 0x2; ecx is a counter, starting from 2 */ 
    ecx = 2; 

    /* xor edx, edx ; set edx to zero  */ 
    edx = 0; 

    /* mov [ebx], edx; set array[0] to edx */ 
    array[0] = edx; 

    /* mov eax , 0x1 */ 
    eax = 1; 

    /* mov [ebx+0x4] , eax */ 
    array[1] = 1; 

    /* The while loop condition is checked with a loop condition: 
    <Lloop_cond>:   ; place to jump back to every iteration 
    cmp ecx , [esp+0x4] ; compare to n 
    jle 0x080480b2 <Lloop> ; if ecx <= n, jump to loop 
    */ 
    while (ecx <= n) { 
     /* 
     This is the inner loop 
     <Lloop >: 
     */ 

     /* push eax; eax is temporarily saved to stack */ 
     tmp = eax; 

     /* add eax, edx */ 
     eax += edx; 

     /* mov [ebx+ecx∗4], eax */ 
     array[ecx] = eax; 

     /* pop edx */ 
     edx = tmp; 

     /* inc ecx */ 
     ecx++; 
    } 

    /* ret */ 
    return; 
} 
+0

爲什麼downvote?它對我來說看起來像個好的答案。 – Johan

+0

我喜歡這個問題,並且爲自己做了或多或少的工作,因爲給出了正確的答案,OP瞭解了這個問題。我的文章不僅僅是提供想法,而且直接解決了作業。 (不完全是這個意思) –