Signed Right Shift (>>)Signed Right Shift in PIC16 AssemblyThe value 0x80 = -128 in two’s complement.Recall that right shift <strong>is</strong> same as divide by two. Then-128 >> 1 == -128 / 2 == -64 == 0xC0If unsigned right shift <strong>is</strong> performed (0 shifted into MSB), then0x80 >> 1 == 0x40 == +64 , the wrong answer!!When doing a signed right shift, the MSB must be kept thesame value (th<strong>is</strong> <strong>is</strong> also known as an arithmetic right shift).Makes sense, dividing a negative number by 2 should notchange the sign!!b7 b6 b5 b4 b3 b2 b1 b0C codesigned char i;i >> 1;Set carry to besame as sign bitbefore shift.PIC16 assembly;; i >> 1bcf STATUS,C ;clear carrybtfsc i,7 ;sign bit =1?bsf STATUS,C ;set carryrrf i ;i >> 1V 0.1 19V 0.1 20Signed Left Shift ( j true if i <strong>is</strong> to left of j,need for -i and +j to swap places if want to useunsigned compar<strong>is</strong>on0xFF2560x80128-1280x7f127+127-1 -i +jSwap sides of -i, +j by complementing sign bits0x00000x0000V 0.1 23PIC16: Signed Compar<strong>is</strong>on Algorithm #1Steps for i > j signed compar<strong>is</strong>onoperand adjustment1. Load j into w reg.2. Complement w sign bit by XOR’ing with 0x80.3. Store in temporary location (you pick), call th<strong>is</strong> temp.4. Load i into w reg.5. Complement w sign bit by XOR’ing with 0x80.6. Subtract w from temp (new j -newi).Unsigned compare7. If carry = 0, then i > jV 0.1 244
C codesigned char i,j;if (i > j) {i = i + j;}/* do stuff */Must use temporarylocation because donot want to modifyj!!!! Requires 8instructions, notSigned Compare (>, Alg #1)PIC16movf j,w ; w ← jxorlw 0x80 ; toggle j sign bitmovwf temp ; save new jmovf i,w ; w ← ixorlw 0x80 ; toggle i sign bitsubwf temp,w ; w← new j-new ibtfsc STATUS,Cgoto skip ;C=1, j>= iifbodymovf j,waddwf i,f ;i = i + jskip;; do stuff...PIC16: Signed Compar<strong>is</strong>on, Algorithm #2Steps for i > j signed compar<strong>is</strong>oncheck if signs equal1. Load j into w reg.2. XOR w with i, store in w3. If bit 7 = 0, then both signs the same. Goto 5, dounsigned compare.4. If bit 7 of j = 1, then j <strong>is</strong> negative, so i > j!5. Load i into w reg6. Subtract w from j (j - i).Unsigned compare7. If carry = 0, then i > jcounting if body.V 0.1 25V 0.1 26signed char i,j;if (i > j) {i = i + j;}/* do stuff */Signed Compare (>, Alg. #2)PIC16C code movf j,w ; w ← jDoes not needtemporary location,but requires moreinstructions (11 <strong>vs</strong>. 8)xorwf i,w ; w ← j ^ ibtfss WREG,7goto docmp ; unsigned cmpbtfss j,7 ; check j signgoto skip ; j pos, so i < jgoto ifbody ; j neg, so i > jdocmpmovf i,w ; w ← <strong>is</strong>ubwf j,w ; w← j- ibtfsc STATUS,Cgoto skip ;C=1, j>= iifbodymovf j,waddwf i,f ;i = i + jskip;; V do 0.1 stuff...27PIC18 Signed CompareThe PIC18 added two additional flagsV (overflow flag), set on two’s complement overflowN (negative flag), set if MSB = 1 after operationAlso added branches based on single flag conditions:bc (branch if carry), bnc (branch if not carry)bov (branch on overflow), bnov (branch if no overflow)bn (branch if negative), bnn (branch if not negative)bz (branch if zero), bnz (branch if not zero)bra (branch always)A branch functions as a conditional goto, will d<strong>is</strong>cuss exactoperation later.V 0.1 28Using N,V flags for Signed CompareTo compare i > j, perform j – iAfter j-i, if V = 0 (correct result, no overflow)if N=1 (result negative) then i > jelse N=0 (answer positive) so j >= iAfter j-i, if V = 1 (incorrect result)if N=0 (result positive) then i > jelse N=1 (result negative) so j >= iMost processors have unsigned compare instructions (operatefrom Z, C flags) and signed compare instructions (operate from Z,N, V flags). The PIC18 only has unsigned compare instructions(cpfsgt, cpfslt) but does have the V,N and branches based on theseflags. The PIC16 only has Z,C flags and no dedicated comparePIC18 Signed Compare (Assembly)signed char i,j;if (i > j) {i = i + j;}/* do stuff */instructions. V 0.1 29V 0.1 30C codeDoes not need atemporary location,requires 6 instructionsoutside of if body.PIC18movf i,w ; w ← <strong>is</strong>ubwf j,w ; w ← j - ib<strong>vs</strong> v_1bnn skip ; V=0,N=0 j>=ibra ifbody; V=0,N=1v_1bn skip ; V=1, N=1 j>=iifbody ;V=1,N=0movf j,waddwf i,f ;i = i + jskip;; do stuff...5