views:

1048

answers:

6

I was reading another question pertaining the efficiency of two lines of code, and the (asker?, questioner?, querier?) said that he looked at the assembly behind the code and both lines were identical in assembly. Digression aside, how could I view the assembly code created when a program is compiled.

I'm using Microsoft's Visual C++, but I would also like to know if it's possible to view the assembly behind code written in Visual Basic.

So, how do I view the assembly code behind a program written in higher level languages like C++ and Visual Basic?

+1  A: 

In Visual C++ the project options under, Output Files I believe has an option for outputing the ASM listing with source code. So you will see the C/C++ source code and the resulting ASM all in the same file.

jcopenha
+2  A: 

The easiest way is to fire the debugger and check the disassembly window.

You can call me Chuck
+8  A: 

There are several approaches:

  1. You can normally see assembly code while debugging C++ in visual studio (and eclipse too). For this in Visual Studio put a breakpoint on code in question and when debugger hits it rigth click and find "Go To Assembly" ( or press CTRL+ALT+D )

  2. Second approach is to generate assembly listings while compiling. For this go to project settings -> C/C++ -> Output Files -> ASM List Location and fill in file name. Also select "Assembly Output" to "Assembly With Source Code".

  3. Compile the program and use any third-party debugger. You can use OllyDbg or WinDbg for this. Also you can use IDA (interactive disassembler). But this is hardcore way of doing it.

inazaruk
A: 

Red Gate's .NET Reflector is a pretty awesome tool that has helped me out more than a few times. The plus side of this utility outside of easily showing you MSIL is that you can analyze a lot of third-party DLLs and have the Reflector take care of converting MSIL to C# and VB.

I'm not promising the code will be as clear as source but you shouldn't have much trouble following it.

Dave L
Note: only applicable to managed assemblies not to disassembly as in assembler, asm.
sean e
Good point, I read it as a "are the two lines of code the same in the assembly" instead of "are the two lines of code the same in assembly"
Dave L
+1  A: 

Specify the /FA switch for the cl compiler. Depending on the value of the switch either only assembly code or high-level code and assembly code is integrated. The filename gets .asm file extension. Here are the supported values:


/FA Assembly code; .asm /FAc Machine and assembly code; .cod /FAs Source and assembly code; .asm /FAcs Machine, source, and assembly code; .cod

steve
+1  A: 

Additional note: there is big difference between Debug assembler output and Release one. The first one is good to learn how compiler produces assembler code from C++. The second one is good to learn how compiler optimizes various C++ constructs. In this case some C++-to-asm transformations are not obvious.

Vova
Thats good to know.
Keand64