Over the weekend I had a friend ask me about MSIL. “How do you even get started understanding all that stuff?”, he asked. MSIL is actually fairly easy to understand and interpret once you get into it a little bit. Even writing simple code in MSIL can be pretty easy once you understand how it gets constructed and works. If you’ve looked at x86 compiled code before and said “no way can I understand this”, don’t dispair. MSIL stack architecture is much easier to understand.
So where do you start? I started here, Vijay Mukhi’s C# to IL. Its probably the best starting place for understanding MSIL on the internet. Its 15 chapters, all available on the website for your viewing pleasure, and an excellent read.
Once you’ve read that a couple of times, I would next get yourself a copy of the best MSIL book out there, Serge Lidin’s Inside Microsoft .NET IL Assembler. I’m actually in the process of rereading some of the chapters right now. This books stays on the bookshelf right next to my chair in the living room for easy access, because I’m always picking it up and referring to it, and I learn something new each time I pick it up, even if I’ve read the chapter before, twice 😉
Another more advanced place to go from the C# to IL article are the docs located in Tool Developers Guide/docs directory of the Framework SDK. You probably want to read those before you do anything else, but they cover a lot more than just MSIL. Regardless, these are all docs that you should probably be reading regardless of a specific topic you have in mind.
Understanding MSIL is an important part of understanding how your code gets compiled. When you understand what your code looks like in IL, especially when there are many different ways to accomplish the same task, you write better code. You can profile your code and examine it with an IL viewer like Reflector or ILDASM.Exe, which should be located on your machine in “C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\ildasm.exe”.
Currently listening to: Love Train – Big & Rich