Hi Mark. You got the same idea as I got when looked at your first post ten minutes ago :) Shift all carries into temp first. Ok, here is straightforward coding. movfw fileC ;1 subwf fileB,w ;2 rlf temp,F ;3 movfw fileA ;4 subwf fileC,w ;5 rlf temp,F ;6 movfw fileB ;7 subwf fileA,w ;8 Indeed table is simmetrical, so we can reduce it by half Cy temp.1 temp.0 > 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 skpnc ;9 comf temp,F ;10 > A>B | B>C | C>A | Choose > ----+-----+-----+-------- > 0 | 1 | 0 | A > 0 | 1 | 1 | C > 0 | 0 | 1 | B Now let's decrement it by one decf temp,F ;11 > A>B | B>C | C>A | Choose > ----+-----+-----+-------- > 0 | 0 | 1 | A > 0 | 1 | 0 | C > 0 | 0 | 0 | B Here comes the result movfw fileB ;12 btfsc temp.0 ;13 movfw fileA ;14 btfsc temp.0 ;15 movfw fileC ;16 Code hasn't been tested thought and looks like there should be way to squeeze it 4 instructions less :) Guess I'm getting old and lazy :) WBR Dmitry. PS. Scott you always were good at finding bugs :) Mark Rages wrote: > > 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 -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist