tags:

views:

116

answers:

4

I have spent hours on a debugging problem only to have a more experienced guy look at the IL (something like 00400089 mov dword ptr [ebp-8],edx ) and point out the problem. Honestly, this looks like Hebrew to me - I have no idea what the heck it's saying.

Where can I learn more about this stuff and impress everyone around me? My goal is to read stuff like the following and make a comment like: Yeh, you are having a race condition.

.maxstack 2
.entrypoint
.locals init (valuetype [MathLib]HangamaHouse.MathClass mclass) 

ldloca mclass
ldc.i4 5
+6  A: 

That is not MSIL, it is assembly langauge 80x86.

Bill
No wonder I didn;t know what's going on.
RonT
Having worked in assembly for several years, many years ago, most recently on an AES cryptographic routine, I can tell you that you should not attempt to dilute your efforts learning c# by attempting to bone up on assembler. If your original code was in c# you should have been able to detect a race condition in the original code.
Bill
+2  A: 

To get great at IL, start with this fantastic article: Introduction to IL Assembly Language. Although it says "introduction", it's everything you need to start getting comfortable.

The other part of what you need is practice and lots of it. Use .NET Reflector and start looking at code disassembled into IL. (Tip: when you go to download it, you don't have to provide a real email.) Also, play with the Reflexil plugin in Reflector. Here's a good starting point for that: Assembly Manipulation and C# / VB.NET Code Injection.

Not necessary but a bonus: Reflexil is open source. You can get the source here.

Dinah
+2  A: 

I can give you an answer that runs both ways.

On the one hand, there's nothing like good assembly language skills to teach you how a computer really operates. MSIL is, to some extent, an assembly-like language. On the down side, there are very few opportunities to do this kind of development any more.

On the other hand, resorting to looking at the MSIL to fix a problem is not necessarily the most direct or educational way to understand a problem. In five years of .NET programming, I've never felt the need to go there. Only once has a co-worker (who had worked at Microsoft on compiler testing) gone there with a problem that I was trying to solve, and in the end, his answer was misleading, because the real issue was based in CLR design and constraints. Better knowledge of the CLR and C# would have led to a better understanding and a real solution.

(In case you're wondering, the problem was that I wanted to use "as" for safe casting with a generic. "as" didn't work, but "is" did. My co-worker noted that "is" and "as" use the same MSIL. The real problem is that "as" only works for casting classes, and without the proper constraint on the generic declaration, C# doesn't know whether your generic type will be a class. In fact, the types I was using with generics were value types; "as" couldn't work for those at all.)

Rather than going for MSIL skills, I highly recommend Jeffrey Richter's book CLR via C#. Even after years of digging hard at into C#, this book is still full of revelations - I learn something from every page.

Cylon Cat
The reason `as` doesn’t work with value types is because value types can’t be `null`, but `as` wants to return `null` if the variable is not of the provided type. If you know that your generic type parameter `T` is a value type (i.e. you’ve specified the `struct` constraint), you can use `as T?` instead of `as T` and it will work even if the instance is actually of type `T` and not `T?`. However, this is a purely C# language issue and has little to do with IL or the CLR...
Timwi
@Timwi, exactly right; "as" can't return a null on a value type, and this has **absolutely** nothing to do with IL. That's the point; learning IL doesn't help you solve cases like this. Whether this is purely a language issue, or involves CLR implementation of generics, I couldn't say, but I will note that VB.NET's handling of casting (via DirectCast or TryCast) has much the same problem.
Cylon Cat
+3  A: 

Can’t say I’m an IL “pro”, but I managed to teach myself pretty much all of IL by doing the following:

  • Write a very short (two or three line) C# program that you are curious about how to write in IL.

  • Compile the program.

  • Open the compiled EXE in .NET Reflector.

  • Look at the IL code for the method in Reflector.

  • Hover your mouse over an IL opcode (e.g. “ldloc”). There is a tooltip describing each IL instruction.

Timwi