That's better, and one less instruction than my answer. Bob ----- Original Message ----- From: "Scott Dattalo" To: Sent: Friday, July 19, 2002 9:48 AM Subject: Re: [PIC]: Code optimization > On Fri, 19 Jul 2002, Bob Ammerman wrote: > > > ----- Original Message ----- > > From: "Scott Dattalo" > > To: > > Sent: Friday, July 19, 2002 9:00 AM > > Subject: Re: [PIC]: Code optimization > > > > > > > On Fri, 19 Jul 2002, Carlos Ojea wrote: > > > > > > > Hello : > > > > > > > > I want to optimize this : > > > > > > > > If number = 2 then var = xxABxxxx > > > > If number = 1 then var = xxxxABxx > > > > If number = 0 then var = xxxxxxAB > > > > > > > > > > > Scott Dattalo's answer: > > > > > If you know that 'number' can only be 0,1,2 then: > > > > > > movlw _AB_ ; assume number == 0 > > > > > > btfsc number,0 ; LSB set? > > > movlw (_AB_ << 2) > > > btfsc number,1 ; Bit 1 set? > > > movlw (_AB_ << 4) > > > > > > ; write the _AB_ bit pattern > > > > > > xorwf var,w ; > > > xorwf var,f > > > > > > > Er, Scott. Not such a good idea. The last two instructions are equivalent > > to: > > > > var = var xor w xor var > > > > which of course is the same as > > > > var = w > > > > which wipes out all the other ('xx') bits in varr > > damn. > > #define _ABABABAB b'01010101 ; or whatever > > movlw 0xfc ; bit 0,1 are low > > btfsc number,0 ; LSB set? > movlw 0xf3 ; bit 2,3 are low > btfsc number,1 ; Bit 1 set? > movlw 0xcf ; bit 4,5 are low > > ; write the _AB_ bit pattern > andwf var,f ; clear the dest bits > xorlw 0xff ; complement the bit mask > andlw _ABABABAB ; select the two desired AB's > iorwf var,f ; write them > > -- > http://www.piclist.com#nomail Going offline? Don't AutoReply us! > email listserv@mitvma.mit.edu with SET PICList DIGEST in the body > > -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body