Visual Studio C++: Seeing the ASM code? - c++

I'd like to see all the asm produced by Visual Studio C++ to learn a bit about ASM, compilers, etc. I know with GCC, you can do it with the -S argument, but not in VS. How do I go about doing this?

The easiest way to do this is to start your program in the integrated debugger, then turn on the assembly language view. This shows the assembly language interleaved with your original source code.
Back when I used VS, this was Alt+F7 or something but it's probably changed. The advantage of doing this (over using -S equivalent) is you can focus on exactly which line(s) of code you're interested in.

Right-click on your project and then Properties -> C/C++ -> Output files -> Assembler Output and set it to something else than No Listing. Equivalently, you can add one of the /FA switches to the compiler's command line.

Related

Eclipse disassembly view, can you view disassembly without debugging the program?

I was using the Eclipse-CDT "Disassembly" View while debugging a program (in the "Debugging" Perspective. I found useful the way that Eclipse-CDT has the source code display and assembly code display synchronized.
I would like to peruse the compiler generated assembly code while not running the debugger. I am interested in changing things like:
compiler flags (for example: -O0, -O1, -O2, -O3)
changing C code
And then observing the changes in the generated assembly code.
In Eclipse-CDT, can you view the disassembly without actively debugging the program, yes/no?
If yes, how?
Maybe Eclipse-CDT isn't the right tool for this. I seem to recall there is a way to prod the compiler into generating a verbose/readable assembly listing with line numbers and annotations back to the original source.
While in Eclipse-CDT, I just noticed that you can double-click on an object file and the assembly disably is produced. I will have to check this out.
I know you can use the compiler to generate assembly listings too.
I'm sure there are other tools too.

How to read a .obj file?

In visual studio a object file (.obj) is generating after compiling a c++ file.
How to read and understand it?
Also how to see the code after compiler optimization in Visual studio 2015.
Please redirect if this is already answered.
Use the DUMPBIN tool from Visual Studio command prompt. Specifically, the /DISASM option shows you the disassembly. Do note the if you have link-time optimization enabled, then at least theoretically the final code may change after the .obj files are linked to form the final binary (.exe or .dll). You can disassemble those with DUMPBIN as well.
Dumpbin at the /all setting shows code sections & directories. Detailed explanations of things like code section characteristics is not common knowledge, but may be found in certain books. Here's a precis on COFF, here's a more detailed description, or there's even a WYSINWYX thesis.
Adding to #cynic's answer, in Visual Studio (15 & 17) it's possible to run DumpBin via the menu.
Go to Tools/External Tools and click Add.
Put in the Title box something like DumpBin Your_Exe or Your_Obj name and find the path for DumpBin if it isn't in the environment settings.
It can be similar to the following:
c:\program files (x86)\microsoft visual studio\2017\community\VC\Tools\MSVC\SDK-Version\bin\Host $(Platform)\$(Platform)\dumpbin.exe
and add it to the Command box. For arguments try something like:
/ALL /OUT:C:\Users\New\Desktop\dumpofYour_ExeName.txt "Pathname to your obj/executable file"
and yes, the quotes will work.
If using the desktop, put that in the initial directory box as well.
Select "prompt for arguments" if you wish to leave the Argument box blank.
You're sort of asking the wrong question: you say you want to see compiler optimizations but then you draw conclusions leading you to think you .obj files are required for that. That works, as cynic's answer shows, but there are alternatives which can be handier/better depending on the situation:
run code under the debugger, break, right-click any source file, select 'Go To Disassembly' and you can view source and assembly inline
have the compiler output assembly code (again optionally including source and machine code): go to project settings->Compiler->Output Files and set 'Assembler Output'

Fastest way to write & compile a C/C++ program in Windows

I'm usually using Visual Studio, but several things bother me when I just quickly want to test some code:
it has a rather long startup time
it always needs a project to execute/debug files
program output gets printed to the console, but the window simply closes when I don't insert a getchar() or a breakpoint in the program and thus I'm not seeing it.
I'm looking for a program which is suitable for a really, really quick programming in Windows. Such as, copying some code from an SO question, running it and seeing its output.
I don't think that console programs or g++ under CygWin are a good solution, because there it takes ages to cd into the right dir to save the file, I'm not used to editors such as Vim, and typing in the compiler commandline myself has always annoyed me etc.
So I guess what I'm looking for is a very lightweight free C/C++ IDE which is preconfigured to work with a free compiler (bonus points if it is even shipped with it.)
What can you recommend which adresses at least two items from the list above?
Is there maybe even a program which can execute/interpret C or C++ in an interactive commandline (like Python)?
I'm looking for a program which is suitable for a really, really quick
programming in Windows. Such as, copying some code from an SO question
and executing it and seeing it's output.
For quick-and-dirty experimental coding, I really like codepad.org. Not having to create a file is especially nice as it saves me from coming up with a suitable name and disk location. Be aware that it uses g++ 4.1.2 behind the scenes so some of the latest C++11 features aren't supported.
"really, really quick (and dirty, throw away?) programming "?
Compiler : VC++ command line - you already have it.
Editor: Notepad or somesuch
Compilation process: A .BAT file you write once
and supply a parameter with the name of the single source file.
Location: Set up some desktop shortcuts to a known directory for your
test code.
Use TCC : Tiny C Compiler
start a command prompt
cd wherever
notepad main.c
write code in notepad. save
back in the command prompt type tcc -run main.c
notice errors, go back to 4
Note that with -run parameter you're invoking tcc like an interpreter
Which compiler you use doesn’t really matter. I prefer G++ but cl.exe (from Visual Studio) works equally well.
In order to use the compiler quickly from the command line, either
include it into your PATH variable by setting it in the system settings, or
create a simple .cmd script which launches a console with the right paths included.
Visual Studio incidentally comes bundled with such a .cmd script which is linked in the Start Menu entry of Visual Studio. Personally, though, I prefer adjusting the PATH variable.
Then you can simply invoke the compiler from any directory in the command line. If you are too lazy to write the whole command line, create a script to do it for you. Or use Cygwin and (C)Make.
Two additional remarks:
Starting the project using the build configuration (Cntr+F5 (?)) leaves the console open after the program has run, without you having to include getch() calls or similar.
I highly recommend you learn an editor such as Emacs or Vim, unless you plan never to use any other platform than Windows, and even then. These editors are just tremendously powerful, and in some ways light-years beyond what the Visual Studio code editor offers.
But if you really don’t have the time, use a decent text editor such as Notepad++ instead.
Open Watcom is easy to install and use, it's fast and it's the closest compiler to MSVC++, although it's noticeably behind in features (especially in C++).
I don't use its IDE at all as I got used to doing most of the stuff in the console, but it's there and the debugger is there too.
Compiling one-filers is easy.
Compiling C code:
wcl386.exe /we /wx /q sourcefile.c
Compiling C++ code:
wcl386.exe /xs /we /wx /q sourcefile.cpp
On my machine, I have a "empty" project called "Test". When I want to test some random code on the internet, I simply put it into main.cpp in that project, and compile.
If you think MSVC takes too long to load, it should be possible to write a batch script that attempts to compile the project and puts the build log in a file. Then you can simply alter the existing main.cpp with notepad, double click the batch file, then pop open the build log or run the executable.
[Edit] I made a batch file to compile the entire solution. Turns out that requires loading visual studio. However, the batch file can compile/run a single cpp file easy enough.
My favorite IDE: http://www.codeblocks.org/
Here is a direct link to the download that includes the MinGW compiler: http://download2.berlios.de/codeblocks/codeblocks-10.05mingw-setup.exe
You're not gonna find any (good) C/C++ interpreters.
Once I used PSPad setting its "compiler" option for C++ files to a reasonable default (cl.exe in the correct directory, speed optimization, all warnings). Then it's just Ctrl+F9.
All of the above compiler recommendations are good. For an editor, I really like Notepad2
I know you didn't ask...
First off, your expectations are not reasonable. no program can guess what you want, over a range of input from the simplest to the most complex. If cd'ing into cygwin is too hard, and starting up visual studio is too time-consuming, you're pretty much toast. Sorry.
That said, you can edit code with notepad (which you can invoke from the command line as notepad foo.cpp). Notepad uses your mouse and the arrow keys on your pc so it's pretty intuitive.
you can use visual studio tools from the command line, without having to fiddle with project files.
Visual studio comes with a tool called nmake, the most basic usage of which is similar to linux make. If you have very simple input, nmake's default rules may be good enough to produce an executable. If not, you may be able to construct a makefile that will take any single simple file, say foo.cpp, and compile and link it to an executable called foo.exe. You'll still have to learn to use nmake, which some people think is easy, and others think is fiendishly difficult. Try nmake foo.cpp and see if the result is what you want.

How to debug to assembly level with visual studio express?

I'm curious about how some features are implemented under the hood,like :
pEvent->WaitForCompletion(INFINITE, &evCode);
I tried to step into it but failed.
Is there a way to do that?
You don't say which actual version of Visual Studio, but I believe all versions contain the ability to set into disassembly of native code for which you do not have source.
However, default debugger options are often set to disable this kind of display. Check out the debugging options in Tools->Options and enable 'show disassembly if source is not available', disable 'just my code' and other similar options.
One caveat: It's quite possible that the core implementation of WaitForCompletion is in kernel mode code (especially if pEvent is a wrapper for a HANDLE created by CreateEvent). In this case you'll need a kernel debugger to step through the inner workings.
Martyn
If you would like to debug in pure assembler I have better tool for you - OllyDbg http://www.ollydbg.de/.
Load the executable file with it, then find all calls of WaitForCompletion (RMB on assembler view -> Search For -> Names -> Select "WaitForCompletion" -> RMB -> Find References). Then create breakpoints on all occurrences and run executable. Debugger should stop on one of breakpoint and you can step into your function.
Happy reverse-engineering.

Generating and saving code from Macro

I am analyzing a legacy code which heavily using macro, I am lost in understanding how macro are expanding in code.
Could any one suggest me some tool or technique so that I can study actual code generated from macro expansion.
Platform : Windows XP
Language : C++
Compiler : VC6
To run the GCC as a pre-processor only do:
gcc -E source-file.cc > processed-source-file.cc
It also do all the #includes that you may or may not want.
With visual studio you can use the Generate Preprocessed File option.
In the properties for your project select
C/C++/Preprocessor/
In that tab there is an option to:
generate a preprocessed file.
Select Yes, with numbers.
Run the pre-processor (cpp) on the source file.
Since you're using Visual C this doesn't help you, but it might be helpful to others to mention:
Netbeans 6.7 is able to display the macro expanded version of C/C++ code in a separate window during editing.