简体   繁体   中英

How to call C inline assembly asm in cython?

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 , ie __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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM