【问题标题】:how to call assembler code with Python on Windows如何在 Windows 上用 Python 调用汇编代码
【发布时间】:2022-12-24 23:24:19
【问题描述】:

this link 给出了一个在 Linux 平台上用 Python 执行 asm 的好例子,但我不知道如何在 Windows 上调用带有返回值的 asm func,你能告诉我怎么做或者给我一个例子吗?

import ctypes
import mmap

buf = mmap.mmap(-1, mmap.PAGESIZE, prot=mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC)

ftype = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
fpointer = ctypes.c_void_p.from_buffer(buf)

f = ftype(ctypes.addressof(fpointer))

buf.write(
    b'\x8b\xc7'  # mov eax, edi
    b'\x83\xc0\x01'  # add eax, 1
    b'\xc3'  # ret
)

r = f(42)
print(r)

del fpointer
buf.close()

【问题讨论】:

  • 如果这是纯粹的好奇心,那很好,但请说出来。如果您尝试使用 Python“内联”程序集解决潜在问题,请直接询问该问题。否则你的问题将只是XY problem
  • PROT_READPROT_WRITEPROT_EXEC 是特定于 Unix 的。对于 Windows 系统,使用 alternative constructoraccess 参数。

标签: python windows assembly x86-64


【解决方案1】:
猜你喜欢
  • 2011-10-08
  • 1970-01-01
  • 2012-03-15
  • 2013-10-21
  • 2019-01-21
  • 2016-06-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
相关资源
最近更新 更多