windows - How to get Python to use Assembly -
i beginner in assembly, master in python. have started learn x86_64 nasm windows, , wish combine power of assembly, , flexibility of python. have looked over, , have not found way use nasm assembly procedure within python. not mean in-line assembly. wish write assembly program, compile it, , somehow extract procedure use in python program. can illustrate simple example of how this, lost.
you create c extension wrapper functions implemented in assembly , link obj file created nasm.
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 function ; 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'])]) build , run:
nasm -fwin32 myfunc.asm
python setup.py build_ext --inplace
python -c"import myext;myext.myfunc()"
Comments
Post a Comment