如何写纯ASM Python扩展,这将是有效的? [英] How to write python extensions in pure asm and would it be efficient?

查看:286
本文介绍了如何写纯ASM Python扩展,这将是有效的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的Python 中的业余技能,​​我初学 ASM 并没有任何知识 C -language。

I have medium amateur skills in Python and I'm beginner in asm and haven't any knowledge of C-language.

我知道蟒蛇 C -Extensions必须遵循特定的接口很好地工作。

I know that python C-extensions must follow specific interface to work fine.

这是可以写在纯大会 Python扩展与正确的接口和完整的功能?第二个问题是,这将是足够有效的,如果这样的情况下,对吧?

Is this possible to write python extension in pure Assembly with the right interface and full functionality? The second question is would it be efficient enough if case of doing it right?

尽管谷歌上搜索我还没有发现或code中的任何实例对这个问题的一些文章或解决方案。

While googling I haven't found any examples of code or some articles or solutions about this question.

和本有关运行 ASM 的问题 - 的Python内code 所以它不是因此重复的主题。

And this ISN'T the question about running asm-code from within Python so it's not duplicate of topics on SO.

推荐答案

如何做到这一点:

我不知道,如果你能在议会做'纯',而是:
如果你做一个代理类(让我们称之为这种方式)在 C,它调用汇编函数,然后你再与C约定写汇编程序,那么,通过简单的编译汇编code:

I don't know if you could do it 'purely' in assembly, but: If you make a "proxy class" (lets call it that way) in C that calls the assembly function, and you then write the assembler with the C convention, then, by simply compiling the assembler code:

nasm -felf64 -g -F dwarf assembly_function.asm

,然后使用含有setup.py文件:

and then, using a setup.py file containing:

from distutils.core import setup, Extension
setup(name='assembly_include_name', version='1.0', ext_modules=[Extension('assembly_include_name', ['c_assembly_proxy.c'],extra_objects=["assembly_function.o"])])

您将能够做你想要的。请注意,您必须为添加参数extra_objects来扩展构造为了告诉Python链接大会code,否则会崩溃说,它无法找到函数名

you will be able to do what you wanted. Notice that you have to add the parameter "extra_objects" to the Extension constructor in order to tell python to link the assembly code, otherwise it will crash saying that it can't find the function's name.

为什么你会做到这一点:

如果你想使用SSE指令(SSE2,SSE3 ...)无论优化编译器可以使。

If you want to use SSE instructions (SSE2, SSE3...) regardless the optimization the compiler could make.

扩展API:
https://docs.python.org/2/extending/extending.html

disutils.core参考:
<一href=\"https://docs.python.org/2/distutils/apiref.html?highlight=distutils.core#module-distutils.core\" rel=\"nofollow\">https://docs.python.org/2/distutils/apiref.html?highlight=distutils.core#module-distutils.core

disutils.core reference: https://docs.python.org/2/distutils/apiref.html?highlight=distutils.core#module-distutils.core

这篇关于如何写纯ASM Python扩展,这将是有效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