iwanttofly
iwanttofly

Reputation: 21

g++ compiled assembly results in an infinite loop?

I've been looking into the compiled assembly from GNU's g++ compiler and it looks like it results in an infinite loop (partial output of g++ -S file.c -o out):

.L3:
        movq    array2(%rip), %rax
        movq    array1(%rip), %rdx
        movq    -8(%rbp), %rcx
        salq    $3, %rcx
        addq    %rcx, %rdx
        movq    (%rdx), %rdx
        andl    $1, %edx
        salq    $3, %rdx
        addq    %rdx, %rax
        movq    (%rax), %rdx
        movq    temp(%rip), %rax
        andq    %rdx, %rax
        movq    %rax, temp(%rip)
        subq    $1, -8(%rbp)
        jmp     .L3

All of these are either movq or computation instructions aside from the last jmp instruction, but this just brings us back to .L3. This is the body of the following code:

          ull i; // unsigned long long int
          for (i = x - 1; i >= 0; i--)
               temp &= array2[array1[i] & 1];

How is it exiting the loop? It looks like it just decreases i (subq $1, -8(%rbp)) and restarts without a comparison.

Upvotes: 0

Views: 70

Answers (1)

Acorn
Acorn

Reputation: 26166

Assuming ull is an unsigned long long, the loop never ends, since:

i >= 0

is always true.

Note: g++ reports this problem when compiling with warnings enabled.

Upvotes: 4

Related Questions