×
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

Assembly Math

Assembly Math

Assembly Math

(OP)
I came across this old thread that hadn't been answered: thread272-1806629: Assembly, math.

The equation is a constant, so it's just plugging in hard numbers. Using Visual Studio 98, and an assembly block, you can see the code here.

Note: The FPU operates using a stack. It's been designed for this type of formula operation. As you can see, you load things on the stack, and then perform some operation. Everything retains its relative position. If you look at it in Visual Studio you can enable the FPU registers and see how it operates. It's quite a slick piece of hardware, and greatly under-utilized. It has some abilities to do transcendental operations that make it powerful. In one of my 3D OpenGL apps, I used the FPU extensively (rather than SSE2 or later) because I used lots of transcendental functions. It performs wholly adequately, but is limited to non-parallel operations. It still has great utility though, especially for these kinds of operations.

CODE

//                  12 - 9        3 * 2
// Equation:  y = ----------  -  -------  *  (12 / 5)
//                (16-9) / 6      2 + 3
//
// Since this equation will result in a non-integer,
// floating point is needed.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
    float sixteen, twelve, nine, six, five, three, two;
    float answer;

    sixteen = 16.0f;
    twelve  = 12.0f;
    nine    =  9.0f;
    six     =  6.0f;
    five    =  5.0f;
    three   =  3.0f;
    two     =  2.0f;

    // The FPU has a stack with register st(0) thru st(7)
    _asm
    {
        finit                       // Initialize the FPU

        // Compute 12 - 9
        fld     twelve              // st0 = 12
        fld     nine                // st0 = 9, st1 = 12
        fsubp   st(1), st(0)        // st0 = (12 - 9)

        // Compute (16 - 9)
        fld     sixteen             // st0 = 16, st1 = (12 - 9)
        fld     nine                // st0 = 9, st1 = 16, st2 = (12 - 9)
        fsubp   st(1), st(0)        // st0 = (16 - 9), st1 = (12 - 9)

        // Compute (16 - 9) / 6
        fld     six                 // st0 = 6, st1 = (16 - 9), st2 = (12 - 9)
        fdivp   st(1), st(0)        // st0 = (16 - 9) / 6, st1 = (12 - 9)

        // Compute (12 - 9) / ((16 - 9) / 6)
        // We'll call this f1 for formula1
        fdivp   st(1), st(0)        // st0 = f1 = (12 - 9) / ((16 - 9) / 6)

        // Compute 3 * 2
        fld     three               // st0 = 3, st1 = f1
        fld     two                 // st0 = 2, st1 = 3, st2 = f1
        fmulp   st(1), st(0)        // st0 = 3 * 2, st1 = f1

        // Compute 2 + 3
        fld     two                 // st0 = 2, st1 = 3 * 2, st1 = f1
        fld     three               // st0 = 3, st1 = 2, st2 = 3 * 2, st1 = f1
        faddp   st(1), st(0)        // st0 = 2 + 3, st1 = 3 * 2, st2 = f1

        // Compute (3 * 2) / (2 + 3)
        // We'll call this f2 for formula2
        fdivp   st(1), st(0)        // st0 = f2, st1 = f1

        // Compute (12 / 5)
        fld     twelve              // st0 = 12, st1 = f2, st2 = f1
        fld     five                // st0 = 5, st1 = 12, st2 = f2, st3 = f1
        fdivp   st(1), st(0)        // st0 = 12 / 5, st1 = f2, st2 = f1

        // Compute f2 * (12 / 5)
        // We'll call this t1 for temporary result 1
        fmulp   st(1), st(0)        // st0 = t1 = f2 * (12 / 5), st1 = f1

        // Compute f1 - t1
        fsubp   st(1), st(0)        // st0 = the answer

        // Store the answer
        fstp    answer              // FPU is now empty again
    }

    printf("The answer is %f\n", answer);
    return 0;
} 

--
Rick C. Hodgin

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