我正在尝试将此汇编代码翻译为C,我需要帮助。它与while循环有关,但我不知道while循环中会发生什么。我已经看了一段时间,我确定它包含“while(something =!null)”然后做一些事情,但我不知道当代码“移动”到%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;
}
答案 0 :(得分: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。
答案 1 :(得分: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。
按照它,你会理解代码,抱歉,我没有时间,祝你好运!