• AVR Freaks

AnsweredHot!xc8 and if statement

Page: 12 > Showing page 1 of 2
Author
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
2019/10/18 06:21:59 (permalink)
0

xc8 and if statement

Hello,
 
I'm really confused about if statement with multiple conditions. I'm using PIC18F66K22 with PICkit4 and XC8.
Using debugger I set brakepoint before if statement, and pressing F7 i'm following how code is executed.
So, before if statement i see that variables Rx2BuffCount = 0 and Rx2Byte = 0x24
Here is my code:
if (Rx2BuffCount == 0 && Rx2Byte != 0xb5)
            {
                Rx2BuffCount = 0;
            }
            
            if (Rx2BuffCount == 0 && Rx2Byte == 0xb5)
            {
               Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
               Rx2BuffCount++ ; // Increment index
            }
                    
            else
            {
                Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
                Rx2BuffCount++ ; // Increment index
            }

 
 Conditions for first statement is TRUE and code is executed corectly. As variables is not changed , i expeect that conditions 
 for second if statement should be FALSE, and here is surprise - it acts like it TRUE. Pressing F7 i see that next line debugger jumps is Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
 
Then i rewrite my second if statement - i slitted it into two if statements:
 
 
if (Rx2BuffCount == 0 && Rx2Byte != 0xb5)
            {
                Rx2BuffCount = 0;
            }
            
            if (Rx2BuffCount == 0)
            {
                if (Rx2Byte == 0xb5)
                   {
                       Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
                       Rx2BuffCount++ ; // Increment index
                   }
            }
            
            else
            {
                Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
                Rx2BuffCount++ ; // Increment index
            }

 
Now it works as it should be.
My question is - where is my mistake in first code? 
 
Thanks in advance!
 
 
#1
mkwahler
Junior Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2012/10/09 12:27:33
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 10:15:30 (permalink)
0
if ((Rx2BuffCount == 0) && (Rx2Byte != 0xb5))

-Mike
#2
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 10:32:03 (permalink)
0
Hello Mike,
 
thanks for prompt response.
Seems that it's not solution, I have same behavior. Here is code;
 if ((Rx2BuffCount == 0) && (Rx2Byte != 0xb5))
            {
                Rx2BuffCount = 0;
            }
            
            if ((Rx2BuffCount == 0) && (Rx2Byte == 0xb5))
            {
                Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
                Rx2BuffCount++ ; // Increment index
            }
            
            else
            {
                Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
                Rx2BuffCount++ ; // Increment index
            }

 
 
#3
1and0
Access is Denied
  • Total Posts : 9899
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: xc8 and if statement 2019/10/18 10:44:47 (permalink)
+1 (1)
Izseko
Here is my code:
if (Rx2BuffCount == 0 && Rx2Byte != 0xb5)
            {
                Rx2BuffCount = 0;
            }
            
            if (Rx2BuffCount == 0 && Rx2Byte == 0xb5)
            {
               Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
               Rx2BuffCount++ ; // Increment index
            }           
            else
            {
                Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
                Rx2BuffCount++ ; // Increment index
            }

 
 Conditions for first statement is TRUE and code is executed corectly. As variables is not changed , i expeect that conditions 
 for second if statement should be FALSE, and here is surprise - it acts like it TRUE. Pressing F7 i see that next line debugger jumps is Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
...
My question is - where is my mistake in first code? 

The compiler is smarter than you. ;)  It is because the else() clause is IDENTICAL to the if() clause; so the else() probably got optimized out.
 
#4
bitdoctor
Starting Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2019/10/11 06:10:09
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 10:50:57 (permalink)
+1 (1)
In this section, Rx2BuffCount is only set to zero if it already has a value of zero:
if ((Rx2BuffCount == 0) && (Rx2Byte != 0xb5))
            {
                Rx2BuffCount = 0;
            }

If you enter a block knowing for sure that a variable equals zero, setting it to zero is futile.
Thus, the code can never do anything useful, regardless of any tests you make with Rx2Byte. You may as well leave it out. Perhaps you meant to do something else?
 
Here you have an if-else where both the if and the else block contain exactly the same code:
            if (Rx2BuffCount == 0 && Rx2Byte == 0xb5)
            {
               Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
               Rx2BuffCount++ ; // Increment index
            }
            else
            {
                Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
                Rx2BuffCount++ ; // Increment index
            }

The result is that the entire code that you have presented, really only does this:
                Uart2RxBuffer [Rx2BuffCount] = Rx2Byte; // Insert received byte into buffer
                Rx2BuffCount++ ; // Increment index

 No test conditions have any effect on the program execution.
 
 
post edited by bitdoctor - 2019/10/18 11:03:31
#5
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 11:08:27 (permalink)
0
Hello 1and0,
I just tried to remove else clause - yes, your right. Compiler did some optimazing, which is not welcomed from me.
Anyway, right now code is executed as it's written...
 
Thanks to all, problem solved!
 
#6
1and0
Access is Denied
  • Total Posts : 9899
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: xc8 and if statement 2019/10/18 11:15:38 (permalink)
+3 (3)
Izseko
I just tried to remove else clause - yes, your right. Compiler did some optimazing, which is not welcomed from me.

Why? Because it is smarter? ;)
 
#7
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 11:24:11 (permalink)
0
Hello bitdoctor,
 
yes, your right - if and else code looks same, i didn't expect that compiler will optimize it. Seems yy head is blowing up after 12 hours of working...
I have slave device connected to UART, this device is sending data packets with static lenght. Each packed begins with 0xB5(sync byte), so my idea was wait till i receive 0xb5 and only after that start to gather data by inserting in buffer. I mean - if buffer is empty, don't insert any data until it's 0xb5, after that all bytes must be inserted in buffer.
 
Thanks.
 
 
#8
1and0
Access is Denied
  • Total Posts : 9899
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: xc8 and if statement 2019/10/18 11:29:02 (permalink)
+1 (1)
Izseko
if and else code looks same, i didn't expect that compiler will optimize it. 

The compiler will remove useless code, that otherwise will waste code space and cpu cycles.
#9
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 11:41:27 (permalink)
0
1and0
Izseko
I just tried to remove else clause - yes, your right. Compiler did some optimazing, which is not welcomed from me.

Why? Because it is smarter? ;)
 


1and0
Izseko
I just tried to remove else clause - yes, your right. Compiler did some optimazing, which is not welcomed from me.

Why? Because it is smarter? ;)
 


To be honest- i have almost 15 years experience with Microchip controllers, arround 20 developed devices and all of them written in ASM. When you write code in ASM, it will be executed exaclty as it's written. Of course, C is much better, but sometimes it brings unexpected surprises for me, like this was.


#10
1and0
Access is Denied
  • Total Posts : 9899
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: xc8 and if statement 2019/10/18 11:47:48 (permalink)
+1 (1)
Izseko
 
To be honest- i have almost 15 years experience with Microchip controllers, arround 20 developed devices and all of them written in ASM. When you write code in ASM, it will be executed exaclty as it's written. Of course, C is much better, but sometimes it brings unexpected surprises for me, like this was.

I like ASM too.  A word of caution, the C compiler's assembler can and may mess with inline assemble code too, which I do not like either.
 
Anyway, you will learn to appreciate the C optimizer. However, there are times you wish it is even smarter or not so dumb. ;)
 
post edited by 1and0 - 2019/10/18 11:54:53
#11
pcbbc
Super Member
  • Total Posts : 1373
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 11:53:37 (permalink)
+1 (1)
The compiler will find duplicate code, in the tails of if..else.. and switch...case... statements, and probably elsewhere besides, and only create a single copy of the code.
This is just an extreme example of that where the code in tge code paths of both the if and the else are identical.
It’s usually best to turn off compiler optimisations when debugging because the debugger often appears confused and seeming “jumps around” otherwise.
#12
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 12:01:01 (permalink)
0
pcbbc
The compiler will find duplicate code, in the tails of if..else.. and switch...case... statements, and probably elsewhere besides, and only create a single copy of the code.
This is just an extreme example of that where the code in tge code paths of both the if and the else are identical.
It’s usually best to turn off compiler optimisations when debugging because the debugger often appears confused and seeming “jumps around” otherwise.

