Print Topic - Archive

XBLite Forum  /  XBLite Programming  /  Using callbacks with CCall convention
Posted by: 26 (Guest), August 24, 2008, 10:25am
Some libraries need to use user callBacks.Native convention for XBlite is STDCall but some of theses library request CCall conventions in their callbacks (NEWTON,LUA,etc...)
Hopefully, Xblite owns inline ASM so nothing is lost, this is just a little trick that does the job.
First, write your function normaly and compile (F9 only, don't build).
Look at yourprogram.asm and check for your function end (use search because ASM code is very very very very (and so on) long).It looks like this :

Quoted Text
end.SV_NEWTON_RaycastNOTfilter.mye2:  ;;; Function end label for Assembly Programmers.
end.func117.mye2:
     lea     esp,[ebp-20]                    ;;; i110
     pop     ebx                    ;;; restore ebx
     pop     edi                    ;;; restore edi
     pop     esi                    ;;; restore esi
     leave                         ;;; replaces 'mov esp,ebp' 'pop ebp'
     ret     20               ;;; i111a


So just copy asm code and remove the number after 'ret' (in fact it removes arguments from stack but in CCall, caller removes the arguments so that unbalance the stack)

And when your funcs returns a value ?

This is simple, if it is an integer, return the value on EAX, if it is a float, return on ST0.You can use a local variable to write the value and load it into EAX or ST0 after.Example :

FUNCTION SINGLE DoSomething(a,b,c,d)

SINGLE float

float=1.25!

ASM fld d[DoSomething.float]
(copy here ASM code like i wrotte before)

If you use some standarts values, this is a shortcut

Quoted Text
FLD1     Push +1.0 onto the FPU register stack.
FLDL2T     Push log210 onto the FPU register stack.
FLDL2E     Push log2e onto the FPU register stack.
FLDPI     Push p onto the FPU register stack.
FLDLG2     Push log102 onto the FPU register stack.
FLDLN2     Push loge2 onto the FPU register stack.
FLDZ     Push +0.0 onto the FPU register stack.

Easy Assembler Shell v3.99 Copyright(C) 1999 Roman Novgorodov



For integers, this is the same methods execpt you must use EAX

A complete real example, a callback filter for raycasting in NEWTON :

Quoted Text

'
' ########################################
' #####  SV_NEWTON_RaycastNOTfilter  #####
' ########################################
'
'will stop hit on first body except body that initiate raycast
'
FUNCTION SINGLE SV_NEWTON_RaycastNOTfilter (body,CVector3 normal,collision,from_body,SINGLE param)
SHARED SERVER_INFO ServerI

IF body=from_body THEN
     
ASM          fld d[SV_NEWTON_RaycastNOTfilter.param]

ELSE     
     ServerI.rayCastResult=body
ASM          fldz

ENDIF

ASM     lea     esp,[ebp-20]     
ASM pop     ebx                    
ASM     pop     edi                    
ASM     pop     esi                    
ASM     leave                    
ASM     ret     
END FUNCTION


I hope this could be usefull to someone else.
Posted by: Rhett Thompson, August 28, 2008, 3:09pm; Reply: 1
I had to use a very similar method to use both Lua and Newton, and I'm glad you posted this.  It would be a very good demo to be added into next Xblite release.
Posted by: Rhett Thompson, August 29, 2008, 2:14am; Reply: 2
Heh.  You can more easily define the function in Xblite.

Code
DECLARE CFUNCTION Test(arg)
...
CFUNCTION Test(arg)


I've tested this with Newton, and it works as expected:)
Posted by: 26 (Guest), August 29, 2008, 4:38am; Reply: 3
I must update a bit some of my knowledges, i miss this one !
Anyway, this is great, no more asm tricks to have CCall callbacks , that rocks :)
Posted by: 13 (Guest), August 29, 2008, 7:01am; Reply: 4
There are a lot of tricks in xblite that take some time to find.  Not because of a lack of documentation - the documentation is great!  The demos are great!  It is just that there is so much capability in xblite that it takes a while to find it all.  It is a very "rich" language.

Greg
Posted by: Rhett Thompson, August 29, 2008, 1:03pm; Reply: 5
I know it's great, Xblite is by far my favorite language:)
Posted by: 26 (Guest), August 29, 2008, 9:36pm; Reply: 6
Yes , XBlite is great, it has evolved a lot from XBasic times.I remember when i was using XBasic and features that were missing.Now XBlite owns a lot of theses features (my favorites are m4 and inline ASM :) ) and is able to come up to my expectations..It is my favorite language too.
Print page generated: February 5, 2012, 2:40pm