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

struct problem.

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








struct problem.
PostPosted: Wed Mar 04, 2009 3:57 pm     Reply with quote

Hi

CCS: 4.083 / PIC:18xx2455

There are some problem in the asm, of this simple struct.
But the code compile and run, but the asm look real funny.

Can anybody explain what going on here?

(not production code just for testing!)

Code:
void st() {
 int16 t;
 int8 c;
 
 struct{
  int16 v1;
  int16 v2;
 }x1[10];
 
 c=1;
 x1[c].v1=t;//why is this faster than the other?
 x1[c].v2=t;//why is this so complicated?
 
 x1[2].v1=t;//ok
 x1[2].v2=t;//ok
}

Code:
0CB0:  MOVLW  01
0CB2:  MOVWF  c
....................  x1[c].v1=t;
0CB4:  MOVF   c,W
0CB6:  MULLW  04
0CB8:  MOVF   PRODL,W
0CBA:  CLRF   @@xAC
0CBC:  MOVWF  @@xAB
0CBE:  MOVLW  x1
0CC0:  ADDWF  @@xAB
0CC2:  MOVWF  FSR0L
0CC4:  MOVLW  x1>>8
0CC6:  ADDWFC @@xAC,W
0CC8:  MOVWF  FSR0H
0CCA:  MOVFF  t+1,PREINC0
0CCE:  MOVF   POSTDEC0,F
0CD0:  MOVFF  t,INDF0
....................  x1[c].v2=t;
0CD4:  MOVF   c,W
0CD6:  MULLW  04
0CD8:  MOVF   PRODL,W
0CDA:  CLRF   @@xAC
0CDC:  MOVWF  @@xAB
0CDE:  MOVLW  02
0CE0:  ADDWF  @@xAB,W
0CE2:  MOVWF  @01
0CE4:  MOVLW  00
0CE6:  ADDWFC @@xAC,W
0CE8:  MOVWF  @03
0CEA:  MOVF   @01,W
0CEC:  ADDLW  x1
0CEE:  MOVWF  FSR0L
0CF0:  MOVLW  x1>>8
0CF2:  ADDWFC @03,W
0CF4:  MOVWF  FSR0H
0CF6:  MOVFF  t+1,PREINC0
0CFA:  MOVF   POSTDEC0,F
0CFC:  MOVFF  t,INDF0
....................   
....................  x1[2].v1=t;
0D00:  MOVFF  t+1,x1+9
0D04:  MOVFF  t,x1+8
....................  x1[2].v2=t;
0D08:  MOVFF  t+1,x1+11
0D0C:  MOVFF  t,x1+10
.................... }
0D10:  GOTO   263E (RETURN)
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Wed Mar 04, 2009 10:15 pm     Reply with quote

The 'c' in x1[c] is a variable, and the compiler needs to generate code to calculate the address of x1[c].

Then the compiler needs to add to that address the offset of the struct items. For v1, it's at the start of the struct and presumably has a zero offset. That can be optimised away. v2 is not at the start of the struct, so extra code to add its offset is required.

When working with x1[2], the addresses can be calculated at compile time, and used directly in the code.
_________________
Andrew
Guest








PostPosted: Thu Mar 05, 2009 1:37 am     Reply with quote

Hi

If changing to another chip 16xx the code is real clean. I dont have the 4.084 but in the release list I can see that some 18xx problem is solved.
Seoman
Guest







PostPosted: Thu Mar 05, 2009 10:55 am     Reply with quote

I change some code, i would like to use a string inside the structure

Code:
void st() {
 int16 t;
 int8 c;
 
 struct{
  int16 v1;
  int16 v2;
  char v3[10];
 }x1[10];
 
 c=1;
 x1[c].v1=t;//why is this faster than the other?
 x1[c].v2=t;//why is this so complicated?
 
 x1[2].v1=t;//ok
 x1[2].v2=t;//ok
 x1[2].v3="Hello";  //Error: Expecting LVALUE such as a variable name or *expression 

Seem that the compiler don't recognize v3 Why?

Thanks


}
Guest








PostPosted: Thu Mar 05, 2009 10:59 am     Reply with quote

My Compiler is 4.065

sorry
Ttelmah
Guest







PostPosted: Thu Mar 05, 2009 10:59 am     Reply with quote

Nothing to do with the structure. You need to use strcpy, to copy strings. C can automatically move 'simple' types (numbers etc.), but strings are more complex, and can't be copied with a simple '='.

Best Wishes
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Mar 05, 2009 11:00 am     Reply with quote

Try
sprintf(x1[2].v3, "Hello"); or
strcpy(x1[2].v3, "Hello");

You can only do a char array assignment at declaration of the var
char mystr[10] = "hello";
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