So it seems that most the compilers optimise a 4 bit shift into a swapf/and combo. Which is a good optimisation. I think I shall actually have a play tonight thanks for the insights. Jon > -----Original Message----- > From: Sergio Masci [mailto:smpl@NTLWORLD.COM] > Sent: 14 October 2003 18:02 > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: [PIC:] Efficiency of C? > > > From: Michael Rigby-Jones wrote: > > > > > Not bad, the first two instructions are for bank setting. > I chose a > > 16F877 so a smaller device may require less instructions. > Note that > > the compiler has optimised the last call to OutputNibble by > converting > > it to a jump. However also note that the nibble variable is not > > actually required, a better implementation would be: > > > > void OutputByte( unsigned char w ) > > { > > OutputNibble((unsigned char)(w >> 4)); > > OutputNibble((unsigned char)(w & 0xF)); > > } > > > > giving: > > > > 33 07EE _OutputByte > > 34 ; _w assigned to > ?a_OutputByte+0 > > 35 0000 _OutputByte$w set > ?a_OutputByte > > 36 ;_w stored from w > > 37 07EE 1283 bcf 3,5 > > 38 07EF 1303 bcf 3,6 > > 39 07F0 00A0 movwf ?a_OutputByte > > 41 07F1 0E20 swapf ?a_OutputByte,w > > 42 07F2 390F andlw 15 > > 43 07F3 27E9 fcall _OutputNibble > > 45 07F4 0820 movf ?a_OutputByte,w > > 46 07F5 390F andlw 15 > > 47 07F6 2FE9 ljmp _OutputNibble > > > > Now apart from the bank setting bits which weren't included in your > > assembly example, this has exactly the same number of instructions, > > but actually executes more quickly due to the jump > optimisation. The > > ljmp macro degenerates to a single goto if the OutputNibble > resides in > > the same program memory page as the OutputByte function. > > > > Hope that calms your fears over the code generation quality > of HiTech > > :o) > > > > Mike > > > The following shows equivalent code compiled using the XCSB > compiler. Note the absence of call and return instructions. > > Admittedly the total of 12 instructions generated could be > further reduced to 8 by a key hole optimiser but none the > less still not bad going for a high level language generating > code for a 16 series PIC > > //---------------------- > //---------------------- > > include "hwreg-p16f628.h" > > proc inline OutputNibble(ubyte val) > > PORTA = val > endproc > > proc main() > > ubyte x > > OutputNibble(x >> 4) > OutputNibble(x & 0x0f) > > endproc > > //---------------------- > //---------------------- > > 0069 main > > ;;; OutputNibble ( > x_main_func_local >> 4 ) > 0069 08 22 f:22 movf x_main_func_local,w > 006A 00 A7 f:27 movwf > xacc_2_main_func_eval_stk > > 006B 0E A7 f:27 swapf > xacc_2_main_func_eval_stk > 006C 30 0F movlw 15 > 006D 05 A7 f:27 andwf > xacc_2_main_func_eval_stk > > ;;; PORTA = xacc_2_main_func_eval_stk > 006E 08 27 f:27 movf > xacc_2_main_func_eval_stk,w > 006F 00 85 f:05 movwf PORTA > > ;;; OutputNibble ( > x_main_func_local & 15 ) > 0070 08 22 f:22 movf x_main_func_local,w > 0071 39 0F andlw 15 > 0072 00 A7 f:27 movwf > xacc_2_main_func_eval_stk > > ;;; PORTA = xacc_2_main_func_eval_stk > 0073 08 27 f:27 movf > xacc_2_main_func_eval_stk,w > 0074 00 85 f:05 movwf PORTA > > 0075 main_func_exit_point > > > Regards > Sergio Masci > http://www.xcprod.com/titan/XCSB - optimising structured PIC BASIC compiler -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads