On 7/15/05, Mark Rages wrote: > I'll post my solution in a minute, once I'm satisfied it works. Well, I think it's working and it's quitting time, so here goes: I started by listing all the possibilities: A > B > C choose B A > C > B choose C B > A > C choose A B > C > A choose C C > A > B choose A C > B > A choose B I notice that there are really only three comparisons, if we ingnore equality. (After all, if A=B, it doesn't matter which I choose.) So I made a mapping from the comparison result to the choice: A>B | B>C | C>A | Choose ----+-----+-----+-------- 1 | 1 | 0 | B 1 | 0 | 0 | C 0 | 1 | 0 | A 0 | 1 | 1 | C 1 | 0 | 1 | A 0 | 0 | 1 | B Or, in decimal and numerical order: comparison | choose ------------+-------- 0 | X 1 | B 2 | A 3 | C 4 | C 5 | A 6 | B 7 | X But there's an interesting symmetry here: 3..0 is the same as 4..7. This will simplify my selection code. Here's my code. I used one temp register for the comparison result. gteq macro fileA,fileB movfw fileB ; w=B subwf fileA,w ; w=A-B ;; if B>A we borrowed, so _borrow=0 ;; ... _borrow=1 means A>B or A==B ;; Tack the result on the right of the comparison register. rlf iris_adc_temp,f ; '1'-> A>=B endm abc_median macro A,B,C local done clrf comparison_temp ; do comparisons gteq A,B gteq B,C gteq C,A ;; fold around 4 movfw comparison_temp btfsc comparison_temp,2 ; is >=4? sublw 0x07 ; yes. 4->3,5->2,6->1,7->0 skpnz ; handle special case A=B=C movlw 0x01 ; in that case just choose any! movwf comparison_temp ; Now select from A,B,C like this: ;; 1 -> B ;; 2 -> A ;; 3 -> C movfw A btfss comparison_temp,0 ; this bit clear: comparison_temp=2 goto done movfw B btfss comparison_temp,1 ; this bit clear: comparison_temp=1 goto done movfw C done endm -- You think that it is a secret, but it never has been one. - fortune cookie -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist