The last update of the 3.5 SP1 seems to have broken NetAsm. There is no way to correct this (NetAsm is based on old SSCLI 2.0 source code project released in 2006).

NetAsm project is probably dead!

Just hope that Microsoft will improve their JIT using SIMD as it has been done for the Mono project (See Mono's SIMD Support).

Project Description

NetAsm provides a hook to the .NET JIT compiler and enables to inject your own native code in replacement of the default CLR JIT compilation. With this library, it is possible, at runtime, to inject x86 assembler code in CLR methods with the speed of a pure CLR method call and without the cost of Interop/PInvoke calls.

NetAsm can be used to integrate optimized native code using CPU extended instructions (SSE,MMX) into your managed code. The NetAsmDemo sample provides two benchmarks that unveil the power of using native code injection with NetAsm.
For more information about NetAsm, code injection techniques and recommendations, please consult the NetAsm-UserGuide.

NetAsm wouldn't have been possible without the brilliant article “.NET Internals and Native Compiling” from Daniel Pistelli.


25 july 2008, NetAsm 1.0 is released.


  • Runs on x86 32bit Microsoft .NET platform with 2.0+ CLR runtime (x64 may be supported in the future).
  • Provides three different native code injection techniques: Static, DLL, and Dynamic.
    • Static code injection: The native code is stored in an attribute of the method.
    • Dll code injection : this method is similar to the DllImport mechanism but CLR methods are directly bind to the DLL function, without going through the interop layers.
    • Dynamic code injection: you can generate native code dynamically with a callback interface that is called by the JIT when compilation of a method is occurring. It means that you can compile a method “on the fly”. You have also access to the IL code of the method being compiled.
  • Supports for debugging static and dynamic code injection.
  • Supports for different calling conventions: StdCall, FastCall, ThisCall, Cdecl. Default calling convention is CLRCall.
  • NetAsm can be used inside any .NET language.
  • Very small library <100Ko.

Future plans

You are welcome to vote for the following features (or any new features) or even contribute to one of them if you have a good experience in such domain:
  • Add a simplified assembler to code inject textual asm instructions from CodeInjection attribute?
  • Add support for 64bit platform?
  • Add support for Mono platform?

HelloWorld example

This sample is available in NetAsmDemo and is fully documented in the the NetAsm User guide.

Using native code injection with NetAsm is a very simple task that can be achieved in two steps:
1) Specify the native code injection in a class
2) Install the hook in the main program of your application

Setup native code injection in a class

using System;
using System.Runtime.CompilerServices;
using NetAsm;

namespace NetAsmDemo
    class TestHelloWorld
        [CodeInjection(new byte[] { 0xC3 }), MethodImpl(MethodImplOptions.NoInlining)]
        static public void NetAsmReturn()
            throw new Exception("With NetAsm, You should not have an exception!");

The steps to allow and use code injection on a method in a class are:

1. Set AllowCodeInjection attribute on the class you want to do code injection.
2. Set CodeInjection attribute on the method that will be injected with native code
3. For void NetAsmReturn() method in TestHelloWorld, the native code used is : new byte[] { 0xC3 }. In x86 assembler, it’s the “RET” (return) command. This method does nothing more than immediately returning after a call.
4. In our example, we have set the attribute MethodImpl(MethodImplOptions.NoInlining) : This attribute force the JIT to not inline the IL code inside the method. This is for the purpose of the demonstration but should be used with caution (see usage recommendation chapter).

That’s all to use code injection!
In NetAsm, this kind of native code injection is called static native code injection. TestHelloWorld only use static code injection at the method level. We will see later that NetAsm provides other code injection techniques.
Now, to run this code injection test, we need to install NetAsm JITHook.Install().

Install the hook and run the code injection

To call the TestHelloWorld method, the main program has to initialize NetAsm:
using System;
using NetAsm;

namespace NetAsmDemo
    class Program
        static void Main(string[] args)
            // Install the JIT Hook
            // Run TestHelloWorld Method
            // Remove the JIT Hook
If you run this program, it will return without any exception. While executing this code, the CLR Virtual Machine use our native code “RET” command instead of the IL code inside the method. It means that the original IL code is not compiled by the default JIT compiler.

Additionnal information and other code injection techniques are available in the NetAsm User Guide.

Blog : Code4k

Last edited Oct 13, 2009 at 8:44 AM by alexandre_mutel, version 25