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 CCS Technical Support

Compiler bug??

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







Compiler bug??
PostPosted: Wed Nov 25, 2009 5:11 am     Reply with quote

Hello,
I 've got a general question whether I misinterprets the facts.
The code below will create a software PWM.

Code:

int1 VariableA
int8 VariableB
int8 VariableC = 127;

  VariableA = --VariableB <= VariableC;


VariableC is compared with VariableB, then VariableB is decremented and then VariableA assigned.

This make the CCS compiler:
Code:

       BCF 0x7, 0x3      //<-- reset VariableA  thats wrong????
       DECF 0x33, F
       MOVF 0x33, W
       SUBWF 0x34, W
       BTFSC 0x3, 0
       BSF 0x7, 0x3      //<-- set VariableA



That's wrong?

Compilerversion PCM V4.097

thanks for answers

ggcode
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 25, 2009 5:52 am     Reply with quote

Placing the -- before the variable name will decrement the variable before it is used. Placing the -- after the var will decrement it after it is used!

Clearing A before the comparison just removes an extra branch.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Nov 25, 2009 6:04 am     Reply with quote

Code:
VariableA = --VariableB <= VariableC;
This is terrible code!
It is correct C syntax but difficult to understand, why not use a few more lines? Possibly you save a few bytes, but most modern compilers are smart enough to optimise. And what is a few bytes if you spend hours extra in debugging?

The correct version which decrements VariableB after the comparison could then look like:
Code:
if (VariableB <= VariableC)
   VariableA = TRUE;
else
   VariableA = FALSE;
VariableB--;
Guest








PostPosted: Wed Nov 25, 2009 6:09 am     Reply with quote

Hi Wayne_,
thanks for answere.


Quote:

Placing the -- before the variable name will decrement the variable before it is used. Placing the -- after the var will decrement it after it is used!


That s clear , but i've written wrong.

But why is the VariableA be reset to 0?
If the compare of VariableB and VariableC 'TRUE' then the VariableA should also stay on 'TRUE'.

ggcode
Guest








PostPosted: Wed Nov 25, 2009 6:18 am     Reply with quote

Hi,
@ckielstra
You're right, but that is c Very Happy

And the compilerversion V4.018 does it right Shocked

thats my problem i updated to a new compilerversion Crying or Very sad
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Nov 25, 2009 6:44 am     Reply with quote

Anonymous wrote:
Hi,
@ckielstra
You're right, but that is c Very Happy
No, this is not depending on the C language but on your programming style. In every programming language you can write ugly code. It is up to you to keep it readable.

Quote:
But why is the VariableA be reset to 0?
If the compare of VariableB and VariableC 'TRUE' then the VariableA should also stay on 'TRUE'.
The disassembly code as shown is compiled correct and the error is in your C-code as explained by Wayne.
You get confused in the assembly code because of the compiler optimizations. What the compiler is doing is:
Code:
VariableA = 0;
if (comparison)
   VariableA = 1;
this is identical to:
Code:
if (comparison)
   VariableA = 1;
else
  VariableA = 0;
but the first version saves a few bytes.

Quote:
And the compilerversion V4.018 does it right Shocked
v4.018 is an old and a very bad version. The first v4.0xx compiler versions were for testing the new features only. Starting around v4.070 the compiler became usable.
Fix the error as indicated by Wayne, or even better, use my suggested code.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 25, 2009 6:56 am     Reply with quote

Code:

int1 VariableA
int8 VariableB
int8 VariableC = 127;

  VariableA = --VariableB <= VariableC;

Code:

       BCF 0x7, 0x3      // Set VarA to false (Saves doing it later)
       DECF 0x33, F      // Dec VarB (Correct)
       MOVF 0x33, W    // Place VarB in W
       SUBWF 0x34, W  // Sub VarW from F(VarC) store in W
       BTFSC 0x3, 0     // Check C Flag (Status) Skip if varB > VarC
       BSF 0x7, 0x3      //<-- set VariableA True (If VarB <= VarC)


Not sure about the BTFSC as I don't know what PIC you are using but it looks OK to me.
Guest








PostPosted: Wed Nov 25, 2009 8:21 am     Reply with quote

Hi,
thanks for answer. I have it change to your code and it works.

But this code works with compiler version 3.1xx ...3.2xx !!!!!

The problem appeared in versionupdate from 4.018 to 4.096

ggcode
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 25, 2009 8:33 am     Reply with quote

Anonymous wrote:
Hi,
thanks for answer. I have it change to your code and it works.

But this code works with compiler version 3.1xx ...3.2xx !!!!!

The problem appeared in versionupdate from 4.018 to 4.096

ggcode


As stated I think the problem was actually in the older versions and was fixed in 4.018 upwards.
Using pre/post appended operators is standard C.
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