Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
29
Task/Machine-code/Nim/machine-code.nim
Normal file
29
Task/Machine-code/Nim/machine-code.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import posix
|
||||
|
||||
when defined(macosx) or defined(bsd):
|
||||
const MAP_ANONYMOUS = 0x1000
|
||||
elif defined(solaris):
|
||||
const MAP_ANONYMOUS = 0x100
|
||||
else:
|
||||
var
|
||||
MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint
|
||||
|
||||
proc test(a, b: cint): cint =
|
||||
# mov EAX, [ESP+4]
|
||||
# add EAX, [ESP+8]
|
||||
var code = [0x8B'u8, 0x44, 0x24, 0x4, 0x3, 0x44, 0x24, 0x8, 0xC3]
|
||||
|
||||
# create executable buffer
|
||||
var buf = mmap(nil, sizeof(code), PROT_READ or PROT_WRITE or PROT_EXEC,
|
||||
MAP_PRIVATE or MAP_ANONYMOUS, -1, 0)
|
||||
|
||||
# copy code to buffer
|
||||
copyMem(addr buf, addr code[0], sizeof(code))
|
||||
|
||||
# run code
|
||||
{.emit: "`result` = ((int (*) (int, int))&`buf`)(`a`,`b`);".}
|
||||
|
||||
# free buffer
|
||||
discard munmap(buf, sizeof(code))
|
||||
|
||||
echo test(7, 12)
|
||||
5
Task/Machine-code/Phix/machine-code-1.phix
Normal file
5
Task/Machine-code/Phix/machine-code-1.phix
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
atom mem = allocate(9)
|
||||
poke(mem,{#8B,#44,#24,#04,#03,#44,#24,#08,#C3})
|
||||
constant mfunc = define_c_func({},mem,{C_INT,C_INT},C_INT)
|
||||
?c_func(mfunc,{12,7})
|
||||
free(mem)
|
||||
12
Task/Machine-code/Phix/machine-code-2.phix
Normal file
12
Task/Machine-code/Phix/machine-code-2.phix
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
atom mem = allocate(9)
|
||||
poke(mem,{#8B,#44,#24,#04,#03,#44,#24,#08,#C3})
|
||||
integer res
|
||||
#ilASM{ mov eax,[mem]
|
||||
call :%pLoadMint -- (in case mem>#3FFFFFFF)
|
||||
push 12
|
||||
push 7
|
||||
call eax
|
||||
add esp,8
|
||||
mov [res],eax }
|
||||
?res
|
||||
free(mem)
|
||||
Loading…
Add table
Add a link
Reference in a new issue