What method and how would I do it if I want to do this: Before the code is executed, the content of Num1 to Num4 equal: Num1 = 100 Num2 = 50 Num3 = 200 Num4 = 150 After the code is executed, the content of the 4 resulting numbers are: First_Place = 3 Second_Place = 4 Third_Place = 1 Fourth_Place = 2 The content of First_Place is calculated to be 3 to indicate that Num3 has the highest value. Second_Place equals 4 to indicate that Num4 has the second highest value. I wrote the code for this, it works OK, but it's about 11 pages! So I think the must be a better way to do this. Thank in advance! Thomas >From: Andrew Warren >Reply-To: pic microcontroller discussion list >To: PICLIST@MITVMA.MIT.EDU >Subject: Re: [PIC]: Compare 4 numbers method. Please help! >Date: Sat, 14 Apr 2001 16:00:16 -0700 > >Peter L. Peres wrote: > > > The original question was, to find a routine for min and max of four > > numbers. The bubble sort is one, here is another. > > > > 19 words, no bubble sort, NO STACK USE (can inline). Uses 1 aux. > > register, leaves input data unchanged, output in Fmin and Fmax. Faster > > than Andy Warren's bubble sort ? Expands to more than 4 values to be > > analyzed (set LAST...). > > > > Run time: 20 + (LAST-1) * 14 -> 62T constant run time > > > > (Andy Warren's bubble sort takes a shorter, but variable time, and > > uses the stack - not an option in an ISR on a 14 bit core and NON > > option on 12 bit cores. Avg. time for his ORDER is 5.5 T. His run time > > is: 9.5 + 15 + 16.5 + 2 ~= 43 (+/- 3T) I think). > >Peter: > >Geez... Suddenly, I feel like Jimmy Ringo, Gregory Peck's character >in "The Gunfighter". > >I have no isea what you mean by "uses the stack - not an option in an >ISR on a 14 bit core and NON option on 12 bit cores". The bubble- >sort routine I posted will work perfectly well in either of those >situations. > >In any case, here's a routine that does exactly what yours does, >except that it's smaller and much faster: > >MINMAX: > > MOVF INDF,W ;GRAB THE FIRST VALUE. > MOVWF MAX ;ASSUME IT'LL BE THE MAX. > MOVWF MIN ;ASSUME IT'LL BE THE MIN, TOO. > > MOVLW LAST ;SETUP TO LOOP "LAST" TIMES (NOTE > MOVWF COUNT ;THAT LAST = 3 FOR A 4-BYTE ARRAY). > >LOOP: > > INCF FSR ;POINT AT THE NEXT VALUE. > > MOVF MIN,W ;W = INDF-MIN. > SUBWF INDF,W ; > SKPC ;IF INDF >= MIN, SKIP AHEAD. > ADDWF MIN ;OTHERWISE, MIN = INDF. > > MOVF MAX ;W = INDF - MAX. > SUBWF INDF,W ; > SKPNC ;IF MAX > INDF, SKIP AHEAD. > ADDWF MAX ;OTHERWISE, MAX = INDF. > > DECFSZ COUNT ;IF WE'VE DONE THEM ALL, SKIP AHEAD. > GOTO LOOP ;OTHERWISE, LOOP BACK. > >Size: 16 Words >Run Time: 5 + 12*LAST - 1 = 40 cycles for a 4-byte array. > >Thanks for playing. > >-Andy > > >=== Andrew Warren - fastfwd@ix.netcom.com >=== Fast Forward Engineering - San Diego, California >=== http://www.geocities.com/SiliconValley/2499 > >-- >http://www.piclist.com hint: The PICList is archived three different >ways. See http://www.piclist.com/#archives for details. > > _________________________________________________________________________ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.