2016-08-30 32 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了解了这个问题。我的文章不仅仅是提供想法,而且直接解决了作业。 (不完全是这个意思) –

相关问题