如何获得Python的使用大会 [英] How to get Python to use Assembly
问题描述
我是一个初学者在组装,但Python中的高手。我才刚刚开始学的x86_64 NASM的窗户,我希望大会的权力和Python的灵活性相结合。我已经看过一切都过去了,我还没有找到一种方法,使用NASM汇编程序从内部的Python。我这样说并不意味着行内汇编。我想编写一个汇编程序,编译它,然后以某种方式提取我的Python程序中使用的程序。有人能说明如何做一个简单的例子,因为我完全丧失。
I am a beginner in assembly, but a master in Python. I have just recently started to learn x86_64 NASM for windows, and I wish to combine the power of assembly, and the flexibility of Python. I have looked all over, and I have not found a way to use a NASM assembly procedure from within Python. By this I do not mean in-line assembly. I wish to write an assembly program, compile it, and then somehow extract the procedure to use in my Python program. Can someone illustrate a simple example of how to do this, as I am completely lost.
推荐答案
您可以创建一个 C扩展包装实施组装,并将其链接到由NASM创建的OBJ文件的功能。
You could create a C extension wrapper for the functions implemented in assembly and link it to the OBJ file created by nasm.
一个虚拟的例子(对于32位的Python 2;未测试):
A dummy example (for 32 bit Python 2; not tested):
myfunc.asm:
;http://www.nasm.us/doc/nasmdoc9.html
global _myfunc
section .text
_myfunc:
push ebp
mov ebp,esp
sub esp,0x40 ; 64 bytes of local stack space
mov ebx,[ebp+8] ; first parameter to function
; some more code
leave
ret
myext.c:
#include <Python.h>
void myfunc(void);
static PyObject*
py_myfunc(PyObject* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
myfunc();
Py_RETURN_NONE;
}
static PyMethodDef MyMethods[] =
{
{"myfunc", py_myfunc, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initmyext(void)
{
(void) Py_InitModule("myext", MyMethods);
}
setup.py:
from distutils.core import setup, Extension
setup(name='myext', ext_modules=[
Extension('myext', ['myext.c'], extra_objects=['myfunc.obj'])])
构建和运行:
NASM -fwin32 myfunc.asm
蟒蛇setup.py build_ext --inplace
蟒蛇-c进口myext; myext.myfunc()
这篇关于如何获得Python的使用大会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!