Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding average between three numbers

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hey guys
how do you find the average between three numbers in assembly language. here is what i have done so far: if you can help, i would be appreciate it very much. thanks

void Find( int *Address, int Number, int *Maximum, int * Minimum, double *Average) {

int Min, Max;
asm{
mov Min, 0x7FFF // Assign largest possible value
mov Max, 0x8000 // Assign smallest possible value

mov bx, word ptr Address // Offset address of array to search Number of

mov cx, word ptr Number // elements in array.
} // end asm

Loop:
asm{ // Loop around the array looking for Min and Max
mov ax, [bx] // Get next array element
add bx, 2 // Point to next element, ready for next time around loop

// Replace Min, if element smaller than current Min
cmp ax, Min
jge MaxTest
mov Min, ax

// Replace Max if element greater than current Max
MaxTest:
cmp ax, Max
jle Finish
mov Max, ax

Finish: // Loop back if more elements left
dec cx
jnz Loop

// Put Min answer into contents pointed to by Minimum
mov bx, word ptr Minimum
mov ax, Min
mov [bx], ax

// Put Max answer into contents pointed to by Maximum
mov bx, word ptr Maximum
mov ax, Max
mov [bx], ax

// put average answer into contents pointed to by average
mov bx, word ptr Average
mov ax, average
mov [bx], ax

} // end asm statement

} /*end of function Find*/
 
Point 1:
If you're gonna use a C compiler for something like this, you might as well use C. Unless this is time critical-code.

Point 2:
Be warned that the 'Average' variable is of type (double *) or pointer to double.
mov bx, word ptr Average
mov ax, average
mov [bx], ax
won't complain, but you're not gonna get the right value out of it.

Point 3:
Both the FP unit and the integer unit have division commands: FDIV and DIV. You can use these commands to divide by three. An average, after all, is the sum divided by the number of elements summed, so if you have 3 numbers to average just add them all up and divide. Since you want a type 'double' you need to use FDIV. Just load in the sum using FILD, load in a 3 (put a 'double const three=3.0; somewhere, then FLD three), then FDIV. 'Course if it's time intensive code you might want to forgo using floats altogether.

"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top