# Assembly Math

Status
Not open for further replies.

#### Rick C. Hodgin

##### Programmer

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;

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

fstp    answer              // FPU is now empty again
}

return 0;
}``````

--
Rick C. Hodgin

Status
Not open for further replies.

Replies
2
Views
117
Replies
5
Views
89
Replies
0
Views
60
Replies
0
Views
81
Replies
4
Views
88