×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

finding average between three numbers

finding average between three numbers

finding average between three numbers

(OP)
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*/

RE: finding average between three numbers

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."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close