» 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!

ASM StrLen vs Len()

This piece of inline ASM code will return the length of the string. It returns the exact same length on strings like the native Len() function. It seems that there is no real speed improvement overhere.

Notice that my string length was 15888. (although that doesn't really matter in this code)

Public Function strLen(sIn As String) As Long
';This is working. Returns the length of the input string. JS
'#ASM_START
'
' push ebp
' mov ebp, esp
' push ebx
' push esi
' push edi
'
' mov eax, [ebp+8]
' mov ecx, [eax]
' xor eax, eax
' mov ax, [ecx-4]
' cdq
' sub eax, edx
' sar eax, 1
'
' pop edi
' pop esi
' pop ebx
' mov esp, ebp
' pop ebp
' ret 4
'
'#ASM_END
End Function

Simply call this code like you would do with Len(), eg:
strLen = mdlASM.strLen(strTest1)

LCase$ % faster than ASMLCase LCase$ (sec) ASMLCase (sec)
12.1% 0.06358 0.056693
6.5% 0.0593 0.055694
23.2% 0.069963 0.056776
7.2% 0.061476 0.057351
-18.4% 0.058873 0.07217

LCase$ % faster than ASMLCase LCase$ (sec) ASMLCase (sec)
-0.9% 0.597489 0.602938
-5.4% 0.614723 0.650017
-9.6% 0.588754 0.651291
-4.2% 0.587962 0.613989
-2.9% 0.591222 0.609115


User contributed notes:

Author: grigri (grigri at deadparrot dot co dot uk) Date: 19:11 20/11/2005
Why are you pushing and popping esi and edi in the prolog/epilog, since you're not using those registers? Removing them would probably incur a speed increase.

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