Jump to content
Sign in to follow this  
ian1scott

Problem with switch and if(n==8)...

Recommended Posts

I have not been able to find a workaround to this apparent bug.  (seems to be in 5.2.0 and 5.2.2)  Please tell me I am doing something very silly!??!?!?

 

The following code:

>--snip

if (c==8){

LCD_send_byte(0, 0x10); // backspace

} else if (c==10){

>--snip

 

compiles as:

>---snip

;;;;;;;;{

;;;;;;;; if (c==8){

btfss param00_LCD_putc, D'3'

goto label_0020

;;;;;;;; LCD_send_byte(0, 0x10); // backspace

clrf param00_LCD_send_byte

movlw D'16'

movwf param01_LCD_send_byte

call _LCD_send_byte

;;;;;;;;

;;;;;;;; } else if (c==10){

goto label_0021

label_0020

movlw D'10'

subwf param00_LCD_putc, W

btfss STATUS, Z

goto label_0022

>---snip

 

 

Not sure if you can see what is going on, but it correctly does a movlw and a subwf when I am testing for (c==10), but when I do a test for (c==8) all it does is check for bit3 being set (btfss param00_LCD_putc, D'3') - it should be treating it the same as the c==10 condition.

 

I originally tried to do a switch..case statement, but it generated the same code.

 

Does anybody know a workaround for this?

 

 ???

 

Ian

Share this post


Link to post
Share on other sites
Guest ian1scott

OK, I have created a whole wee test program to try to demonstrate the problem I was seeing.  It looks like I may have turned something else up, too.  Let me first (attempt to) summarize the issues I have seen, then I will post the code to allow you to replicate it.  Incidentally, these problems may only be arising while building under Win2k - I do not have access to any other machine at the moment, but I will try this same code in Win98 when I get home tonight.

 

Replication steps:

1.  Create an empty project with no source files and headers.

2.  Paste the code below in to a source file and add the file to the project.

3.  Build the project using the default options.

 

Here are the problems I see:

1.  The \a \b \t \n \v \f and \r characters are being interpreted as a, b, t, n, v, f and r (i.e. the \ is being ignored)

2.  When the switch statement is being evaluated for the power of 2 bit position values (i.e. 1, 2, 4, 8, 16 etc) only the bit position itself is being evaluated.  i.e. the if the value is 6, a test is made to see  if bit 1 is set, and if it is, the '2' case is executed.  It never  makes it down to the '6' case.

 

As I said, I will keep digging around with this.  I have a sneaky suspicion that this may only happen when built under win2k.

 

TIA.

 

Ian

 

testc(char c)  
{
switch(c){
 case	'\0'	: delay_ms(1);
       break;
 case	0x00	: delay_ms(1);
       break;
 case	'\1'	: delay_ms(1);
       break;
 case	0x01	: delay_ms(1);
       break;
 case	'\2'	: delay_ms(1);
       break;
 case	0x02	: delay_ms(1);
       break;
 case	'\3'	: delay_ms(1);
       break;
 case	0x03	: delay_ms(1);
       break;
 case	'\4'	: delay_ms(1);
       break;
 case	0x04	: delay_ms(1);
       break;
 case	'\5'	: delay_ms(1);
       break;
 case	0x05	: delay_ms(1);
       break;
 case	'\6'	: delay_ms(1);
       break;
 case	0x06	: delay_ms(1);
       break;
 case	'\a'	: delay_ms(1);
       break;
 case	0x07	: delay_ms(1);
       break;
 case  '\b'	: delay_ms(1);  
       break;
 case	0x08	: delay_ms(1);
       break;
 case	'\t'	: delay_ms(1);	
        break;
 case	0x09	: delay_ms(1);
       break;
 case	'\n'	: delay_ms(1);	
        break;
 case	0x0a	: delay_ms(1);
       break;
 case	'\v'	: delay_ms(1);	
        break;
 case	0x0b	: delay_ms(1);
       break;
 case	'\f'	: delay_ms(1);	
        break;
 case	0x0c	: delay_ms(1);
       break;
 case	'\r'	: delay_ms(1);	
        break;
 case	0x0d	: delay_ms(1);
       break;
 default 	 : delay_ms(1);
       break;
}

}

void main()
{
char n = 0;

while(1){
 testc(n++);
}

}

Share this post


Link to post
Share on other sites
Guest Pavel
Not sure if you can see what is going on, but it correctly does a movlw and a subwf when I am testing for (c==10), but when I do a test for (c==8) all it does is check for bit3 being set (btfss param00_LCD_putc, D'3') - it should be treating it the same as the c==10 condition.

I can not reproduce the problem. The code that I tried:

#include <system.h>

char c, a;

main()
{
   if (c==8)
   {
       a = 0;
   }
   else if (c==10)
   {
       a = 1;
   }
   while(1);
}

results in correct assembly. Please try again using the latest compiler 5.2.2. If you still see the problem create a project that exposes the problem and has as little code as possible, zip it and send to support@picant.com

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...