需要一些帮助将汇编代码转换为 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/