How to call C language's inline assembly function asm()
in cython? I tried a simple asm("mov $eax, 0x1") or __asm__()
. It cythonizes fine until the asm
-call, which then give me below error:
NameError: name 'asm' is not defined
I compile my code as python setup.py build_ext --inplace && python runids.py
It is not possible to call assembler directly from Cython. The best you can do is to wrap asm
in a C-function and call it from Cython - for example using verbatim C-code like this (here for gcc):
%%cython
cdef extern from *:
"""
void inline_asm_fun(void){
__asm__("mov $1, %%eax" ::: "eax");
}
"""
void inline_asm_fun()
def use_inline_asm():
inline_asm_fun()
As @PeterCordes has pointed out, to do something useful in inline-assembler one should use extended asm, i.e. __asm__("mov $1, %%eax" ::: "eax")
instead of simple and unsafe __asm__("mov $1, %eax")
. The latter would modify %eax
without notifying the compiler about it.
For MVSC, the nonstandard extension for inline-assembler is __asm
but it is only supported for x86 and not x64.