Can embed asm code in c#

H

harvie wang

I want to embeded ASM code in c# code ,like C++ embed asm:
_asc{
mov ax,3
.....
}

how to do?

thanks!
 
D

Daniel O'Connell [C# MVP]

harvie wang said:
I want to embeded ASM code in c# code ,like C++ embed asm:
_asc{
mov ax,3
....
}

how to do?

You can't. C# compiles in a relativly machien independent manner, so x86
assembly wouldn't make sense.
 
M

Mike Schilling

Daniel O'Connell said:
You can't. C# compiles in a relativly machien independent manner, so x86
assembly wouldn't make sense.

And you can't embed ILASM either, as far as I know, even though that would
make sense (and even be useful on rare occasions.)
 
G

Guest

And you can't embed ILASM either, as far as I know, even though that would
make sense (and even be useful on rare occasions.)

thats right and infact writing code in Ilasm inside C# (like __asm in c++)
was submitted as a suggestion to microsoft for C# 2.0 and got a lot of
positive votes but still it was rejected cuz they say it was complicating the
language and nothing valueable enough could be achieved through this feature.

Abubakar,
http://joehacker.blogspot.com
 
D

Daniel O'Connell [C# MVP]

Abubakar said:
thats right and infact writing code in Ilasm inside C# (like __asm in c++)
was submitted as a suggestion to microsoft for C# 2.0 and got a lot of
positive votes but still it was rejected cuz they say it was complicating
the
language and nothing valueable enough could be achieved through this
feature.

I tend to agree. Adding an entire sublanguage and primary langauge hookups
for a feature that has very minimal value doesn't make sense. Adding ILASM
could double the complexity of the compiler, IDE, and language to help the
handful of people that would actually use it properly.
 
H

harvie wang

I use Reflector to Get ILASM code ,but I can't edit it;
in c# code ,create a new empty function named FunA,such as:

public class test
{
public test(){};
public void FunA(){};
}
then build this code ,I get a test.dll file
Open the test.dll file with Reflector
I get ILASM code
can I edit this code and add ILASM code to the Empty function FunA()?

thanks!
 
M

Mike Schilling

Daniel O'Connell said:
I tend to agree. Adding an entire sublanguage and primary langauge hookups
for a feature that has very minimal value doesn't make sense. Adding ILASM
could double the complexity of the compiler, IDE, and language to help the
handful of people that would actually use it properly.

Actually, I agree too. Allowing ILASM and C# files to be linked into the
same assembly would solve the same set of problems much more naturally. (It
still strikes me as bizarre that a system deisgned from day one to support
multiple languages and compiles them all to a common object format doesn't
support heterogeneous linking.)
 
D

Daniel O'Connell [C# MVP]

Actually, I agree too. Allowing ILASM and C# files to be linked into the
same assembly would solve the same set of problems much more naturally.
(It still strikes me as bizarre that a system deisgned from day one to
support multiple languages and compiles them all to a common object format
doesn't support heterogeneous linking.)

Ya, that was always odd, although the system itself supports it, the two big
languages(VB and C#) do not. C++ might, I'm not sure off hand. It is
certainly possible at the lowest levels.

It'd be nice if they would just provide an IDE that supports it, not to
mention a good IDE for editing IL code in VS. I'd love to have a good
Reflecter clone in VS, although I know that it would potentially give MS
problems for providing decompilers.
 
M

Mike Schilling

Jeff Louie said:
You can code in IL assembler and call the dll method from c#

I do that now; it just seems stupid to have an entire assembly to hold the
three methods I can't write in C#.
 
A

Abubakar

u can also do this: use ildasm to save a file in il file and than change
with notepad and compile with ilasm.

Ab.
 
J

Jeff Louie

OK..
Here is the assembler code that prints a string to the console wrapped
in a dll
call testildll. To compile this code is the ilasm with the /dll
directive:
ilasm testildll.il /dll

You can then add a reference to the dll in your C# project and call the
method.

// testildll.il
// 11.29.04 Jeff Louie
.assembly extern mscorlib {}
.module testildll.dll
.class public auto autochar Class2
{
.method public hidebysig static void Test() cil managed
{
.maxstack 1
ldstr "JAL 11.25.04"
call void [mscorlib]System.Console::WriteLine(string)
ret
}
}

Regards,
Jeff
 
H

harvie wang

Hello Jeff,

hoho:)
I see
Thanks!

OK..
Here is the assembler code that prints a string to the console wrapped
in a dll
call testildll. To compile this code is the ilasm with the /dll
directive:
ilasm testildll.il /dll
You can then add a reference to the dll in your C# project and call
the method.

// testildll.il
// 11.29.04 Jeff Louie
.assembly extern mscorlib {}
.module testildll.dll
.class public auto autochar Class2
{
.method public hidebysig static void Test() cil managed
{
.maxstack 1
ldstr "JAL 11.25.04"
call void [mscorlib]System.Console::WriteLine(string)
ret
}
}

Regards,
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top