View Full Version : Assembly Averaging

01-30-2007, 09:53 AM
I have 5 numbers in 5 RES variables.


How do I toss the high and low values, then average ?

Mike Green
01-30-2007, 10:54 AM
Define new RES variables "MaxVal", "MinVal", and "Total" and do:

mov MaxVal,A1 ' This can be made into a loop with
mov MinVal,A1 ' any number of variables, but this
mov Total,A1 ' is simpler when only a few variables
add Total,A2 ' are used
min MaxVal,A2
max MinVal,A2
add Total,A3
min MaxVal,A3
max MinVal,A3
add Total,A4
min MaxVal,A4 ' Note that this wouldn't necessarily
max MinVal,A4 ' be what you want if two or more
add Total,A5 ' values were the high or low values
min MaxVal,A5
max MinVal,A5
sub Total,MaxVal ' Toss the high and low values
sub Total,MinVal ' and divide by 3 to get average
mov MaxVal,#3 ' Reuse MaxVal and MinVal since
shl MaxVal,#14 ' they're not needed further
mov MinVal,#16
:divide cmpsub Total,MaxVal wc
rcl Total,#1 ' See document called Propeller Guts
djnz MinVal,#:divide ' Result is left in "Total"

Post Edited (Mike Green) : 3/5/2007 6:49:02 PM GMT

03-05-2007, 11:58 PM
Your brain sees problems differently than mine does. It takes me 10 minutes to read
something like this before it clicks.

Sorry I didn't get back to you on this sooner.



Mike Green
03-06-2007, 12:23 AM
There are a couple of things going on there
1) Accumulating a total of the points
2) Determining the maximum of the points
3) Determining the minimum of the points
4) At the end: Divide the total by the # of points
1) At the beginning, we initialize MaxVal, MinVal, and Total to the 1st value
2) For each subsequent point (A2 - A5), we add it into the Total, use the max
instruction to determine the minimum value (I know it seems backwards,
but there it is ... It does work this way ... Look carefully at the instruction
description) and the min instruction to determine the maximum value.
3) There's a simple division by a constant (the number of points). Note that
I messed up with the constant value. It should be 5, not 3.

Lord Steve
03-06-2007, 01:43 AM
Mike Green said...
I messed up with the constant value. It should be 5, not 3.

Did I miss something? If you have 5 values and you toss the minimum and maximum values...you have 3 left over. So, the average of 3 values is the sum divided by...3, not 5.

Mike Green
03-06-2007, 01:48 AM
Lord Steve,
Thanks. You're absolutely correct. I forgot about the "tossing the max and min values". Oh well, I guess I'm more confused than usual today.