View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
bit_test improve |
Posted: Wed Jul 13, 2016 2:49 pm |
|
|
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: 1345
|
|
Posted: Wed Jul 13, 2016 4:09 pm |
|
|
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: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Jul 13, 2016 4:53 pm |
|
|
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
|
|
Posted: Wed Jul 13, 2016 6:41 pm |
|
|
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: 19496
|
|
Posted: Thu Jul 14, 2016 12:51 am |
|
|
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
|
|
Posted: Fri Jul 15, 2016 6:09 am |
|
|
@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: 19496
|
|
Posted: Fri Jul 15, 2016 7:57 am |
|
|
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'... |
|
|
|