使用 cmpq 和 je 时出现无限循环
我在每次迭代中递减 RAX。如果 RAX 为零,则程序应更改流程。
# AT&T syntax
start_calc_factorial:
decq %rax
cmpq $0, %rax
je quit_calc_factorial
mulq %rcx
jmp start_calc_factorial
但是,该程序永远不会终止。调试器告诉我 RAX 的值为 0xa0257c7238581842
(它可能下溢,但由于 je
指令,它不应该下溢)。 RAX 的初始值为 7。
这可能是什么问题?
I'm decrementing RAX on each iteration. If RAX is zero, the program should change flow.
# AT&T syntax
start_calc_factorial:
decq %rax
cmpq $0, %rax
je quit_calc_factorial
mulq %rcx
jmp start_calc_factorial
However, the program never terminates. The debugger tells me that RAX has a value of 0xa0257c7238581842
(it probably underflowed, but it shouldn't because of the je
instruction). The initial value of RAX is 7.
What could be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信乘法的结果存储在 RAX 寄存器中,因此这肯定会扰乱循环。
I believe the result of the multiplication is stored in the RAX register, so that would definitely mess up the looping.
问题是您使用相同的寄存器 rax 作为参数和乘积。
您的代码相当于此 C 代码:
它可以循环很长时间(如果不是永远)。
您可能想要的是这样的:
The problem is that you use the same register, rax, as both, the argument and product.
Your code is equivalent to this C code:
It can loop for a long time if not forever.
What you probably want is this: