使用 cmpq 和 je 时出现无限循环

发布于 2024-12-18 19:33:49 字数 336 浏览 6 评论 0原文

我在每次迭代中递减 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-12-25 19:33:49

我相信乘法的结果存储在 RAX 寄存器中,因此这肯定会扰乱循环。

I believe the result of the multiplication is stored in the RAX register, so that would definitely mess up the looping.

¢好甜 2024-12-25 19:33:49

问题是您使用相同的寄存器 rax 作为参数和乘积。
您的代码相当于此 C 代码:

while (1)
{
  rax = rax - 1;
  if (rax == 0) break;
  rax = rax * rcx;
}

它可以循环很长时间(如果不是永远)。

您可能想要的是这样的:

while (1)
{
  rcx = rcx - 1;
  if (rcx == 0) break;
  rax = rax * rcx;
}

The problem is that you use the same register, rax, as both, the argument and product.
Your code is equivalent to this C code:

while (1)
{
  rax = rax - 1;
  if (rax == 0) break;
  rax = rax * rcx;
}

It can loop for a long time if not forever.

What you probably want is this:

while (1)
{
  rcx = rcx - 1;
  if (rcx == 0) break;
  rax = rax * rcx;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文