【发布时间】:2013-02-16 23:48:34
【问题描述】:
我正在寻找一个好的 Python 程序集生成模块。 我找到了这个: PyAsm
但是效果不好。 我想为简单的操作(如加法、减法、除法和乘法)执行和生成汇编可执行文件。 .NET 中的 Reflection.Emit 库之类的东西。
我正在 Linux (Ubuntu 12.10 64bit) 和 Python2.7 下开发。
例如,当我尝试使用 PyAsm 编译这个简单的子代码时,它给了我一个“分段错误(核心转储)”:
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()
【问题讨论】:
-
.NET 中的
Reflection.Emit不会生成程序集,它会生成 ILCode。 ILCode 在 .net 虚拟机中执行。 -
而不是寻求建议(这通常不是这里的主题),您为什么不展示您在 PyASM 中遇到的确切问题 - 可能有人可以帮助您解决它和/或提出替代方案。
-
@Femaref,我完全知道 Reflection.Emit 在 .NET 中的作用。我的意思是这样的,但对于 x86 程序集。
-
@Mat,当尝试在 bitbucket 中构建预定义测试之一时,它给了我一个“分段错误(核心转储)”。比如这个:
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() -
当我在大学时,我们不得不自己编写程序集(没有其他人的帮助),几乎是为了让我们知道它是可怕的,我们应该把它留给编译器。