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

Code generation issue

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



Joined: 18 Jul 2006
Posts: 92
Location: Iasi, Romania

View user's profile Send private message

Code generation issue
PostPosted: Wed May 09, 2007 2:19 am     Reply with quote

I have de following declarations:
Code:
struct
{
    int8 Dummy;
    int8 a[20];
} str;

int8 *x;
To get in x a pointer from one element from a, i could do
Code:
void Main()
{
    int8 i = 7;
    x = &str.a[i];
}
which generates:
Code:
...................     int8 i = 7;
0024:  MOVLW  07
0026:  MOVWF  1D
....................     x = &str.a[i];
0028:  CLRF   03
002A:  MOVF   1D,W
002C:  ADDLW  01
002E:  MOVWF  01
0030:  MOVLW  00
0032:  ADDWFC 03,F
0034:  MOVF   01,W
0036:  ADDLW  06
0038:  MOVWF  1B
003A:  MOVLW  00
003C:  ADDWFC 03,W
003E:  MOVWF  1C


or:
Code:
void Main()
{
    int8 i = 7;
    x = str.a + i;
}
which generates
Code:
....................     int8 i = 7;
0024:  MOVLW  07
0026:  MOVWF  1D
....................     x = str.a + i;
0028:  ADDWF  1D,W
002A:  MOVWF  1B
002C:  CLRF   1C
002E:  BTFSC  FD8.0
0030:  INCF   1C,F


Since both C statements are the same, why genereted code is not?
Ttelmah
Guest







PostPosted: Wed May 09, 2007 3:06 am     Reply with quote

This is very common, and fairly 'classic' with compilers.
You will almost certainly find the effects will change if you change the #opt setting.
Basically, in the second case, you say to take the address 'str.a' (which is 'hardwired' at compile time), and add 'i' to this.
In the first you say to access the array element, and then get the address of this. The arithmetic is done in this case, by working out the offset needed for 'i' in the array, then adding this to the array starting point. Just slightly more complex.
The optimiser, will try to improve this sort of behaviour, but it is the price you 'pay' for the convenience of being able to access elements without having to do the arithmetic yourself...

Best Wishes
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