At 14:20 05/11/1997 -0500, you wrote: >On Wed, 5 Nov 1997 13:33:20 +0000 "Frank A. Vorstenbosch" > writes: >>Hi there, >> >>I thought I'd post my LDK.I include file here, which I use to load >>a constant in a register without trashing W. > >Umm, how about: > movwf r ;r=W (cap. W is old W) > xorlw K ;w=K^W > xorwf r,f ;r = W^(K^W) = K > xorlw K ;w= K^(K^W) = W > >This works for any K, always taking 4 cycles. You do have to be sure >that an interrupt won't tamper with r during the process. > >Another neat xor trick involves what I'll call "merging" bits. Say you >have two registers > >A = AAAAAXXX >B = XXXXXBBB > >and you want to copy the 5 high bits of A into the high 5 bits of B, >leaving the 3 B bits the same and ignoring all the X bits. At the end >B=AAAAABBB. Ordinarily this would be something like: > > movlw b'00000111' > andwf B,f ;Guarantee high bits of B are 0. > movfw A ;Get A > andlw b'11111000' ;Keep only high bits of A > iorwf B,f ;OR the two together AAAAABBB > >But you can do this: > movfw A > xorwf B,w > andlw b'11111000' > xorwf B,f >which is even more trick if you happen to have the A bits in W already >(then the movfw A isn't needed). In the conventional mask and OR method >it's hard to mask the B bits without messing up the A bits in W. > > Hi, I use another xor trick for detecting the rising or falling edge on a input port movf port,w xorwf oldport,w ; port ^ oldport xorwf oldport,f ; oldport = oldport ^ oldport ^ port = p ort andwf oldport,w ; the rising edge or comf port,w xorwf comoldport,w ; w = comport ^ comoldport xorwf comoldport,f ; comoldport = comport andwf comoldport,w ; w = falling edge Jean-Louis VERN