Hello pcbbc,
Yes, i totally agree with you.
Can you suggest me where can i disable all optimizations? So far i know, i have free verision of XC8, which not supporting code optimization. 
#13
1and0
Access is Denied
  • Total Posts : 9899
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: xc8 and if statement 2019/10/18 12:07:14 (permalink) ☼ Best Answerby Izseko 2019/10/18 12:13:00
+1 (1)
Izseko
Can you suggest me where can i disable all optimizations? So far i know, i have free verision of XC8, which not supporting code optimization. 

In MPLAB X, go to Project Properties (wrench icon in your Dashboard) > XC8 Compiler > Optimizations option > Optimization Level, and set it to 0.
 
Edit: The Free version gives you 2 or 3 levels of optimization.
post edited by 1and0 - 2019/10/18 12:12:23
#14
NKurzman
A Guy on the Net
  • Total Posts : 17939
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: xc8 and if statement 2019/10/18 12:08:52 (permalink)
+1 (1)
You can set the optimization level to zero to eliminate optimization, but your code will be a lot bigger, and slower.
#15
1and0
Access is Denied
  • Total Posts : 9899
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: xc8 and if statement 2019/10/18 12:20:45 (permalink)
+1 (1)
@Izseko, when you've disabled the optimization, take a look at the disassembly code listing and you will think the compiler is an imbecile. mr green: mr green  Then you'll appreciate the optimizer more, with you coming from ASM. ;)
 
#16
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 12:23:06 (permalink)
0
Just checked - I already have optimization level set to zero by default.
Anyway, now i know where these problems comes from.
#17
bitdoctor
Starting Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2019/10/11 06:10:09
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 12:30:15 (permalink)
0
Izseko
Hello bitdoctor,
 
yes, your right - if and else code looks same, i didn't expect that compiler will optimize it. Seems yy head is blowing up after 12 hours of working...
I have slave device connected to UART, this device is sending data packets with static lenght. Each packed begins with 0xB5(sync byte), so my idea was wait till i receive 0xb5 and only after that start to gather data by inserting in buffer. I mean - if buffer is empty, don't insert any data until it's 0xb5, after that all bytes must be inserted in buffer.
 
Thanks.



What if the data packet happens to contain by coincidence some other byte that equals 0xb5? How do you know that all the bytes in the packet have been received?
#18
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 12:37:51 (permalink)
0
1and0
@Izseko, when you've disabled the optimization, take a look at the disassembly code listing and you will think the compiler is an imbecile. mr green: mr green  Then you'll appreciate the optimizer more, with you coming from ASM. ;)
 


I can agree with you  - compiler without optimization will waste most of memory. However, i would preffer some basic information about optimized lines of my code. Like in this time, i didn't noticed that.
It's good that you are keeping alive this forum, because there are lot of you and each can find and point to problem.
Thanks to all of you. 
#19
Izseko
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/12 07:47:06
  • Location: 0
  • Status: offline
Re: xc8 and if statement 2019/10/18 12:43:41 (permalink)
0
bitdoctor
Izseko
Hello bitdoctor,
 
yes, your right - if and else code looks same, i didn't expect that compiler will optimize it. Seems yy head is blowing up after 12 hours of working...
I have slave device connected to UART, this device is sending data packets with static lenght. Each packed begins with 0xB5(sync byte), so my idea was wait till i receive 0xb5 and only after that start to gather data by inserting in buffer. I mean - if buffer is empty, don't insert any data until it's 0xb5, after that all bytes must be inserted in buffer.
 
Thanks.



What if the data packet happens to contain by coincidence some other byte that equals 0xb5? How do you know that all the bytes in the packet have been received?


Actually there are two sync bytes - 0xb5 and 0x62
I know that packed leght is 167 bytes, where last two bytes are CRC, so it's easy to check if all data received corectly.
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5