Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I was wondering if I could invoke directly in C# assembly code (i must optimize a loop which involves some math computations and I was thinking I should write them in assembly to speed it up).
However, I'ved google'it and all I could find was some sort of invoking the assembly code in C and then using P/Invoke in C# .
Is there a way that I could invoke the assembly directly from C#?

Thanks in advance !
Posted

It would be quite possible to write a dll in assembly language and P/Invoke that from C# but you would be making life inordinately difficult for yourself.

I would just hack together a dll in C++ and write the assembler stuff in there.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Dec-10 15:44pm    
Yes, Phil, you're right. I totally agree.
Sergey Alexandrovich Kryukov 25-Dec-10 15:47pm    
...and, after all, one could also insert in-line assembly in that C++ code if it even makes any sense. In this case, mixed-mode target (both native and CLR) should be used for C++ project.
As creating a library from Assembly Language source code and using P/Invoke to link it to C# project is already direct enough ;), I think what you have in mind is something like "inline assembly" available in C++ or Borland Pascal/Delphi code.

In this way, you could actually mix C# and assembly code, but -- no, you can't, but you should better understand why not and also understand that you really don't want it.

You can not do such thing, because .NET assembly is not compiled in any CPU's code. The compilation is done by JIT when the code is already loaded; and this is done gradually, on per-method basis (so, for example, method which never called will not be compiled). This makes things amazingly portable. A good deal of my applications I develop I also run on Linux, even UI applications. The assembly has target CPUs specified in it; and it can be some class of Intel CPU (x86 (32-bit architecture), x86-64 and Itanium (two different 64-bit architectures)), but this is no more than a simple "tag" in the executable image; and the default is "Any CPU" (using auto-detected CPU);

If "inline assembly" existed its code would not get a target to be compiled into.

(Well, I could fantasize remote possibility that such high-level .NET language with "inline assembly" feature can be created, but all it could do is automating process of creating if native assembly in linking is using PInvoke. I don't think there will be any practical justification for such work.)

Don't confuse, however, assembly language with IL. Yes, any assembly can be "disassembled" into IL, so IL plays a role of Assembly Language in the word of .NET in that sense it is low-level. IL also target .NET byte code (it actually a text human-readable representation of the byte code resembling tradition Assembly Language), it does not target CPU instruction code.

You could also optimize IL code and compile it back. There are few interesting and useful works where people automatically disassemble an assembly, tweak IL code and assemble it back during build step behind the scene. This is because IL has some features not available in other languages, and because with IL you can gain more fine-grain control.

I don't think it can give you much. I would advice to try regular P/Invoke.
Good luck!
 
Share this answer
 
Comments
tamash_ionut 25-Dec-10 16:27pm    
I'ved came across the following link that does something similar of what I had in mind :

http://www.atrevido.net/blog/CommentView,guid,ac03f447-d487-45a6-8119-dc4fa1e932e1.aspx#commentstart

However this throws an error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Is this the inline assembly that you'ved mention?If so how could I make this work and where could I get more native instructions like this ? (like 0x89,0xD0 is MOV EAX,EDX )
I wouldn't go this far if I didn't need some very good performance (real-time image processing does involves some serious math :) )
Sergey Alexandrovich Kryukov 26-Dec-10 14:22pm    
You reference does not work
Espen Harlinn 26-Feb-11 11:26am    
Good effort - my 5
Sergey Alexandrovich Kryukov 26-Feb-11 14:44pm    
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