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

For loop compiler bug

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



Joined: 17 May 2006
Posts: 9

View user's profile Send private message

For loop compiler bug
PostPosted: Wed May 17, 2006 1:17 pm     Reply with quote

I'm having a problem with a particular for loop in one of my programs and I think it is a problem with the compiler. The for loop is supposed to count backwards from 4 to 0; however what ends up happening is the program gets stuck in an infinite loop with the integer variable counting down, overflowing, then counting down again, etc.

I've included source code that demonstrates this problem. I have not tried it on another PIC. I have also included the assembly generated when I compile this program. If someone could try this code and confirm or deny my results that would be much appreciated.

Code:
#include <18F4620.h>
#device ICD=TRUE
#device adc=8

#fuses HS,WDT8,NOPROTECT,IESO,NOPUT,NOCPD,STVREN,NOLVP,NOWRT,NOWRTD,NOEBTR
#fuses NOCPB,NOEBTRB,NOWRTC,NOWRTB,MCLR

#use delay(clock=20000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=COM1,ERRORS,restart_wdt)

#include <stdio.h>

void main()
{
   int x=0;

   for (x=4; x>=0; x--)
   {
      printf("x = %d\r\n",x);
   }
     
   printf("\r\nDone\r\n");
}


Code:
.................... void main()
.................... {
013A:  CLRF   FF8
013C:  BCF    FD0.7
013E:  CLRF   FEA
0140:  CLRF   FE9
0142:  BSF    FB8.3
0144:  MOVLW  56
0146:  MOVWF  FAF
0148:  MOVLW  00
014A:  MOVWF  FB0
014C:  MOVLW  26
014E:  MOVWF  FAC
0150:  MOVLW  90
0152:  MOVWF  FAB
0154:  MOVF   FC1,W
0156:  ANDLW  C0
0158:  IORLW  0F
015A:  MOVWF  FC1
015C:  MOVLW  07
015E:  MOVWF  FB4
0160:  CLRF   05
0162:  CLRF   06
0164:  CLRF   07
....................    int x=0;
0166:  CLRF   08
....................
....................    for (x=4; x>=0; x--)
0168:  MOVLW  04
016A:  MOVWF  08
....................    {
....................       printf("x = %d\r\n",x);
016C:  CLRF   09
016E:  MOVF   09,W
0170:  RCALL  0004
0172:  INCF   09,F
0174:  MOVWF  00
0176:  CLRWDT
0178:  BTFSS  F9E.4
017A:  BRA    0176
017C:  MOVWF  FAD
017E:  MOVLW  04
0180:  SUBWF  09,W
0182:  BNZ   016E
0184:  MOVFF  08,0A
0188:  MOVLW  1F
018A:  MOVWF  0B
018C:  BRA    0060
018E:  MOVLW  0D
0190:  CLRWDT
0192:  BTFSS  F9E.4
0194:  BRA    0190
0196:  MOVWF  FAD
0198:  MOVLW  0A
019A:  CLRWDT
019C:  BTFSS  F9E.4
019E:  BRA    019A
01A0:  MOVWF  FAD
....................    }
01A2:  DECF   08,F
01A4:  BRA    016C
....................
....................    printf("\r\nDone\r\n");
01A6:  CLRF   09
01A8:  MOVF   09,W
01AA:  RCALL  001E
01AC:  IORLW  00
01AE:  BZ    01BC
01B0:  INCF   09,F
01B2:  CLRWDT
01B4:  BTFSS  F9E.4
01B6:  BRA    01B2
01B8:  MOVWF  FAD
01BA:  BRA    01A8
.................... }
01BC:  BRA    01BC
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Wed May 17, 2006 1:26 pm     Reply with quote

Hate to rain on your parade, but your for loop is doing exactly what you're asking it to.

You declare x as an unsigned integer. In the loop you set x = 4, and decrement x on every pass through the loop. Your test for the continuation of the loop is to test if x is greater than or equal to 0.

Let's say that on a particular pass through the loop, x = 0. Since 0 is equal to 0, the loop executes. Once it executes, x is decremented and becomes 255. 255 is indeed greater than 0, so the loop continues.

Change your definition of x to:

Code:
signed int8 x;


It will perform as you expect with this change.
jleblanc



Joined: 17 May 2006
Posts: 9

View user's profile Send private message

PostPosted: Wed May 17, 2006 1:27 pm     Reply with quote

Addendum:

I noticed what was wrong. I needed to add the "signed" keyword infront of x's declaration. However what I find puzzling is how without this keyword the application would count negative numbers when it ran (which if "int" is unsigned by default it shouldn't do).

It was only when I tried setting x equal to negative 2 to do a test of the ">=" operator and the compiler flaged that as not in the valid range for that variable that I caught this.

So it appears a variable declared only as "int" exhibits properties of both the signed and unsigned types.

Edit: Wait, I just realized that was probably the printf() call interpretting the numbers as signed because of the %d.

Nevermind; nothing to see here. Move along.
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