Is there any good assembly generation module for Python?
Asked Answered
B

1

6

I'm searching for a good assembly generation module for Python. I have found this one: PyAsm

But it's not working well. I want to execute and generate assembly executable file for simple operations like add, sub, divide and multiply. Something like Reflection.Emit library in .NET.

I'm developing under Linux (Ubuntu 12.10 64bit) and Python2.7.

For example, when I try to compile this simple sub code with PyAsm it gives me a "Segmentation fault (core dumped)":

from ctypes import c_int
from pyasm import Program
from pyasm.instructions import push, mov, ret, pop, sub
from pyasm.registers import eax, esp, ebp

def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr+8),
        sub(eax, 10),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_int, [c_int])
    assert fun(1234) == 1224

if __name__ == '__main__':
    test()
Bothersome answered 2/3, 2013 at 13:59 Comment(9)
Reflection.Emit in .NET doesn't generate assembly, it generates ILCode. ILCode is executed in the .net virtual machine.Elyssa
Rather than asking for recommendations (which is generally not on-topic here), why don't you show the exact problem you have with PyASM - chances are someone can either help you fix it and/or suggest alternatives.Shanklin
@Femaref, I perfectly know what Reflection.Emit in .NET does. I meant something like this but for x86 assembly.Bothersome
@Mat, when trying to build one of the predefined tests in the bitbucket it gives me a "Segmentation fault (core dumped)". For example this one: from ctypes import c_int from pyasm import Program from pyasm.instructions import push, mov, ret, pop, sub from pyasm.registers import eax, esp, ebp def test(): prog = Program( push(ebp), mov(ebp, esp), mov(eax, ebp.addr+8), sub(eax, 10), pop(ebp), ret(), ) fun = prog.compile(c_int, [c_int]) assert fun(1234) == 1224 if __name__ == '__main__': test()Bothersome
Please edit your question to add all the necessary information.Shanklin
Why why why? I'm a compilerNiela
@danodonovan, because I have an university related project - simple C compiler.Bothersome
When I was at uni we had to write assembly ourselves (without help from others), pretty much so we could learn it was horrid and we should leave it to compilers.Niela
I have to learn it too during the first year but now I have to write a whole compiler. So that's why I'm searching for such a module which will automatically and easily generate assembly.Bothersome
A
3

Not just an awesome name: https://github.com/Maratyszcza/PeachPy

Looks at it's use in: http://www.yeppp.info

Automation answered 1/7, 2013 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.