-1

Possible Duplicate:
Retrieve JIT output
Generating Assembly from C# code?

I started to learn assembler!

I would like to know if it is possible to see the assembly code for my c# code?

For example if I have:

int i;

How can I:

  1. see the assembly code?
  2. identify exactly in the assembly code what that does?
Community
  • 1
  • 1
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • Have you seen: http://stackoverflow.com/questions/9247222/generating-assembly-from-c-sharp-code – Dave Hogan Oct 15 '12 at 21:42
  • You may be interested in seeing the compiled IL byte code, which is one step above assembly, but still very, very low level. .NET Reflector can do this, for example. – Cameron Oct 15 '12 at 21:43
  • C code is much more easily translated to assembly. Still, it is usually much easier to learn assembly on its own than to reverse engineer compiled code. – TheZ Oct 15 '12 at 21:45
  • FYI, `int i` is not nearly enough context to actually compile; you've given a symbol a type, but since you haven't done anything with the symbol, nothing happens. A valid C# program at the very minimum requires at least one class. – Dan Bryant Oct 15 '12 at 21:47
  • @DanBryant what about public class myClass {} ? – Alex Gordon Oct 15 '12 at 21:48
  • In that case, you will have a single public method (.ctor) which will compile (jit) to some assembly code for calling the base (Object) class constructor. – Dan Bryant Oct 15 '12 at 21:50
  • cool! but there is no constructor – Alex Gordon Oct 15 '12 at 21:51
  • Consider starting with basic C code - much easier to get reasoning about C function to assembly transformation than C# classes to IL to Assmembly... – Alexei Levenkov Oct 15 '12 at 21:59
  • Assuming "int i" is part of a function definition, and forgoing optimisation, I'd expect that (on x86) to translate to something like, "sub esp, 4" which makes room on the stack for a 32 bit value, but you need to see it in context really and the code produced will also vary by platform of course. – cirrus Oct 15 '12 at 22:28

2 Answers2

2

Use the debugger to see the machine code that's generated by the jitter. Start debugging and single step or set a breakpoint. Right-click the editor window, select Go To Disassembly. Or use Debug + Windows + Disassembly. Other debug windows you'll want to use are Registers and Memory, they show the raw cpu register and memory view. Right-click the Registers window to add registers to the view.

There are a few quirks about the assembly code you see there. All code starts at address 0, it doesn't show the actual address since that's entirely unpredictable due to the jitter. But that also causes a bug, the target address of CALL instructions is wrong. And you typically want to see the optimized code since that's what actually runs on the user's machine. Switch to the Release build and use Tools + Options, Debugging, General, untick the "Suppress JIT optimization on module load" option.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

The assembler code only usually gets written at JIT time, so the best place to view it, especially if you want to learn about it is to look in the VS debugger.

http://msdn.microsoft.com/en-us/library/a3cwf295.aspx

If you mean the IL (intermediate language), this isn't assembly language, but it's stored in your EXE or DLL. A good tool for looking at that is ILSpy.

http://ilspy.net/

cirrus
  • 5,624
  • 8
  • 44
  • 62