14

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.

Nick Pandolfi
  • 993
  • 1
  • 8
  • 22
  • 1
    possible duplicate of [How to write python extensions in pure asm and would it be efficient?](http://stackoverflow.com/questions/14546610/how-to-write-python-extensions-in-pure-asm-and-would-it-be-efficient) – jcaron Jun 28 '14 at 01:38

3 Answers3

16

You could create a C extension wrapper for the functions implemented in assembly and link it to the OBJ file created by 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 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'])])

Build and run:

nasm -fwin32 myfunc.asm

python setup.py build_ext --inplace

python -c"import myext;myext.myfunc()"

cgohlke
  • 9,142
  • 2
  • 33
  • 36
10

You can also embed assembly directly inside your Python program:

These work by compiling the assembly and loading it into executable memory at runtime. The first three projects implement x86 assemblers in Python, whereas the last calls out to an external compiler.

Luke
  • 11,374
  • 2
  • 48
  • 61
2

Not sure about the "power" of assembly, really.

You can start here: https://docs.python.org/2/extending/extending.html

It's about extending python with compiled code, written in C or C++, but the principle should be the same (C is really just a portable macro-assembler).

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • I read many docs on how to extend python, but I cant figure this out, thus the question. If I could have figured this out from a python documentation page, I would have. Posting a answer directing me to something that doesn't even acknowledge the question (NASM / Assembly / 'illustrate how') is not an answer. Sorry. The least you could have done was show how you could have used C as a assembler for my case. – Nick Pandolfi Jun 28 '14 at 01:33
  • C generates assembly which is then compiled (though this is now well hidden nowadays). Object formats, symbols and all the rest are common to both. You can even inline assembly in C code. So if you understand the conventions used for integrating C code in python, then they are exactly the same for assembly. – jcaron Jun 28 '14 at 01:37
  • The topic has already been addressed here: http://stackoverflow.com/questions/14546610/how-to-write-python-extensions-in-pure-asm-and-would-it-be-efficient – jcaron Jun 28 '14 at 01:38
  • 2
    Completely different questions, just look at them and read them. One is asking if it is possible and efficient, the other if it can be done from NASM, and the procedure imported into python as a function. – Nick Pandolfi Jun 28 '14 at 01:42