This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Older C compiler bug

I ran across a compiler bug in fairly recent renditions of the C51 compiler.

    unsigned char i,j;
    for (i=5; i<11; i--) j += i;

Rather than walking the indexes 5,4,...0, this code goes 5,4,...0,0xff,0xfe...0xb. That's because the assembly generated for the end test is
    CJNE  R7,#0BH,?loop

Similar code is generated for an integer test against a constant. Correct code is generated when the end test is against a variable instead of a constant, or if the increment is not monotonically decreasing (e.g., i -= 2).

I observed this in versions 6.14 and 7.01 of the C51 compiler. It does seem to be working correctly in 7.10 and 7.50, so a fix occurred sometime in there. A quick search of the knowledge base didn't turn up any references to this bug, so I decided to post it.

Caveat coder!

Zig