Is it somehow possible to call x86-opcodes with FreeBasic? Something like "CALL ABSOLUTE" in QBasic?
I tried the following:
Code: Select all
dim as ubyte test(0)={&hC3}
dim as ubyte ptr test_call=@test(0)
asm call [test_call]
Code: Select all
dim as ubyte test(0)={&hC3}
dim as ubyte ptr test_call=@test(0)
asm call [test_call]
Code: Select all
asm call label
asm label:
print "done"
sleep
Works fine for me on Linux.dodicat wrote:This crashes on 64 bit fbc win 10
I want to write a JIT-Compiler for a script-language. The compiler generates x86-opcodes out of the script and saves them inside a byte-array. Then the program needs to execute those opcodes with an ASM "call".MrSwiss wrote:The statement by you, is by far to fuzzy to make sense, do you have a more "in depth explanation" ?
Code: Select all
dim as ubyte ptr function_in_memory
asm
xor rdi, rdi 'address = 0
mov rsi, 4096 'length of the array
mov rdx, 7 'read OR write OR exec
mov r10, 34 'map_annonymous OR map_private
xor r8, r8
xor r9, r9
mov rax, 9 'mmap
syscall
mov [function_in_memory], rax
end asm
function_in_memory[0]=&hC3 'ret
asm call [function_in_memory]
Code: Select all
#ifndef __FB_64BIT__
dim as ubyte obcodes(...) = > { _
&H55, _ ' push ebp
&H89,&He5, _ ' mov ebp,esp
&H8b,&H75,&H0C, _ ' mov esi,DWORD PTR [ebp+12]
&Hdd,&H06, _ ' fld QWORD PTR [esi]
&H8b,&H7d,&H08, _ ' mov edi,DWORD PTR [ebp+8]
&Hd9,&H1f, _ ' fstp DWORD PTR [edi]
&H5d, _ ' pop ebp
&Hc2,&H08,&H00} ' ret 8
#else
#error 666: only test for x86 !
#endif
type DoubleToSingle_t as sub stdcall(byval pSingle as single ptr, byval pDouble as double ptr)
dim as DoubleToSingle_t DoubleToSingle = cast(DoubleToSingle_t,@obcodes(0))
dim as single sValue
dim as double dValue = atn(1)*4
DoubletoSingle(@sValue,@dValue)
print sValue,dValue
sleep
I called code in memory without mmap.IchMagBier wrote:You need some memory marked as executable. On Linux, you need to use "mmap" for that.
Code: Select all
asm
mov rax, [pMemory]
jmp rax
end asm