1

我正在尝试将此汇编代码翻译成 C,我需要帮助。它与while循环有关,但我不知道while循环中发生了什么。我已经看了一段时间,我确定它包含“while(something =!null)”然后做一些事情,但我不知道当代码“movl”进入%eax时会发生什么。

本节是编译的 x86 汇编代码:

whilecode:
        pushl   %ebp
        movl    %esp, %ebp
        jmp     .L20
.L22:
        movl    8(%ebp), %eax
        movl    16(%eax), %eax
        movl    %eax, 8(%ebp)
.L20:
        cmpl    $0, 8(%ebp)
        je      .L21
        movl    8(%ebp), %eax
        movl    4(%eax), %eax
        cmpl    12(%ebp), %eax
        jne     .L22
.L21:
        cmpl    $0, 8(%ebp)
        setne   %al
        movzbl  %al, %eax
        popl    %ebp
        ret

这是一个节点的定义:

typedef enum {CHAR,SHORT,INT} Type;

typedef struct node {
  Type   thetype;
  int     data;
  void   *opaque;
  struct node *ptr1, *ptr2;
} Node;

这是 while 循环的函数定义:

/* a while loop */
int whilecode(Node *somenode, int data)
{
  // FIX ME
  return 0;
}
4

2 回答 2

5

评论大会的作用:

whilecode:
    pushl   %ebp            // save caller's frame pointer
    movl    %esp, %ebp      // set up our frame pointer
                            // no local variables set up
    jmp     .L20            // jump to the entry point of the function body

.L22:                       // NOT the beginning of the function -- probably a loop body
    movl    8(%ebp), %eax   // %eax = first argument
    movl    16(%eax), %eax  // %eax = %eax->fifth field
    movl    %eax, 8(%ebp)   // first argument = %eax
.L20:
    cmpl    $0, 8(%ebp)     // compare first argument to 0
    je      .L21            // branch to exit if they're equal 
    movl    8(%ebp), %eax   // %eax = first argument
    movl    4(%eax), %eax   // %eax = %eax->second field
    cmpl    12(%ebp), %eax  // compare %eax to second argument
    jne     .L22            // loop if not equal
.L21:
    cmpl    $0, 8(%ebp)     // compare first argument to 0
    setne   %al             // set %al = 1 if they're not equal (0 otherwise)
    movzbl  %al, %eax       // zero extend %al to %eax
    popl    %ebp            // restore the callers stack frame
    ret

现在你有一个结构定义和一个原型,所以这最终是:

int whilecode(Node *somenode, int data)
{
    while (somenode != 0 && somenode->data != data)
        somenode = somenode->ptr2;
    return somenode != 0;
}

在链表中搜索包含特定数据值的节点,如果找到则返回 true。

于 2013-04-28T05:17:24.247 回答
0

固定的

whilecode:
        pushl   %ebp         `Push EBP to stack`
        movl    %esp, %ebp   `EBP = ESP`
        jmp     .L20         `goto L20`
.L22:
        movl    8(%ebp), %eax  `EAX = (EBP+8)`
        movl    16(%eax), %eax `EAX = (EAX+16)`
        movl    %eax, 8(%ebp)  `(EBP+8) = EAX`
.L20:
        cmpl    $0, 8(%ebp)
        je      .L21           `if (EBP+8) == 0 goto L21`
        movl    8(%ebp), %eax  `EAX = (EBP+8)`
        movl    4(%eax), %eax  `EAX = (EAX+4)`
        cmpl    12(%ebp), %eax 
        jne     .L22           `if (EBP+12) != EAX goto L22`
.L21:
        cmpl    $0, 8(%ebp)    
        setne   %al            `if 0 != (EBP+8) Sets the byte in the AL to 1`
        movzbl  %al, %eax      `EAX = AL (zero ext)`
        popl    %ebp           `POP from stack to EBP (recover it)`
        ret                    `return`

EBP、ESP、EAX 是 32 位寄存器,AL 是 8 位寄存器。

(EBP+8) 是 EBP 地址中的值加上 8 BYTES。

只要跟着它,你就会明白代码,对不起我没有时间,祝你好运!

于 2013-04-27T21:12:14.253 回答