Executing assembler code with python

Yuda Prawira picture Yuda Prawira · May 18, 2011 · Viewed 37.8k times · Source

I want to execute assembly code inside a python script. Is that possible?

In C programming would be like this

static inline getesp(){
        __asm__("mov %esp, %eax");
}

But how to do that with Python? Is it possible?

Answer

jkp picture jkp · May 18, 2011

One way you could do this would be to write a (C) extension for Python. You can take a look at this documentation for full details of how to do that.

Another way of developing C-based Python extensions would be to interface directly with an external library using the ctypes module.

In any case, you'd need some C code compiled into either a library or an extension and a way to call it from Python. Clearly for what you want to achieve this is probably not optimal but actually its not that much work to expose a few functions.