Anonymous user
Machine code: Difference between revisions
m
Updated whitespace, removed comments as language is explanatory enough.
m (Updated whitespace.) |
m (Updated whitespace, removed comments as language is explanatory enough.) |
||
Line 773:
=={{header|Python}}==
{{works with|CPython|3.x}}
<lang Python>import ctypes
import os
from ctypes import c_ubyte, c_int
code = bytes([0x8b, 0x44, 0x24, 0x04, 0x03, 0x44, 0x24, 0x08, 0xc3])
code_size = len(code)
if (os.name == 'posix'):
import mmap
executable_map = mmap.mmap(-1, code_size, mmap.MAP_PRIVATE | mmap.MAP_ANON, mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC)
executable_map.write(code)
func_address = ctypes.addressof(c_ubyte.from_buffer(executable_map))
elif (os.name == 'nt'):
code_buffer = ctypes.create_string_buffer(code)
PAGE_EXECUTE_READWRITE = 0x40
MEM_COMMIT = 0x1000
executable_buffer_address = ctypes.windll.kernel32.VirtualAlloc(0, code_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
Line 807 ⟶ 796:
func_address = executable_buffer_address
else:
code_buffer = ctypes.create_string_buffer(code)
func_address = ctypes.addressof(code_buffer)
prototype = ctypes.CFUNCTYPE(c_int, c_ubyte, c_ubyte)
func = prototype(func_address)
▲prototype = ctypes.CFUNCTYPE(c_int, c_ubyte, c_ubyte) # build a function prototype from return type and argument types
res = func(7, 12)▼
print(res)</lang>▼
▲res = func(7,12)
▲print(res)
=={{header|Racket}}==
|