CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Help needed: int or int8 doesn't compare < 0 correctly?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
djpark



Joined: 02 Mar 2004
Posts: 49

View user's profile Send private message

Help needed: int or int8 doesn't compare < 0 correctly?
PostPosted: Thu Apr 08, 2004 9:02 pm     Reply with quote

I am desperately seeking help for the code below using PCM 3.188 for 16F648A.

When i becomes negative (-127 ~ -1) I intend to add MAX_INDEX, but that part of the code is not executed and from the asm/list below, it doesn't seem to be compiled in at all.

Am I missing something such as the way how to declare a signed int variable or PCM has a bug?

-- dj

Code:
#separate
int8 get_index(int t)
{
   int8 i;

   i = rf_index - t + 1;
   if (i < 0)
   {
      i += MAX_INDEX;
   }
   return i;
}


Code:
0000                02473 .................... #separate
0000                02474 .................... int8 get_index(int t)
0000                02475 .................... {
0000                02476 ....................    int8 i;
0000                02477 ....................
0000                02478 ....................    i = rf_index - t + 1;
071B 087E       02479 MOVF   7E,W
071C 0261       02480 SUBWF  61,W
071D 3E01       02481 ADDLW  01
071E 1683       02482 BSF    03.5
071F 00A0       02483 MOVWF  20
0000                02484 ....................    if (i < 0)
0000                02485 ....................    {
0000                02486 ....................       i += MAX_INDEX;
0000                02487 ....................    }
0000                02488 ....................    return i;
0720 0820       02489 MOVF   20,W
0721 00F8       02490 MOVWF  78
0000                02491 .................... }
0722 1283       02492 BCF    03.5
0723 118A       02493 BCF    0A.3
0724 2F28       02494 GOTO   728 (RETURN)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 08, 2004 9:33 pm     Reply with quote

Quote:
Am I missing something such as the way how to declare a signed
int variable or PCM has a bug?

The problem is that older versions of the CCS manual had this
vital sentence in them:
Quote:
All types, except float, by default are unsigned; however,
they may be preceded by 'unsigned' or 'signed'.

Recent versions do not have it. I have emailed CCS about the
importance of putting that sentence back in, but they haven't
done it yet.

To declare a variable as signed, you have to do this:
Code:
signed int8 value;
djpark



Joined: 02 Mar 2004
Posts: 49

View user's profile Send private message

PostPosted: Thu Apr 08, 2004 9:47 pm     Reply with quote

PCM programmer wrote:
Quote:
All types, except float, by default are unsigned; however,
they may be preceded by 'unsigned' or 'signed'.


To declare a variable as signed, you have to do this:
Code:
signed int8 value;


Thanks a lot for the quick reply.

How strange CCS handles the default. From other C programming experience, I never expected that int default to unsigned int.

Till I got your answer, I had to use bit_test to check the sign bit.

Code:
        //if (i < 0)
        if (bit_test(i, 7))


Now I am back to the normal comparison syntax, thanks to you!

-- dj
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group