Scott Dattalo wrote: > movf fileB,W > subwf fileA,W ; W=A-B > movf fileB,W ; assume B is larger > skpnc ; if Carry is clear then B is larger > movf fileA,W ; carry is set, so A is larger (or equal) > > movwf temp ;temp = max(A,B) > > subwf fileC,W ; compare C, W = C-max(A,B) > movf temp,W ; assume max(A,B) > C > skpnc ; > movf fileC,W ; C is larger than the max(A,B) > > ; W = max(A,B,C) Scott: You can do it quicker... Here's the "min" code from my web page; it's easily modified to compute max: MOVF NUM1,W ;Set the Carry flag if NUM1 is SUBWF NUM2,W ;less than NUM2. MOVF NUM1,W ;Assume that NUM1 was less than ;NUM2. This instruction does ;NOT affect the Carry flag. SKPC ;If NUM1 really was less than ;NUM2, skip ahead with W = NUM1. MOVF NUM2,W ;Otherwise, W = NUM2. ;At this point, W contains min(NUM1,NUM2). MOVWF RESULT ;Store it in RESULT. ;At this point, both W and RESULT contain min(NUM1,NUM2). SUBWF NUM3,W ;Set the Carry flag if W [which ;still contains min(NUM1,NUM2)] ;is less then NUM3. ;At this point, RESULT still holds min(NUM1,NUM2), ;but W now contains NUM3 - min(NUM1,NUM2). SKPC ;If W really was less than NUM3, ;skip ahead. ADDWF RESULT ;Otherwise, ;RESULT=RESULT + W ; =min(N1,N2) + W ; =min(N1,N2) + [N3-min(N1,N2)] ; =N3. ; ;At this point, RESULT holds min(NUM3,min(NUM1,NUM2)), ;which is of course equivalent to min(NUM1,NUM2,NUM3). Of course, we're supposed to find the median rather than min or max, so how about this? MOVF REGA,W ;Copy from REGA-REGC to MOVWF TEMP1 ;TEMP1, TEMP2, and MEDIAN. MOVF REGB,W ; MOVWF TEMP2 ; MOVF REGC,W ; MOVWF MEDIAN ; SUBWF TEMP2,W ;TEMP2 >= MEDIAN? BC $+3 ;If so, skip ahead. ADDWF MEDIAN ;Otherwise, swap MEDIAN and SUBWF TEMP2 ;TEMP2. MOVF TEMP1,W ;MEDIAN >= TEMP1? SUBWF MEDIAN,W ; SKPC ;If so, skip ahead. SUBWF MEDIAN ;Otherwise, MEDIAN = TEMP1. MOVF MEDIAN,W ;TEMP2 >= MEDIAN? SUBWF TEMP2,W ; SKPC ;If so, skip ahead. ADDWF MEDIAN ;Otherwise, MEDIAN = TEMP2. 19 words, 18 or 19 cycles. -Andy === Andrew Warren - fastfwd@ix.netcom.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist