Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET
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 at 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 at 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 at 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 at 27-Apr-12 22:45pm
   
The addition JIT compilation is performed on the per-method basis is really a good informative addition.
SAKryukov at 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 at 27-Apr-12 22:41pm
   
A good find, may be helpful, a 5.
--SA
VJ Reddy at 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
0 George Jonsson 359
1 Sergey Alexandrovich Kryukov 355
2 CPallini 335
3 BillWoodruff 324
4 OriginalGriff 237
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 27 Apr 2012
Copyright © CodeProject, 1999-2014
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