简体   繁体   English

如何在 cython 中调用 C 内联汇编汇编?

[英]How to call C inline assembly asm in cython?

How to call C language's inline assembly function asm() in cython?如何在cython中调用C语言的内联汇编函数asm() I tried a simple asm("mov $eax, 0x1") or __asm__() .我尝试了一个简单的asm("mov $eax, 0x1") or __asm__() It cythonizes fine until the asm -call, which then give me below error:它在asm调用之前很好地处理,然后给我以下错误:

NameError: name 'asm' is not defined NameError: 名称 'asm' 未定义

I compile my code as python setup.py build_ext --inplace && python runids.py我将我的代码编译为python setup.py build_ext --inplace && python runids.py

It is not possible to call assembler directly from Cython.无法直接从 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):你能做的最好的事情是将asm包装在一个 C 函数中并从 Cython 调用它 - 例如使用像这样的逐字 C 代码(这里是 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") .正如@PeterCordes 指出的那样,要在内联汇编程序中做一些有用的事情,应该使用扩展 asm ,即__asm__("mov $1, %%eax" ::: "eax")而不是简单__asm__("mov $1, %eax")安全的__asm__("mov $1, %eax") The latter would modify %eax without notifying the compiler about it.后者会修改%eax而不通知编译器。

For MVSC, the nonstandard extension for inline-assembler is __asm but it is only supported for x86 and not x64.对于 MVSC,内联汇编器的非标准扩展是__asm但它仅支持 x86 而不是 x64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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