Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As, we can intermix assembly language with C++
like

_asm mov eax, ebx


Can we use assembly language in vb.NET or C#.NET. If yes, then how??
Posted
Comments
Sergey Alexandrovich Kryukov 27-Apr-12 23:00pm    
Please don't post non-answers using "Add you solution here" -- it's not a solution. It will be removed, no one receives e-mail notification. Use comments, reply on existing comments, or, if you need, use "Improve question" above.
--SA

You cannot, not directly.

The platform is absolutely different: it does not compile to CPU instruction-set code; the code is compiled into CIL (Common Intermediate Language), so the code can run on many different platforms implementing SLR without recompilation, unless some limitation are applied. (One such limitations is using the native code through P/Invoke, for example.) This is really so: I regularly run my .NET applications on Linux without recompilation, even those using System.Windows.Forms (which is not a standard library).

When loaded, the .NET assembly is compiled to the CPU instructions on each platforms it's being executed using JIT (Just-in-Time Compiler). Most typically, JIT compilation is performed on the per-method basis, when each method is about to be called for the very first time. This way, the methods never called may never be compiled in the native code.

Please see:
http://en.wikipedia.org/wiki/Common_Language_Runtime[^],
http://en.wikipedia.org/wiki/Common_Intermediate_Language[^],
http://en.wikipedia.org/wiki/Common_Language_Infrastructure[^],
http://en.wikipedia.org/wiki/Just-in-time_compilation[^].

This should explain why there is not a place for assembly language in a .NET assembly written in C#, VB.NET or IL.

However, there is a delicate difference here: it does not mean that an executable module cannot have a place for both native code and .NET assembly at the same time. This pretty exotic but relatively widely used option can be implemented with so-called mixed-mode (managed+unmanaged) project written in C++ and C++/CLI at the same time. You can even use C++ code in C++/CLI and visa versa.

In — voila! — you can use asm directives in unmanaged C++ parts of such projects.

About C++/CLI, please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^].

I don't know if it needs to mention that you can use Assembly language in a .NET application indirectly: you can write some unmanaged (native) executable module (DLL) using Assembly language or some high-level language with embedded Assembler and use this module in .NET assembly using P/Invoke.

If you need to learn it, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be useful:
Essential P/Invoke[^].

—SA
 
Share this answer
 
v3
Comments
VJ Reddy 27-Apr-12 22:36pm    
Excellent and comprehensive answer. 5!
I did not refresh the page before posting my answer, so I could see your answer only after posting my answer.
Sergey Alexandrovich Kryukov 27-Apr-12 22:41pm    
Thank you, VJ.

I just added an additional rarely known statement on when JIT compilation takes place.
--SA
VJ Reddy 27-Apr-12 22:45pm    
The addition JIT compilation is performed on the per-method basis is really a good informative addition.
Sergey Alexandrovich Kryukov 27-Apr-12 23:01pm    
Thank you. It's good to understand how things are optimized and to what extent.
--SA
I think this Code Project article
Using Unmanaged code and assembler in C#[^]
may be helpful.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Apr-12 22:41pm    
A good find, may be helpful, a 5.
--SA
VJ Reddy 27-Apr-12 22:46pm    
Thank you, SA.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900