e01.org

Inline assembler

In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada.

In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada. include int syscall3(int num, int arg1, int arg2, int arg3) { int res = 0; asm ( "int $0x80" // make the request to the OS : "=a" (res), // return result in eax ("a") "+b" (arg1), // pass arg1 in ebx ("b") [as a "+" output because the syscall may change it] "+c" (arg2), // pass arg2 in ecx ("c") [ditto] "+d" (arg3) // pass arg3 in edx ("d") [ditto] : "a" (num) // pass system call number in eax ("a") : "memory", "cc", // announce to the compiler that the memory and condition codes have been modified "esi", "edi", "ebp" // these registers are clobbered [changed by the syscall] too ); // The operating system will return a negative value on error; // wrappers return -1 on error and set the errno global variable if (-125 // 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 ; // C0 = 1: 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 ; // if (!(fp_status & 0x20)) goto Lret jnp Lret ; // C2 = 1: x is out of range, do argument reduction fldpi ; // load pi fxch ; SC17: fprem1 ; // reminder (partial) fstsw AX ; sahf ; jp SC17 ; // C2 = 1: partial reminder, need to loop fstp ST(1) ; // remove pi from stack jmp SC18 ; } trigerr: return real.nan; Lret: // No need to manually return anything as the value is already on FP stack ; } The followed by conditional jump idiom is used to access the x87 FPU status word bits C0 and C2. stores the status in a general-purpose register; sahf sets the FLAGS register to the higher 8 bits of the register; and the jump is used to judge on whatever flag bit that happens to correspond to the FPU status bit.

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Inline assembler". 1779854287. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1356350399

JSON record · API collection · citation policy