Author Topic: inline asm using header  (Read 3004 times)

0 Members and 1 Guest are viewing this topic.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
inline asm using header
« on: June 25, 2020, 09:19:49 am »
some very simple examples of gcc inline asm --Windows 64-bit
c_include.h
Code: [Select]
void subpi(double *x)
{
asm (
"fldpi \n"
"movq %[x], %%rax \n"
"fstpl (%%rax) \n"
:[x]"=m"(x)
:
:"rax"
);
}

double funpi(void)
{
double x;
asm (
"fldpi \n"
"leaq %[x], %%rax \n"
"fstpl (%%rax) \n"
:[x]"=m"(x)
:
:"rax"
);
return x;
}

__attribute__((naked)) double naked_funln2(void)
{
asm (
"fldln2 \n"
"fstpl -16(%rsp) \n"
"movsd -16(%rsp), %xmm0 \n"
"ret"
);
}

double funadd(double *x, double *y)
{
double z;
asm (
"movq %[x], %%rax \n"
"fldl (%%rax) \n"
"movq %[y], %%rax \n"
"faddl (%%rax) \n"
"leaq %[z], %%rax \n"
"fstpl (%%rax) \n"
:[x]"=m"(x), [y]"=m"(y), [z]"=m"(z)
:
:"rax"
);
return z;
}

__attribute__((naked)) double naked_funadd(double *x, double *y)
{
asm (
"fldl (%rcx) \n"
"faddl (%rdx) \n"
"fstpl -16(%rsp) \n"
"movsd -16(%rsp), %xmm0 \n"
"ret"
);
}
test_inline_asm.bas
Code: [Select]
' c_include.h needs to be in QB64 folder
DECLARE CUSTOMTYPE LIBRARY ".\c_include"
    SUB subpi (x AS DOUBLE)
    FUNCTION funpi# ()
    FUNCTION naked_funln2# ()
    FUNCTION funadd# (x AS DOUBLE, y AS DOUBLE)
    FUNCTION naked_funadd# (x AS DOUBLE, y AS DOUBLE)
END DECLARE

DIM x AS DOUBLE
DIM y AS DOUBLE

CALL subpi(x)
PRINT "subpi "; x
y = funpi
PRINT "funpi "; y
PRINT "naked_funln2 "; naked_funln2
x = 10
y = 32
PRINT "funadd "; funadd(x, y)
PRINT "naked_funadd "; naked_funadd(x, y)
« Last Edit: June 25, 2020, 09:34:55 am by jack »