我正在尝试将此汇编代码翻译成 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;
}
最佳答案
评论程序集的作用:
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
现在你有了一个结构定义和一个原型(prototype),所以这最终是:
int whilecode(Node *somenode, int data)
{
while (somenode != 0 && somenode->data != data)
somenode = somenode->ptr2;
return somenode != 0;
}
在链表中搜索包含特定数据值的节点,如果找到则返回 true。
关于c - 汇编代码到C代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16256854/