tags:

views:

180

answers:

6
+2  Q: 

In-line assembly

For the below code, if i want to convert the for loop to in-line assembly, how would it be done? (Pardon the weird code, i just made it up.)

1) This is for the x86, using visual studio

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

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

OK i hope this is a better 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;
}
+1  A: 

You can optimise 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 turn up the optimisation level:

double doSomething(void)
{
    return -5.0;
}
Paul R
Why the down-vote ? With no comment ? It's a perfectly valid answer to the question (as originally posed).
Paul R
If someone wanted to learn how to calculate the value of pi to 5 decimal places, it wouldn't be overly helpful to answer "3.14159" unless they could come back to you for a new answer when they decided they wanted 10 or 100 decimal places. Teach a man to fish...
Dinah
@Dinah: well there actually is a serious point here, which is often overlooked, and that is that you need to optimise at a high level before resorting to micro-optimisations such as hand-coded assembler (which should always be a last resort). Also, the question as originally posed lacked detail and context, so it wasn't clear what the asker *really* was trying to do. The trigger-happy down-voters among us so often miss such subtle points.
Paul R
@Paul R: I have now looked at the original post and still disagree that this is a helpful answer. (For the record though it wasn't me who downvoted you)
Dinah
@Dinah: OK - let's agree to differ then - I'll probably delete this answer shortly as it no longer applies to the question in its present form.
Paul R
+2  A: 

Totally dependent upon compiler and architecture. You'll need to scour the web for information on inline asm for your compiler and then learn the asm op codes for your architecture (in the correct asm dialect -- compiler dependent).

Noah Roberts
+4  A: 

It would probably start out something like this incomplete and somewhat inefficient example. Should demonstrate the syntax, though.

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 documented here: http://msdn.microsoft.com/en-us/library/4ks26t93.aspx

The Intel processor reference manuals are here: http://www.intel.com/products/processor/manuals/

brone
Nice answer. +1
Heath Hunnicutt
+3  A: 

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

Dinah
+1  A: 

No real point learning inline assembly. Its not supported for x64 (with Visual Studio, that is). Whether you are using x64 now or not using it, at some point you will be and inline will be history.

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

Stephen Kellett
I've seen a lot of "what's the point" arguments online lately. I applaud you putting forth a better way to do things in case the OP wasn't aware of them, but regarding "what's the point" -- who cares what the "point" is? I didn't even begin programming with a "point" in mind. I did it because it was fun and interesting. Most of my hobby programs don't have a "point" either, they're just something I want to do. I created an esoteric programming language once that did very little. What was the point? Because I wanted to.
Dinah
A: 

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

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

Thomas Matthews