Click here to Skip to main content
13,049,814 members (74,525 online)
Rate this:
 
Please Sign up or sign in to vote.
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 27-Apr-12 15:51pm
Comments
SAKryukov 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
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.
SAKryukov 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.
SAKryukov 27-Apr-12 23:01pm
   
Thank you. It's good to understand how things are optimized and to what extent.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I think this Code Project article
Using Unmanaged code and assembler in C#[^]
may be helpful.
  Permalink  
Comments
SAKryukov 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 27 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100