Scott Dattalo wrote: >Tony Try this: >Since it's 12 instructions and uncommented it must be optimal :) Well yes that works to :) And here is Scotts code with comments: ;******************************************************************* ; CHECK_W_BCD_DIGIT_CLAMP - Checks if the value in w is bcd and that is ; if within the range <=3D MinBCD >=3D MaxBCD, if not bcd or outside = range the value ; will be clamped to either MaxBCD or MinBCD. ; Input:=20 ; W - The value to test/clamp ; MinBCD - Constant BCD,minimum accepted value, range 0x00-0x98 (must be = less than MaxBCD) ; MaxBCD - Constant BCD,maximum accepted value, range 0x01-0x99 (must be = higher than MinBCD) ; Temp - An temp file register in *current* bank or access ram ;=20 ; Output: ; W - The clamped value of input (or Input if input is within range and = BCD)=20 ; Originator: Scott Dattalo ;=20 CHECK_W_BCD_DIGIT_CLAMP MACRO MinBCD,MaxBCD,Temp local L1 MOVWF Temp ; save input ADDLW 0x66 ; w =3D input+0x66 to test BCD conformity (i.e = input<=3D 0x99) MOVLW MaxBCD ; w =3D MaxBCD SKPC ; c =3D input.h > 9 SKPNDC ; dc =3D input.l > 9 bra L1 ; exit with MaxBCD in w as input was not BCD CPFSGT Temp ; Compare input with MaxBCD MOVF Temp,W ; input <=3D MaxBCD, w =3D input MOVWF Temp ; Temp =3D input if input was <=3D MaxBCD=20 ; OR Temp =3D MaxBCD if input > MaxBCD MOVLW MinBCD ; w =3D MinBCD CPFSLT Temp ; compare input with MinBCD MOVF Temp,W ; input >=3D MinBCD, w =3D temp ; w =3D input if MinBCD<=3Dinput<=3DMaxBCD ; else if input>MaxBCD, w =3D MaxBCD ; else w =3D MinBCD (input