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++

_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
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.
Rate this: bad
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:[^],[^],[^],[^].

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:[^],[^],[^],[^].

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:[^],[^].

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

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.
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.
Rate this: bad
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.
SAKryukov 27-Apr-12 22:41pm
A good find, may be helpful, a 5.
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