Inline assembler
Inline assembler is a feature of programming languages, that enables very low level code written in assembler to be embedded in a high level language like C.
This allows programmers to optimise a very performance-sensitive algorithm by sending individual commands to the computer's CPU.
This example of inline assembler is from the D programming language and computes the tangent of x using the x86's FPU instructions.
Example
// Compute the tangent of x
real tan(real x)
{
asm
{
fld x[EBP] ; // load x
fxam ; // test for oddball values
fstsw AX ;
sahf ;
jc trigerr ; // x is NAN, infinity, or empty
// 387's can handle denormals
SC18: fptan ;
fstp ST(0) ; // dump X, which is always 1
fstsw AX ;
sahf ;
jnp Lret ; // C2 = 1 (x is out of range)
// Do argument reduction to bring x into range
fldpi ;
fxch ;
SC17: fprem1 ;
fstsw AX ;
sahf ;
jp SC17 ;
fstp ST(1) ; // remove pi from stack
jmp SC18 ;
}
trigerr:
return real.nan;
Lret:
;
}
Categories: Stub