View previous topic :: View next topic |
Author |
Message |
ggcode Guest
|
Compiler bug?? |
Posted: Wed Nov 25, 2009 5:11 am |
|
|
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
|
|
Posted: Wed Nov 25, 2009 5:52 am |
|
|
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
|
|
Posted: Wed Nov 25, 2009 6:04 am |
|
|
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
|
|
Posted: Wed Nov 25, 2009 6:09 am |
|
|
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
|
|
Posted: Wed Nov 25, 2009 6:18 am |
|
|
Hi,
@ckielstra
You're right, but that is c
And the compilerversion V4.018 does it right
thats my problem i updated to a new compilerversion |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Nov 25, 2009 6:44 am |
|
|
Anonymous wrote: | Hi,
@ckielstra
You're right, but that is c | 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 | 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
|
|
Posted: Wed Nov 25, 2009 6:56 am |
|
|
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
|
|
Posted: Wed Nov 25, 2009 8:21 am |
|
|
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
|
|
Posted: Wed Nov 25, 2009 8:33 am |
|
|
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. |
|
|
|