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;
开发者_如何学Python if(d>i)
return (i-99);
}
return -1;
}
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/
Here's an Inline Assembly tutorial and another Using Inline Assembly in C/C++ tutorial on CodeProject.
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).
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;
}
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.
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).
精彩评论