c - 汇编代码到C代码

标签 c assembly x86

我正在尝试将此汇编代码翻译成 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/

相关文章:

c - string是数组还是指针?

c - 如何使用 zlib 在 C 中压缩并在 golang 中解压缩

c - 以秒为单位的朱利安时间戳的差异

assembly - 递归过程

c# - 如何重新编译 C# 程序以在 ARM CPU 上运行?

c++ - 逆向工程的汇编语言

c - Ncurses 读取小键盘键和转义

linux - 在 Windows 上的 64 位 Ubuntu 上编译 32 位程序集

python - 如何从x86asm/x64asm调用python2.5函数?

assembly - 我如何在 Linux 64 位上从 C 编写简单的内联 asm 指令?