Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Python to use Assembly

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.

like image 578
Nick Pandolfi Avatar asked Jun 28 '14 01:06

Nick Pandolfi


People also ask

Can you use assembly in Python?

Assembly is a pythonic object-oriented, mid stack, batteries included framework built on Flask, that adds structure to your Flask application, and group your routes by class. Assembly allows you to build web applications in much the same way you would build any other object-oriented Python program.

Is Python an assembly?

Is Python an Assembly Language? Python is more advanced than assembly languages. Assembly languages are considered a low level language, while high-level languages such as C, Java, or Python use 0's and 1's instead of numbers, symbols, and abbreviations.


2 Answers

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()"

like image 113
cgohlke Avatar answered Oct 05 '22 16:10

cgohlke


You can also embed assembly directly inside your Python program:

  • https://github.com/Maratyszcza/PeachPy
  • https://github.com//pycca/pycca
  • http://codeflow.org/entries/2009/jul/31/pyasm-python-x86-assembler/
  • https://github.com/AmihaiN/pyAsm

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.

like image 30
Luke Avatar answered Oct 05 '22 16:10

Luke