Using inline assembly:
Code: Select all
Type pnt
x As Integer
y As Integer
End Type
Dim Punkt As pnt
Punkt.x = 17
Punkt.y = 55
Print Punkt.x, Punkt.y
#If (Not Defined(__FB_64BIT__))
#Error "only x86_64 architecture supported"
#EndIf
Asm
lea rcx, Punkt
mov rax, [rcx+offsetof(pnt, x)]
Add rax, [rcx+OffsetOf(pnt, y)]
inc rax
Shr rax, 1
mov [rcx+offsetof(pnt, x)], rax
mov [rcx+offsetof(pnt, y)], rax
End Asm
Print Punkt.x,Punkt.y
Sleep
or alternatively implementing it using a (naked) function:
Code: Select all
Type pnt
x As Integer
y As Integer
End Type
Dim Punkt As pnt
Punkt.x = 17
Punkt.y = 55
Print Punkt.x, Punkt.y
Sub doCalc Naked (p As pnt Ptr)
#If (Not Defined(__FB_WIN32__)) Or (Not Defined(__FB_64BIT__))
#Error "only x86_64 architecture on windows supported"
#EndIf
Asm
mov rax, [rcx+offsetof(pnt, x)]
Add rax, [rcx+offsetof(pnt, y)]
inc rax
Shr rax, 1
mov [rcx+offsetof(pnt, x)], rax
mov [rcx+offsetof(pnt, y)], rax
ret
End Asm
End Sub
doCalc(@Punkt)
Print Punkt.x,Punkt.y
Sleep
Of course using assembly does not make sense in this example.
See also
https://msdn.microsoft.com/en-us/library/ms235286.aspx
https://msdn.microsoft.com/en-us/library/6t169e9c.aspx
(or the corresponding docs of the platform you use)
The calling conventions used by Windows / Unix differ and are not compatible, so you probably have to adapt the code to the corresponding platform.
//edit: integrated fxm's suggestion using "offsetof" instead of hardcoded offsets; I didn't know that FB's inline assembly supports that
//edit2: I've updated the wiki/tutorial pages accordingly with portability warnings (text in german):
http://www.freebasic-portal.de/tutorial ... ic-50.html
http://www.freebasic-portal.de/befehlsr ... sm-47.html
http://www.freebasic-portal.de/befehlsr ... r-434.html
//edit3: corrected too many brackets error introduced with first edit (thanks again, fxm)