Click here to Skip to main content
15,908,907 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hi,
How to mix assembly code in C# ?

I remember in pascal we could mix like this:

Delphi
function GetCharacter ( x, y : word ) : byte; assembler;
asm
PUSH    BP
MOV BP,SP
PUSH    SI
PUSHF
CLD
{...}
end;


But, what's the solution in C#?

Thank you.
Posted
Updated 12-Feb-12 21:46pm
v2

No way. C# is a managed code but assembly is unmanaged.
You can only import unmanaged dlls to your program.
use [DllImport("dllname.dll")]
 
Share this answer
 
v2
Comments
Fardan 13-Feb-12 4:31am    
Thank you Maria
Sergey Alexandrovich Kryukov 14-Feb-12 11:06am    
Correct answer, but incorrect explanation. Fully incorrect.
.NET application can mix managed + unmanaged, even easily.
The problem is deeper. I would guess, such feature would be just too hard to implement, due to JIT-compiled nature of architecture, and the results would be totally non-portable.
--SA
You can write a C dll, and then invoke the C code from C#. That's how I do it - and by the way, that is described in Abhinav's first link above.
 
Share this answer
 
 
Share this answer
 
Comments
Fardan 13-Feb-12 3:56am    
Thanks but the first link is not about calling assembly code in C#, It's about calling unmanaged DLLs from C#. The second link is irrelevant.
Manfred Rudolf Bihy 13-Feb-12 4:33am    
If you had read the article completely you'd know by now that calling assembler from C# directly is not possible. That is why OP of said article used the feature of calling code from an unmanaged DLL wherein he used assembler. Voilá!
Fardan 13-Feb-12 4:34am    
I've read them completely Manfred
Sergey Alexandrovich Kryukov 14-Feb-12 11:09am    
Good references to non-trivial articles, especially the second one, my 5.
No wonder 2/6 votes was "1" -- this is how the weak-minded aggressively react to anything disturbing -- my congratulations. :-)
--SA
Abhinav S 14-Feb-12 22:41pm    
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