Skip to main content
Explorer II
March 4, 2013
Question

Cosmic Compiler Bug

  • March 4, 2013
  • 2 replies
  • 658 views
Posted on March 04, 2013 at 16:33

I'm using Cosmic Eval C Compiler V 4.3.7.

The code of concern is as follows:

if( RST->SR & (u8)0x02 == (u8)0x02)

{....}

I'm simply checking bit 1 of the reset register. I checked the disassembler because the program was not working as expected and this is what I found:

main.c:215  if( RST->SR & (u8)0x02 == (u8)0x02){

0x9272 <.ckResetForIWDG+12> 0xC650B3        LD    A,0x50b3            LD    A,0x50b3

0x9275 <.ckResetForIWDG+15> 0xA501          BCP   A,#0x01             BCP   A,#0x01

0x9277 <.ckResetForIWDG+17> 0x275F          JREQ  0x92d8 

.....}

This is clearly why my program will not work as expected, the disassembler tells me that I'm checking bit 0 not bit 1! Am I missing something?
    This topic has been closed for replies.

    2 replies

    Explorer II
    March 4, 2013
    Posted on March 04, 2013 at 19:32

    I found a work around by using a temp variable to store the result of the AND and compare using the temp variable. However, as far as I know my original bit of code was legal C... 

    Visitor II
    March 5, 2013
    Posted on March 05, 2013 at 10:40

    Hello,

    the problem is in the source code: as it is written, since the operator == has higher priority than the operator &, the compiler understands it as

    if( RST->SR & ( (u8)0x02 == (u8)0x02) )

    whereas what you wanted to write is

    if(  (RST->SR &  (u8)0x02) == (u8)0x02 )

    which is usually written simply as

    if( RST->SR & (u8)0x02  )

    Regards.