This shows how to call strings of x86 machine code. It shows how the EAX register is used to return integer results and how to extract a parameter from the stack, replacing the return address. This assumes the SDECL calling protocol where the called function cleans the stack of any parameters before returning to the caller.
This approach can be used for dynamic assembly or compilation during run time.
' Calling Opcode strings
' Charles E V Pegge
' 22 June 2007
' FreeBasic ver 0.16b
union opcodestring
s as byte ptr
f as function () as long
f1 as function ( byval long ) as long
end union
dim as string st,st1
dim as opcodestring act,act1
' clear the eax register, increment it and return
st=chr$(&h33)+chr$(&hc0)+chr$(&h40)+chr$(&hc3) ' xor eax,eax inc eax ret
' extract the parameter from the stack into eax and return
st1=chr$(&h5a)+chr$(&h58)+chr$(&h52)+chr$(&hc3) ' pop edx pop eax push edx ret
act.s=strptr(st): act1.s=strptr(st1) ' set string pointers
' Test the strings
print act.f() ' answer 1
print act1.f1(42) ' answer 42