将程序集转换为 C

标签 c assembly x86

需要一些帮助将汇编代码转换为 C。据我了解,这是一个带有条件 (a < c) 的 while 循环,但我不理解 while 循环的主体。

 movl $0, -8(%ebp) # variable B is at ebp - 8
 movl $0, -4(%ebp) # variable A is at ebp - 4
 jmp .L3
.L2
 movl 8(%ebp), %eax # parameter C is at ebp + 8
 addl $2, %eax
 addl %eax, %eax
 addl %eax, -8(%ebp)
 addl $1, -4(%ebp)
 .L3
 movl -4(%ebp), %eax
 cmpl 8(%ebp), %eax
 jl .L2

同时解释一下你为什么这么做,谢谢。

这是我目前所得到的

int a,b = 0;

while (a < c) {
     c += 4 + 2*c;
     a++;
}

如果我做对了所有这些,那么我唯一不明白的就是这条线

addl %eax, -8(%ebp)

最佳答案

addl %eax, -8(%ebp) 会将 eax 中的值添加到存储在 ebp-8 中的值。如果你能理解其他的添加指令,那就一样了。没有 add 4 指令,所以我不知道如何获得表达式 4 + 2*c

 movl $0, -8(%ebp)   # B = 0
 movl $0, -4(%ebp)   # A = 0
 jmp .L3
.L2
 movl 8(%ebp), %eax  # eax = C
 addl $2, %eax       # eax = C + 2
 addl %eax, %eax     # eax *= 2
 addl %eax, -8(%ebp) # B += eax
 addl $1, -4(%ebp)   # A++
 .L3
 movl -4(%ebp), %eax
 cmpl 8(%ebp), %eax
 jl .L2

所以结果如下

int a, b = 0;

while (a < c) {
     b += (c + 2)*2;
     a++;
}

这很简单

int a = c, b = c*(c+2)*2;

关于将程序集转换为 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19800748/

相关文章:

c - 读取复杂的二进制文件格式

c - 创建汇编程序的好方法?

x86 - shuffle 和 permute 有什么区别

python - 为什么python中的字符串比较如此之快?

c - 在 32 位 Controller 上访问 SFR(特殊功能寄存器)

c - GCC vs CLANG 指向 char* 优化的指针

assembly - assembly 中的斐波那契实现给出了意想不到的结果

从 C 调用 Nasm 函数但返回 -nan

memory-management - 为什么段从段落边界开始?

c++ - 指向二维数组的指针 C、C++