» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

GetDistance function in asm

This function will calculate the distance between two given points in 2D. It's a lot faster than the "normal" VB code to do a Distance calculation.

'//How-to use:
'//Just use: value = ASMGetDistance(100, 100, 500, 500)
'//Arguments should be long, and are send ByVal. (Original variables won't be altered)

Public Function ASMGetDistance(ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
'#ASM_START
'.686
' long1 equ [ebp+8]

' push ebp
' mov ebp, esp

' push edi
' push edx

' mov eax, [ebp+16] ; Get first argument
' mov edx, [ebp+8] ; Third argument
' sub eax, edx
' mov ecx, eax
' mul ecx ; //(x2-x1)*(x2-x1)
' mov edi, eax

' mov eax, [ebp+20] ; Get second argument
' mov edx, [ebp+12] ; Get fourth argument
' sub eax, edx
' mov ecx, eax
' mul ecx ; //(y2-y1)*(y2-y1)

' add eax, edi
' mov long1, eax


' fild dword ptr long1
' fsqrt
' fistp dword ptr long1

' mov eax, long1

' pop edx
' pop edi

' mov esp, ebp ;MOV/POP is much faster
' pop ebp ;on 486 and Pentium than Leave
' ret 8
'#ASM_END

End Function

Public Function FindDistance(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Double
FindDistance = Sqr((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1))
End Function

DistanceVB % faster than DistanceASM DistanceVB (sec) DistanceASM (sec)
83.6% 0.001312 0.000715
85.3% 0.001302 0.000702
85.4% 0.001301 0.000701
43.3% 0.001302 0.000908
100.5% 0.001413 0.000705

DistanceVB % faster than DistanceASM DistanceVB (sec) DistanceASM (sec)
96.5% 0.002805 0.001427
243% 0.004913 0.001432
68.7% 0.002799 0.001659
112.2% 0.003045 0.001435
52.8% 0.002824 0.001848


User contributed notes:

Author: Tom (hurendo_kun at hotmail dot com) Date: 17:07 14/07/2005
This test isn't really fair since VB is working with singles and ASM is working with longs. How about an ASM float version?

Author: Spodi (spodii at hotmail dot com) Date: 21:04 05/04/2006
Yeah, I agree - or just redo the VB version in Longs, too.

Author: Darkie (ego at babiestastegood dot com) Date: 22:04 22/04/2006
No matter how you bend it - the asm version is faster. Thanks

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: