[Solved]Compiled SPI config line is not efficient

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm making a spi.c file and it has the function below and my question is why would the compiler reference r25 ( it's not needed at all ) ?

I'm using #1 & #2 from my sign, opt. -Os, for a m644.

void   spi_clk_set( uint8_t a ){

	SPCR |= ( a & 0x03 );
	SPSR =  ( a & 0x04 ) ? _BV(SPI2X) : 0; 

}

spi.lss

	SPSR =   ( a & 0x04 ) ? _BV(SPI2X) : 0; 

 20e:	90 e0       	ldi	r25, 0x00	; 0
 210:	96 95       	lsr	r25
 212:	87 95       	ror	r24
 214:	96 95       	lsr	r25
 216:	87 95       	ror	r24
 218:	81 70       	andi	r24, 0x01	; 1
 21a:	8d bd       	out	0x2d, r24

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

Last Edited: Mon. May 2, 2011 - 12:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

indianajones11 wrote:
why would the compiler reference r25?
The expression for the conditional operator is promoted to an int. If this bothers you, rewrite the code like this:
void spi_clk_set( uint8_t a ){
   SPCR |= ( a & 0x03 );
   if (a & 0x04)
      a = _BV(SPI2X);
   else
      a = 0;
   SPSR = a;
}

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The expression for the conditional operator is promoted to an int.
Thanks for the understanding, dkinzer. I'll have to review my C books on that !

I used this after I saw what the compiler was doing:

SPSR =   ( ( a >> 2 ) & 0x01 ) ;

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1