Search code examples
assemblyx86-64decrementcmp

Infinite loop when using cmpq and je


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?


Solution

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