__asm in C#

B

Bill

I feel pressure to start using C# but I'm worried about losing
control.

It appears C# does not allow "__asm" for those "if it don't work,
force it" situations that crop up. Is that true? Is there any way to
get close to the machine with C#? How does one use assembler
language in C#? Can I get to MFC? How about low level functions
like "_open" and _clock()" available in straight C - are they
available in C#?
 
D

Daniel O'Connell [C# MVP]

Bill said:
I feel pressure to start using C# but I'm worried about losing
control.

It appears C# does not allow "__asm" for those "if it don't work,
force it" situations that crop up. Is that true? Is there any way to
get close to the machine with C#? How does one use assembler
language in C#? Can I get to MFC? How about low level functions
like "_open" and _clock()" available in straight C - are they
available in C#?
No, C# is not actually derived from C/C++, it is a C style language similar
to java. C# exists entirely within a managed runtime called the CLR(VB.NET
also exists within this runtime), which provides garbage collection and
other services to your application. C# doesn't directly compile to machine
code either, it compiles to an intermediate assembly language (IL), which is
converted into machine code at runtime.
Frankly, its not meant for situations where you need to worry about
performance or behaviour to hte point wherey ou would want to break into
assembly.

Likewise, MFC is a C++ framework and is not available in C#. You can use
Managed C++ however which provides you a mixture between managed and native
code using C++.
 
C

codymanix

You can simply call every native function from every DLL you want.
In C# itself you cannot use assembler, you have to put your asm code in a
DLL.
 
A

Arthur Mnev

I missed it too for a while :) - C# automates a lot of memory managmenet;
hence __asm is out of the question; also, C#'s pointer functionality is very
very limited (you can use them but working with (Manager/Unmanaged Code) &
pointers is painful.

I'd recommend going Managed C++ if you need ASM in there you could run both
Managed and Unmanaged code in the same place; - probably will ease your pain
 
W

Willy Denoyette [MVP]

Some remarks;
C# doesn't automate memory management, the CLR does it.
You don't use _asm in C++ only to use pointers or do some pointer
arithmetic's, but because you have some highly optimized assembly code to
merge with your C++ code.
_asm is not allowed in MEC++, for the same reason it's not done in C#.
If you need this, your only option is (unmanaged) C++.

Willy.
 
R

Rob Teixeira [MVP]

Also, as an aside, there's a few projects to come up with abstract IL
compilers (similar to ASM, but writes IL instead of native op codes).
Although, my suspicions are that this won't become part of C# unless there's
an incredibly good reason to do it (and a lot of pressure). More likely, you
can create an assembly of highly-optimized IL, and use it from your C#
programs. Additionally, C# does a pretty decent job of optimization, and
furthermore, the JIT compiler optimzes the IL to native code, so specifying
IL instructions directly isn't the end-all to optimization, and there's no
guarantee the JIT compiler won't do something unexpected behind your back.

-Rob Teixeira [MVP]
 

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