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 support@ccsinfo.com

Strange variable reaction

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



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

Strange variable reaction
PostPosted: Wed Aug 25, 2004 4:58 pm     Reply with quote

I have a section of my code that is acting a bit qirky.

This is just a code snippet, of course.
Code:

#define MAX_TRAVEL 201000

unsigned int32 travel = 0;//global
unsigned int32 distance = 0;//global
unsigned int16 shimmy = 400;//global

main()
{
unsigned int32 skoot = 0;

  skoot = MAX_TRAVEL;
  travel = skoot - shimmy;//  <-- this assignment works correctly

  travel = MAX_TRAVEL - shimmy;//  <-- this assignment does NOT work correctly

  while(1)
    ;
}


Maybe somebody can tell me why one statement works and why one doesn't.

Compiler ver. 3.206
PIC18F452

Here's a snippet of the LST file for each statement. I don't know assembly and I'm really not too excited to learn it either. Wink

Code:

....................          travel = skoot - shimmy;
1A8C:  MOVF   7C,W
1A8E:  SUBWF  x9D,W
1A90:  MOVWF  70
1A92:  MOVF   7D,W
1A94:  SUBWFB x9E,W
1A96:  MOVWF  71
1A98:  MOVLW  00
1A9A:  SUBWFB x9F,W
1A9C:  MOVWF  72
1A9E:  MOVLW  00
1AA0:  SUBWFB xA0,W
1AA2:  MOVWF  73

....................          travel = MAX_TRAVEL - shimmy;
1A8C:  MOVLW  28
1A8E:  BSF    FD8.0
1A90:  SUBFWB 7C,W
1A92:  MOVWF  70
1A94:  MOVLW  11
1A96:  SUBFWB 7D,W
1A98:  MOVWF  71
1A9A:  MOVLW  00
1A9C:  SUBWFB 03,W
1A9E:  MOVWF  72
1AA0:  MOVLW  00
1AA2:  SUBWFB 00,W
1AA4:  MOVWF  73



I haven't tried to find out what the actual value is being assigned for the statement that doesn't work, I just know that the statement that compares it:

if(distance > travel)// and so on....

never enters the if() statement.

Anybody enlighten me?

Ronald
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 25, 2004 6:01 pm     Reply with quote

I was able to make it work by explicitly casting the 16-bit variable
to 32-bits. I tested this with PCH vs. 3.188. The changes are
shown in bold, below.

travel = MAX_TRAVEL - (int32)shimmy;
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