Results 1 to 7 of 7

Thread: [RESOLVED] ThunderVB ASM and function return values

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Resolved [RESOLVED] ThunderVB ASM and function return values

    Anyone can give a simple sample code that is changes a function return value? I can work with subs, but being able to work with functions would be handy as well.

  2. #2
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: ThunderVB ASM and function return values

    functions usually return their result in the EAX or EAX:EDX register(s) or somewhere in memory and return a pointer. floating point values are returned in the floating point registers. so what you have to do depends on the function.

    can you post a sample code?

  3. #3

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: ThunderVB ASM and function return values

    Current mess I have:
    VB Code:
    1. Public Function AsmIfSng(ByVal Expression As Long, ByRef TruePart As Single, ByRef FalsePart As Single) As Single
    2.     ''#asm'.pureasm
    3.     '#asm' push ebp
    4.     '#asm' mov ebp, esp
    5.     '#asm' push ebx
    6.     '#asm' push esi
    7.     '#asm' push edi
    8.     '#asm'
    9.     '#asm' mov eax, [ebp+8]
    10.     '#asm' cmp eax, 0
    11.     '#asm' je falsepart
    12.     '#asm' mov ebx, dword ptr [ebp+12]
    13.     '#asm' jmp done
    14.     '#asm' falsepart:
    15.     '#asm' mov ebx, dword ptr [ebp+16]
    16.     '#asm'
    17.     '#asm' done:
    18.     '#asm' mov ?,?
    19.     '#asm' pop edi
    20.     '#asm' pop esi
    21.     '#asm' pop ebx
    22.     '#asm' mov esp, ebp
    23.     '#asm' pop ebp
    24. End Function

    Basically trying to do this out of interest to see how big difference I there is between native VB6 implementation and an inline ASM version. And to learn atleast some ASM.

  4. #4
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: ThunderVB ASM and function return values

    the VB code...

    VB Code:
    1. Public Function IfSng(ByVal Expression As Long, ByRef TruePart As Single, ByRef FalsePart As Single) As Single
    2.     If Expression Then IfSng = TruePart Else IfSng = FalsePart
    3. End Function

    and how it is compiled (assembly listing)

    Code:
    PUBLIC	?IfSng@modCastedProcs@@AAGXXZ			; modCastedProcs::IfSng
    EXTRN	__fltused:NEAR
    ;	COMDAT ?IfSng@modCastedProcs@@AAGXXZ
    text$1	SEGMENT
    _FalsePart$ = 16
    _TruePart$ = 12
    _Expression$ = 8
    ?IfSng@modCastedProcs@@AAGXXZ PROC NEAR			; modCastedProcs::IfSng, COMDAT
    
    ; 58   :     If Expression Then IfSng = TruePart Else IfSng = FalsePart
    
    	mov	eax, DWORD PTR _Expression$[esp-4]
    	test	eax, eax
    	je	SHORT $L129
    	mov	eax, DWORD PTR _TruePart$[esp-4]
    	fld	DWORD PTR [eax]
    
    ; 59   : End Function
    
    	ret	12					; 0000000cH
    $L129:
    
    ; 58   :     If Expression Then IfSng = TruePart Else IfSng = FalsePart
    
    	mov	ecx, DWORD PTR _FalsePart$[esp-4]
    	fld	DWORD PTR [ecx]
    
    ; 59   : End Function
    
    	ret	12					; 0000000cH
    ?IfSng@modCastedProcs@@AAGXXZ ENDP			; modCastedProcs::IfSng
    text$1	ENDS
    i'm not a assembly guru but i think the VB compiler does a good job here.

  5. #5

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: ThunderVB ASM and function return values

    Ah, thanks I ended up with this:
    VB Code:
    1. Public Function AsmIfSng(ByVal Expression As Boolean, ByRef TruePart As Single, ByRef FalsePart As Single) As Single
    2.     '#asm'.pureasm
    3.     '#asm' mov eax,[esp+4]
    4.     '#asm' test eax,eax
    5.     '#asm' je falsepart
    6.     '#asm' mov eax,[esp+8]
    7.     '#asm' fld dword ptr [eax]
    8.     '#asm' ret 12
    9.     '#asm' falsepart:
    10.     '#asm' mov ecx,[esp+12]
    11.     '#asm' fld dword ptr [ecx]
    12.     '#asm' ret 12
    13. End Function
    Pretty much the same code VB compiles to, but for some reason I found that the ThunderVB/MASM32 compiled version performed slightly faster (0.86 ms vs. 0.8 ms with 100 000 loops). Also, for a reason I do not understand, the first time I call the "native VB" compiled version, it seems to have a big slowdown. But on second test it works just as fast as the ASM version.


    Edit!
    Oo, VB6's compiled version is faster for False. End conclusion: VB6 compiler does so good with this that there is no need to optimize
    Last edited by Merri; Jul 16th, 2006 at 09:09 AM.

  6. #6
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: [RESOLVED] ThunderVB ASM and function return values

    unfortunately it doesn't seem to be easy to get proper benchmark results. when you compare two functions then the first one always seems to be slower then the second one. run every test at least twice in a row. and the longer the test runs the more accurate is the result.

    the reason why the second test is faster might be that things are already in the second or fist level cache

  7. #7

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] ThunderVB ASM and function return values

    I don't test like that: I always put benchmarks behind their own buttons. One button for one test and you can ensure with multiple tries what is the general truth behind there I found out that weirdness in my own benchmarkings a few years ago and since then I've always had benchmarked code separated from each other.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width