Thoughts on assembly...

coldfusion244

Junior Contributor
Joined
Nov 24, 2004
Messages
266
Location
Philadelphia
Hello again,

I use C# at home, but C++ at school. In vc++.net and older versions there is _asm that let's you put in assembly code, for instance

_asm
{
push AX
mov dx, offset[ds]
out ax,dx
}

I was reading and there is no _asm directive that I found in C# (unless i'm blind which is half true), and I read the latest random thoughts thread and was wondering if there really is an _asm type directive or if i'd have to do it like it's shown in the random thoughts post.

-Sean
 
In the first place C# doesn't compile to assembly code. C# is compiled to intermediate language (IL) instructions so you really can't just dump a chunk of native code into the linker and have it work.

There is no _il keyword to let you type a chunk of native IL instructions either. I think this is because you should not be working at this level in c# or any other/net language. The compilers make a lot of optimisations and take into account far more than you'd expect, they are much more likely to make 100% correct optimisations than any single user is unless that used it very knowledgable.
 
Wraith said:
In the first place C# doesn't compile to assembly code. C# is compiled to intermediate language (IL) instructions so you really can't just dump a chunk of native code into the linker and have it work.
VC++.NET also compiles to MSIL correct? you can use __asm to directly add assembly into your code, I don't see why it wouldn't be possible then in c#.
 
coldfusion244 said:
VC++.NET also compiles to MSIL correct? you can use __asm to directly add assembly into your code, I don't see why it wouldn't be possible then in c#.

vc++ compiles managed code to IL and unmanaged code to native instructions. You can't use managed code in _asm. Read the docs and the articles, this sort of thing is covered.

There is also the problem that because IL is interpreted at runtime you can't debug at the IL level because there is no direct corresondance between the wto instruction sets.
 
Curious

Out of curiosity, what have you been doing (as in, to what intent) with assembler in your past c++ programming? And would you be wanting to get the same benefit or effect if you could do the same in managed .NET code? Inquiring minds want to know.

Thanks! :)
 
Richard Crist said:
Out of curiosity, what have you been doing (as in, to what intent) with assembler in your past c++ programming? And would you be wanting to get the same benefit or effect if you could do the same in managed .NET code? Inquiring minds want to know.

Thanks! :)


I have been using assembly in C++ because it is the only way that I knew of to use direct ASM without using something like MASM and having a DOS Emulated interface. Basically I have been making WinApps to control hardware, like 20x4 LCD displays, motors, etc. Since I know how to control them in assembly I figured why not use C++ to add a nice GUI so that I can control the hardware without having to stare (not that I don't like the asthetics) of the DOS Emulation screens... Bad enough I use edit/notepad to write the ASM... That and I like using the Tab control, Animations, and a nice GUI :p

As for wanting to do the same in managed .NET code, since I don't really know the difference between managed and unmanaged, so I can't fully answer that question (I'm not a professional .NET programmer). I'm used to VHDL, ASM, etc hardware languages. I've only recently been learning VB/C#/C++ trying to soak all of them up at the same time :o I'm finding that for what I do C++ is the best but I'm becoming very partial to C# for leasiurely programming...
 
Assembly is useful

coldfusion244 said:
Basically I have been making WinApps to control hardware, like 20x4 LCD displays, motors, etc.

Sounds like you have been a busy technical programmer. :cool: I started out in manufacturing using Fortran on DEC VAX's (a while back ;) ) so I can relate to your experience somewhat. Currently I program in good old-fashioned C. My first experience in "PC" and object-oriented programming has been with Visual Studio .NET 2003. I have recently posted questions about stuff related to what I have come to understand as "interoperability" which is basically, in my understanding, calling "old-fashioned" DOS/Windows COM routines (dll's ?) from .NET managed code. From my experience you have to include the System.Interoperability class to do this. I have done this to access the PlaySound routine, for example.

I blab about all this to say that you might be able to create dll's that call or use machine routines that you create. This is totally hypothetical on my part, but it seems feasible, even if current .NET standards would frown or worse on it. But for technical programming such as low-level IO in the form of motor, testing or other mechanical device control and monitoring it might still be useful.

Can a guru comment on this? :confused:

Thanks everyone! :)
 
Richard Crist said:
Sounds like you have been a busy technical programmer. :cool: I started out in manufacturing using Fortran on DEC VAX's (a while back ;) ) so I can relate to your experience somewhat. Currently I program in good old-fashioned C.

I blab about all this to say that you might be able to create dll's that call or use machine routines that you create. This is totally hypothetical on my part, but it seems feasible, even if current .NET standards would frown or worse on it. But for technical programming such as low-level IO in the form of motor, testing or other mechanical device control and monitoring it might still be useful.

Can a guru comment on this? :confused:

Thanks everyone! :)


Yeah, that's what makes it the hardest in my opinion. Going from the lowest level basic programming and having all these nice premade objects and fuctions right there at your fingertips. A great example is that when you implement a double (64 bits) on a 32 bit processor, you need 2x32 bit registers. In .NET it's great, does everything for you all you have to do is add the variables together. I am so used to breaking them up that I reinvent the wheel half the time I use .NET to program :mad:

But yes if a .NET guru could comment on this if it's possible or not... else I'll have to go back to C and ASM (Not that it's a bad thing).
 
There is no _asm directive in C#, you would either have to go through the method in the post you refer to or possibly put the asm in a C DLL and call that from C#.
 
PlausiblyDamp said:
There is no _asm directive in C#, you would either have to go through the method in the post you refer to or possibly put the asm in a C DLL and call that from C#.

Thanks plausy, I guess I'll have to do the DLL. :(
 
Back
Top