Including Assmebler code into CSharp???

  • Thread starter Thread starter Joe DeAngelo
  • Start date Start date
J

Joe DeAngelo

Is it possible to include assembler code into CSharp?
If yes, could someone give me an example?

Joe
 
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
 
Ryan Riddell said:
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 said:
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#.
 
Daniel Jin said:
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;
 
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 said:
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.
 
Daniel Jin said:
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.)
 
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 said:
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,
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
 
Back
Top