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

bit_test improve

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



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

bit_test improve
PostPosted: Wed Jul 13, 2016 2:49 pm     Reply with quote

Hi, I'm trying to make the code more C portable; first I have this code

Code:

   if(bit_test(cmd_TS[0],7))
   {
      Data_H
   }   
   else
   {
      Data_L
   }   


And now I'm trying to replace it by more C look alike code

Code:

   if((cmd_TS[0]&&0x80)==0x80)
   {
      Data_H
   }   
   else
   {
      Data_L
   }   


But that result in a weird ASM code and I don't understand why.
Could somebody tell me what I'm doing wrong?

Second; I want to rotate through carry an array; in ASM is very easy, but in C I just get stuck.

Code:

   #asm
      rlcf cmd_TS[15]
      rlcf cmd_TS[14]
      rlcf cmd_TS[13]
      rlcf cmd_TS[12]
      rlcf cmd_TS[11]
      rlcf cmd_TS[10]
      rlcf cmd_TS[9]
      rlcf cmd_TS[8]
      rlcf cmd_TS[7]
      rlcf cmd_TS[6]
      rlcf cmd_TS[5]
      rlcf cmd_TS[4]
      rlcf cmd_TS[3]
      rlcf cmd_TS[2]
      rlcf cmd_TS[1]
      rlcf cmd_TS[0]
      #endasm


I already read some examples but nothing simple, all the methods includes loops and huge lines of codes.
There isn't a simple way to do it as in ASM? That's just weird.
_________________
Electric Blue
jeremiah



Joined: 20 Jul 2010
Posts: 1328

View user's profile Send private message

PostPosted: Wed Jul 13, 2016 4:09 pm     Reply with quote

The && is for *logical* AND. The & is for *bitwise* AND. That will change what type of code you output.
temtronic



Joined: 01 Jul 2010
Posts: 9170
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jul 13, 2016 4:53 pm     Reply with quote

re: rotate...

were you aware that CCS C has the built-in function...

rotate_right (address, bytes)

as for 'portability', assuming you're using PICs and CCS C , I'd be using the CCS functions as most of them produce 'tight' code. Making 'portable' code can result in 'generic', cumbersome code that while it looks 'right' in the C source it actually creates a messy, hard to follow asm code.


Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 13, 2016 6:41 pm     Reply with quote

In addition to what Jeremiah said, in an if() statement, any non-zero
value is "True". So, it can be shortened to:
Code:
if((cmd_TS[0] & 0x80))
  {

This test generates only two lines of ASM code with the PCH compiler.
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Thu Jul 14, 2016 12:51 am     Reply with quote

You can also make 'bit_test' portable.
Code:

#if (!defined(__PCM__) && !defined(__PCH__) && !defined(__PCB__))
#define bit_test(a,b) (a & (1<<b))!=0
#endif


Then if you compile the code on another compiler, bit_test will be defined for you....

However 'super portability', is never possible for 'low level' code.

This macro makes an immediate assumption about bit ordering, so won't work on processors where this is not the same.
Bit_test, and bit_set/clear, exist because the PIC itself has specific low level instructions to perform these operations, and are allowing you to take advantage of these. If you move to a different processor, it will have it's own abilities and shortcomings, and code will have to be modified to suit this if you want to use the chip properly.
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

PostPosted: Fri Jul 15, 2016 6:09 am     Reply with quote

@jeremiah Thanks, I have that clear now.

@temtronic No, I wasn't aware of those built-in functions, thanks for the info.

@PCM programmer Thanks for the tip.

@Ttelmah I did not know that #define could do that, I thought that only replace text.
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Fri Jul 15, 2016 7:57 am     Reply with quote

Every bit of code, starts off as text!.....

You can do a lot with #define. However as with most things 'C', some parts can be quite 'obtuse'... Very Happy
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