By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

Including Assmebler code into CSharp???

P: n/a
Is it possible to include assembler code into CSharp?
If yes, could someone give me an example?

Joe

Nov 16 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Joe DeAngelo <de*******@nortel.com> wrote:
Is it possible to include assembler code into CSharp?
If yes, could someone give me an example?


No, it's not possible.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
I'm fairly sure you can't do that. CSharp compiles into intermediate language (IL) + Metadata. From there at runtime the IL is compiled into machine specific code. Writing assembler code would break this chain. You can however, execute other programs from within your C# code. So if you could compile the Assembly code into an executable you should be able to run that from your C# code.

Check out the method 'System.Diagnostics.Process.Start'

HTH

"Joe DeAngelo" wrote:
Is it possible to include assembler code into CSharp?
If yes, could someone give me an example?

Joe

Nov 16 '05 #3

P: n/a
Ryan Riddell <Ry*********@discussions.microsoft.com> wrote:
I'm fairly sure you can't do that. CSharp compiles into intermediate
language (IL) + Metadata. From there at runtime the IL is compiled
into machine specific code. Writing assembler code would break this
chain. You can however, execute other programs from within your C#
code. So if you could compile the Assembly code into an executable
you should be able to run that from your C# code.


What would occasionally be useful would be the ability to include ILASM
in C# code - but you can't do that, either :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ryan Riddell <Ry*********@discussions.microsoft.com> wrote:
I'm fairly sure you can't do that. CSharp compiles into intermediate
language (IL) + Metadata. From there at runtime the IL is compiled
into machine specific code. Writing assembler code would break this
chain. You can however, execute other programs from within your C#
code. So if you could compile the Assembly code into an executable
you should be able to run that from your C# code.


What would occasionally be useful would be the ability to include ILASM
in C# code - but you can't do that, either :(


Or even link an ILASM program and a C# one in the same module (the same
*assembly* when using VS.NET). I have an entire assembly containing two
methods I couldn't write in C#.
Nov 16 '05 #5

P: n/a
Daniel Jin <Da*******@discussions.microsoft.com> wrote:
What would occasionally be useful would be the ability to include ILASM
in C# code - but you can't do that, either :(


Jon, I once had thought of this as well. but when I trying to find
what I can accomplish in IL that C# doesn't offer, I can't really
find anything particularly important. do you have anything in mind
where IL is absolutely necessary? very curious on what you can come
up with.


Well, two possibilities spring to mind:

1) Sometimes you can handwrite IL which is faster (after JITting) than
what the C# compiler comes up with. Someone posted an example of this
once.

2) You can replace boxed values without unboxing/reboxing. For
instance, in a hashtable mapping string to int, you could increment the
value of the boxed int without all the

int x = (int)table[key];
x++;
table[key]=x;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
de*******@nortel.com (Joe DeAngelo) wrote:
Is it possible to include assembler code into CSharp?
If yes, could someone give me an example?


Well, pardon my newbieness, but couldn't you include it as a resource or
DLL?

One of my books gives an example of writing a DLL in C#, but the finished
product - mustn't it be machine code?

The Doormouse

--
The Doormouse cannot be reached by e-mail without her permission.
Nov 16 '05 #7

P: n/a
The Doormouse <do*******@att.net> wrote:
de*******@nortel.com (Joe DeAngelo) wrote:
Is it possible to include assembler code into CSharp?
If yes, could someone give me an example?
Well, pardon my newbieness, but couldn't you include it as a resource or
DLL?


Well, it would have to be a separate DLL if you want to compile with
VS.NET.
One of my books gives an example of writing a DLL in C#, but the finished
product - mustn't it be machine code?


Nope, it's IL - the JIT compiler turns it into native code at runtime.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

P: n/a
"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:6E**********************************@microsof t.com...


"Jon Skeet [C# MVP]" wrote:
Ryan Riddell <Ry*********@discussions.microsoft.com> wrote:
I'm fairly sure you can't do that. CSharp compiles into intermediate
language (IL) + Metadata. From there at runtime the IL is compiled
into machine specific code. Writing assembler code would break this
chain. You can however, execute other programs from within your C#
code. So if you could compile the Assembly code into an executable
you should be able to run that from your C# code.


What would occasionally be useful would be the ability to include ILASM
in C# code - but you can't do that, either :(


Jon, I once had thought of this as well. but when I trying to find what I

can accomplish in IL that C# doesn't offer, I can't really find anything
particularly important. do you have anything in mind where IL is absolutely
necessary? very curious on what you can come up with.

You can make non-virtual calls to virtual methods, like the C++ syntax

ptr->definingClass::method()

I needed this in .NET 1.0 to access the System.Object version of
GetHashCode() on arbitrary objects. In .NET 1.1, there's another way to do
this (which I don't offhand recall.)
Nov 16 '05 #9

P: n/a
Jon,

I used to advocate adding "inline ILAsm" support too, but I no longer
think the few situations where it would be useful justifies the
resources it would take to implement it.

To me, the Whidbey VC++ linker's support for linking modules into a
single file assembly is a better solution to the problem.

2) You can replace boxed values without unboxing/reboxing. For
instance, in a hashtable mapping string to int, you could increment the
value of the boxed int without all the

int x = (int)table[key];
x++;
table[key]=x;


If you do that a lot, perhaps you should consider writing a wrapper
class around the int and store that in the hashtable instead, to avoid
the (un)boxing.

class IntWrapper { public int x; }
(table[key] as IntWrapper).x++;

And then in Whidbey, you'd probably use a Dictionary<string,int>
instead.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #10

P: n/a
Mattias Sjögren <ma********************@mvps.org> wrote:
I used to advocate adding "inline ILAsm" support too, but I no longer
think the few situations where it would be useful justifies the
resources it would take to implement it.
Oh I'd certainly have no problem with that argument. I was just giving
examples of where it could be useful had it been implemented.
To me, the Whidbey VC++ linker's support for linking modules into a
single file assembly is a better solution to the problem.
Is that available outside VC++ though? Also, can you create ILASM
modules within Whidbey?
2) You can replace boxed values without unboxing/reboxing. For
instance, in a hashtable mapping string to int, you could increment the
value of the boxed int without all the

int x = (int)table[key];
x++;
table[key]=x;


If you do that a lot, perhaps you should consider writing a wrapper
class around the int and store that in the hashtable instead, to avoid
the (un)boxing.


If it were easy to avoid the boxing/unboxing *without* creating a
wrapper, however, I'd do that. There are obviously ways round these
things :)
class IntWrapper { public int x; }
(table[key] as IntWrapper).x++;

And then in Whidbey, you'd probably use a Dictionary<string,int>
instead.


Oh indeed - it was just an example, really. As you say, it would have
been more useful in .NET 1 than .NET 2.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11

P: n/a
Jon,
Is that available outside VC++ though?
Yes, to anything that can launch link.exe.

Also, can you create ILASM modules within Whidbey?


I haven't tried it myself, but considering how flexible MSBuild is I
don't think it would be a problem to do this if you want to.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.