【问题标题】:What does this Code in Assembler do?汇编程序中的这段代码有什么作用?
【发布时间】:2016-08-30 16:06:24
【问题描述】:

我得到了以下代码,现在尝试理解它。

数组指向足够大的内存,在.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 . . .

它要求或有必要我可以提供我的想法,但担心它会混淆而不是它的好处。 感谢您的任何建设性帮助。

【问题讨论】:

  • 我注意到这个确切的问题几年前在德国大学被用作homework assignment。没有您的想法和您当前对代码的理解或关于您不理解的特定问题 - 您现在出现在要求我们做功课。
  • @MichaelPetch 这似乎是从您链接的 PDF 中复制过来的。
  • @MichaelPetch 如果你研究得这么好,那么你也知道德国目前有假期,假期没有作业,所以我问是因为我想知道不是因为我需要对下一个作业的好评。但是无所谓。通常人们不会得到关于 SO 的有用评论(甚至没有谈论答案),但有时这些 cmets 仍然足以进行逆向工程并理解事情是如何工作的。
  • 在您的问题中,您说 “它要求或有必要我可以提供我的想法,”。我要求您提供您的想法。编辑您的问题,告诉我们什么您已经了解,您认为某些代码在做什么,如果您对您不理解的代码有特定的问题,您可以提出这个问题。是否在假期无关紧要。这是几年前考试问题/家庭作业的精确副本不会让学生自己思考

标签: assembly


【解决方案1】:

该函数名为fib,它正在创建一个斐波那契数列数组。工作的主体是按照标签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

剩下的交给你。

【讨论】:

  • 啊。一个绿色的勾号很快就被撤消了,取而代之的是反对票。多好!我已经回答了页面顶部的问题,并对代码的基本部分进行了注释。
  • 这样啊,你期待什么?你不会在这里得到感激或感谢。但是无所谓。我感谢您的回复。我使用它,重新编辑我的问题并用我的 cmets 标记我的进度。
  • 那你为什么要撤掉绿勾呢?你期待一个完整的逆向工程师吗?
  • 这里的人真的在乎吗?我没想到代码的完整解释,那会很疯狂+有点粗鲁。一个“基本”的想法或代码的目的,你可以在 1 行注释中写的东西,实际上就足够了。
【解决方案2】:

所以已经给出了正确的答案,这个问题的原因可能是可疑的,但我还需要处理我的程序集,这是一个有趣的练习。

我的逆向工程 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;
}

【讨论】:

  • 我喜欢这个问题,并且或多或少地为自己解决了问题,因为已经给出了正确的答案并且 OP 理解了这个问题。我的帖子不仅仅是提供想法,还直接解决了作业。 (不完全是 SO 的意思)
猜你喜欢
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 2019-11-02
  • 2012-12-28
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
相关资源
最近更新 更多