|
|
View previous topic :: View next topic |
Author |
Message |
djpark
Joined: 02 Mar 2004 Posts: 49
|
Help needed: int or int8 doesn't compare < 0 correctly? |
Posted: Thu Apr 08, 2004 9:02 pm |
|
|
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
|
|
Posted: Thu Apr 08, 2004 9:33 pm |
|
|
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:
|
|
|
djpark
Joined: 02 Mar 2004 Posts: 49
|
|
Posted: Thu Apr 08, 2004 9:47 pm |
|
|
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:
|
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 |
|
|
|
|
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
|