Inline assembly

  • Thread starter Thread starter Nak
  • Start date Start date
N

Nak

Hi there,

I thought I would just ask this question (though I'm sure I have
before), why can you use inline assembly in C# and not VB.NET? And would
there be any way to do this in VB? I saw a solution a long time ago via
pre-compiled assembly, but I was hoping for something a little easier to
change. Anyway, just wondered, thanks in advance!

Nick.
 
My appologies, can you actually use assembly in C#? I have just been led to
believe that I was talking crap, but I would still like to know how to do it
nicely in VB.NET if pos...
 
Nak said:
I thought I would just ask this question (though I'm sure I have
before), why can you use inline assembly in C# and not VB.NET?

JFMI: What's "inline assembly"?
 
Inline assembly was a technique that enabled C++ programmers to put I386
assembly language directives into the C++ code. It was a horrible hack that
made all kinds of assumptions about the processor (in a portable language?)
and was used quite often when something fast and low-level was needed.

C# does not allow you to put inline assembly in the source. Nor does VB.

A good job too.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Bob,

Bob Powell said:
Inline assembly was a technique that enabled C++
programmers to put I386 assembly language directives
into the C++ code. It was a horrible hack that made
all kinds of assumptions about the processor (in a portable
language?) and was used quite often when something fast and
low-level was needed.

Well, I know that, but I didn't think of that.

I was thinking about a keyword that allows to "inline" a .NET assembly into
another C# project which VB.NET doesn't have. That's why I asked to make
sure we are talking about the same thing :-).
C# does not allow you to put inline assembly in the source.
Nor does VB.

ACK.
 
Hi Bob,
C# does not allow you to put inline assembly in the source. Nor does VB.

Yeah I realise this now, but manages C++ can still do it right?
A good job too.

Well, I wouldn't exactly say that. Assembly is still one of the fastest
way to perform mathematics, and probably graphical operations without
utilising hardware. I just wanted to know as I could see this being an
alternative to graphical effects rather than relying on GDI and GDI+ which
don't always cut the mustard.

I've seen it done on planet source code before in a VB6 application
manipulating graphics, and this proved to be quite fast. I'd love to see a
bitblt routine programmed in assembly, though I'm sure that's what the GDI
API is written in, it's just a matter of it being well written assembly or
not I suppose.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=41320&lngWId=1

^ *think* that might have been the example I saw, though I can't double
check as I don't have VB6 anymore. But anyway, regardless, if your careful
enough with any technology there is no reason why you shouldn't use it,
assembly code doesn't have to cause problems, it never used to (when written
correctly) did it? And as far as I'm aware assembly is still used quite
allot in the demo scene, which I've always been impressed with because of
the sheer speed of graphical manipulation on low spec computers.

http://www.scene.org/

Nick.
 
C++ can but managed C++ cannot. The whole idea of a managed language is that
it compiles to a typesafe and verifiable code (IL) which is JIT compiled to
the final native code, whatever that may be.

It's hardly typesafe to allow people to mess about with the CPU registers in
the middle of a statement.

Having used inline assembly on numerous occasions I understand it's
advantages but I still think it's a good idea not to consider it for all but
the most critical of applications considering the power and efficiency of
modern compilers.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hi Bob,
C++ can but managed C++ cannot. The whole idea of a managed language is
that
it compiles to a typesafe and verifiable code (IL) which is JIT compiled
to
the final native code, whatever that may be.

Yup, but cant you have a mixed assembly? As I understand, the IL is
embedded inside on compilation.
It's hardly typesafe to allow people to mess about with the CPU registers
in
the middle of a statement.

ACK, I totally agree with you. But as a programmer I like to experiment
with things, and I'm sure if thoroughly tested you can make some nice little
methods that would give any app a speed boost if used correctly.
Having used inline assembly on numerous occasions I understand it's
advantages but I still think it's a good idea not to consider it for all
but
the most critical of applications considering the power and efficiency of
modern compilers.

Well, I partly agree with that, but at the end of the day I'm 100% self
taught and I am still to learn assembly, using "inline" in my favourite
language would be the best way for me as I could probably find practical
uses for it, as well as knocking up some examples for my web site so other
people know how to do it.

I wouldn't really consider GDI or GDI+ as an alternative to assembly if
you want sheer speed, would you? I understand that it's allot easier to use
but that's one of the reasons it's so slow. I've had to learn Direct3D just
to get some decent performance out of graphics on my PC, and the only bottle
neck is the part written in GDI+, which is a shame, hence my interest in
messing around with a bit of assembly. But anyway, I wouldn't worry, I
wasn't planning on doing inline assembly willy nilly! ;-)

Nick.
 

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

Back
Top