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

You can't just type ASM code in VB to use inline ASM.
To get inline ASM working in VB, you will have to use an free plugin which John Sagas made in 2003. He posted his code on Planet-source-code, but somehow it disappeared.
This site simply has a mirror of the original zip file. Comments and bugfixes might be found on the PSCode.com submission. You can find the files in the files section.
After you have installed the addin (the best way is compiling the Add-in, I think), and turned it on in the "Add-in" dialog, you can start with inline ASM!

First you will see this icon in your toolbar:

You will need to download MASM as well. This is an assembler which will "compile" your inline asm code between VB's compilation. Get it from here. Please read the documentation carefully so everything is set-up correctly.

Don't forget to config the MASM's ML.exe (which is in masmbin), under the configuration of the add-in:

To add ASM code to your project, simply follow these steps:

  • Add a new module to your project
  • Simply make a sub or function like you normal would:
  • Public Function Test(Byval Argument1)

    End Function

  • When writing ASM code, put your code in COMMENTS. by using ' in front of your code.
  • Use the #ASM_START and #ASM_END tag!
  • '#ASM_START
    '#ASM_END

    This way, VB won't start telling you it's wrong. The plugin will internally remove the comments and then compile the actual asm code. Very nifty hm? :)

  • Simply call your function like how you would do it in VB. It might look weird. A call to a function with only ASM functions. but if you compile to an EXE it will be alright! (that is, if your ASM code is alright ;-))
I recommend also reading the article about conditional compilation. It's very usefull. Since the inline ASM functions/subs won't do anything while you are running your code from the Visual Basic IDE!


Arguments

You can use arguments from VB very easily into your ASM code. Simply use:

equ [ebp+8] ;input argument1
equ [ebp+12] ;input argument2

Etc. I recommend using pointers (long, 4 bytes) only! But sometimes you should use Byref arguments instead.
I recommend checking out the code examples to see how strins and singles are used.

Example function:

Public Sub asmSinCosPointer(ByRef sngCosAngle As Single, ByRef sngSinAngle As Single)
'#ASM_START
'.MMX
' push ebp
' mov ebp, esp

' mov eax, DWORD PTR [ebp+8] ;'//First argument, store in eax (= value now!)

' fld dword ptr [eax] ;'//Get value for cos
' fsincos ;'//Execute sin/cos instruction
' fstp dword ptr [eax] ;'//Store cos in eax (1st param pointer!)

' mov eax, DWORD PTR [ebp+12] ;'//Second argument, store in eax (=value now!)
' fstp dword ptr [eax] ;'//Store sin in eax (2nd param pointer!)

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

Note that you should be a bit familiar with ASM before starting with this. Really!.
You can learn the basics of ASM just by spending one complete weekend on it.

Errors
When you have an error in your ASM code, VB will give an error during compilation "Unexpected error occured in code generator or linker - view error messages?" or something similar. Click yes will only result in some vague error.
The best way to see what error you made is by opening the file "modulename.dbg", it contains the error somewhere, with description and number.

Have fun!