RosettaCodeData/Task/Machine-code/Pluto/machine-code-2.pluto

20 lines
527 B
Plaintext

local ffi = require "pluto:ffi"
local lib = ffi.open("./mclib.so")
lib:cdef[[
unsigned char run_machine_code(const char *code, unsigned char a, unsigned char b, int len);
]]
local a = 7
local b = 12
-- x64 opcodes for this task
local m = {
0x55, 0x48, 0x89, 0xe5, 0x89, 0x7d,
0xfc, 0x89, 0x75, 0xf8, 0x8b, 0x75,
0xfc, 0x03, 0x75, 0xf8, 0x89, 0x75,
0xf4, 0x8b, 0x45, 0xf4, 0x5d, 0xc3
}
local code = m:mapped(|byte| -> string.char(byte)):concat("")
print($"{a} + {b} = {lib.run_machine_code(code, a, b, #m)}")