Inline assembly

For the code below, if I want to convert the for loop to a streaming assembly, how do I do it? (Sorry for the weird code, I just made it.)

1) This is for x86 using visual studio

2) This is a question "how to use it in string assembly", not a question "how to optimize this code"

3) Any other example would be fine. I will think of some better code example in abit.

OK I hope this is the best example:

int doSomething(double a, double b, double c)
{
    double d;
    for(int i=100;i<200;i++)
    {
        d = a*a + b*b + c*c;
        if(d>i)
            return (i-99);
    }
    return -1;
}

      

+2


a source to share


6 answers


He will probably start something like this incomplete and somewhat ineffective example. However, you should demonstrate the syntax.

double doSomething(void) {
    double a=1,b=2,c=3;
    __asm {
        mov ecx,10
loop:
        fld a // a
        fmul st(0),st(0) // aa
        fld b // b aa
        fmul st(0),st(0) // bb aa
        fsubp // aa-bb
        fstp c // c = a*a-b*b

        // and so on

        dec ecx
        jnz loop
    }
    return a+b+c;
}

      

Using SSE instructions would be another option.



The VC ++ inline assembler is described here: http://msdn.microsoft.com/en-us/library/4ks26t93.aspx

Reference guides for Intel processors are located here: http://www.intel.com/products/processor/manuals/

+5


a source


Here's an Inline Assembly Tutorial and another Using Inline Assembly in the C / C ++ Tutorial on CodeProject .



+3


a source


Completely compiler and architecture dependent. You will have to scour the net for information about the built-in asm for your compiler, and then study the opm codes for your architecture (in the correct dialect of asm - compiler-dependent).

+2


a source


You can optimize this without resorting to assembler:

double doSomething(void)
{
    double a = 1.0, b = 2.0, c = 3.0;
    c = a * a - b * b;
    b = c;
    return a + b + c;
}

      

Or, if you increase the optimization level:

double doSomething(void)
{
    return -5.0;
}

      

+1


a source


No inline build in real time. It is not supported for x64 (with Visual Studio, that is). If you are using x64 now or not using it, at some point you will, and inline will be history.

Better learn how to use MASM instead, where what you learn for x86 will still be useful for x64.

+1


a source


Your best option is to force the compiler to print out the assembly language for the function. Use this as a baseline for inline assembly.

In general, inline assembly should be avoided as it is platform specific, especially processor specific. The best solution is to include the function in a separate file. Create a C version and an assembly language version. During the build process, choose between different files. This will allow you to have different versions of assembler for different platforms and processors with minimal side effects to the rest of your program (which is very important).

0


a source







All Articles