C/C++ low level curriculum: Looking at optimized assembly

May 9, 2012
C/C++ low level curriculum: Looking at optimized assembly

[In this reprinted #altdevblogaday opinion piece, Gamer Camp's Alex Darby explains how to set up Visual Studio to look at optimized assembly code generated for simple code snippets.] It's that time again where I have managed to find a few spare hours to squoze out an article for the Low Level Curriculum. This is the eighth post in this series, which is not in any way significant except that I like the number 8. As well as being a power of two, it is also the maximum number of unarmed people who can simultaneously get close enough to attack you (according to a martial arts book I once read). This post covers how to set up Visual Studio to allow you to easily look at the optimized assembly code generated for simple code snippets like the ones we deal with in this series. If you wonder why I feel this is worth a post of its own, here's the reason – optimizing compilers are good, and given code with constants as input and no external output (like the snippets I give as examples in this series) the compiler will generally optimize the code away to nothing – which I find makes it pretty hard to look at. This should prove immensely useful, both to refer back to, and for your own experimentation. Here are the backlinks for preceding articles in the series in case you want to refer back to any of them (warning: the first few are quite long): Here are the backlinks for preceding articles in the series (warning: it might take you a while, the first few are quite long):

  1. A Low Level Curriculum for C and C++

  2. C / C++ Low Level Curriculum part 2: Data Types

  3. C / C++ Low Level Curriculum Part 3: The Stack

  4. C / C++ Low Level Curriculum: More Stack

  5. C / C++ Low Level Curriculum Part 5: Even More Stack

  6. C / C++ Low Level Curriculum Part 6: Conditionals

  7. C / C++ Low Level Curriculum Part 6: More Conditionals

Assumptions Strictly speaking, dear reader, I am making tons of assumptions about you as I write this – that you read English, that you like to program etc. but we'll be here all day if I try to list those so let's stick to the ones that might be immediately inconvenient if they were incorrect. I will be assuming that you have access to some sub-species of Visual Studio 2010 on a Windows PC, and that you are familiar with using it to do all the everyday basics like change build configurations, open files, edit, compile, run, and debug C/C++. Creating a project Open Visual Studio and from the menu choose "File -> New -> Project…". Once the new project wizard window opens (see below):

  • go to the tree view on the left of the window and select "Other Languages -> Visual C++"

  • in the main pane select "Win32 Console Application Visual C++"

  • give it a name in the Name edit box

  • browse for a location of your choosing on your PC

  • click OK to create the project

Once you have clicked OK just click "Finish" on the next stage of the wizard – in case you're wondering, the options available when you click next don't matter for our purposes (and un-checking the "Precompiled header" check box makes no difference, it still generates a console app that uses a precompiled header…). Changing the Project Properties The next step is to use the menu to select "Project -> <YourProjectName> Properties", which will bring up the properties dialog for the project. When the properties dialog appears (see image below):

  • select "All Configurations" from the Configuration drop list

  • select "Configuration Properties ->General" in the tree view at the left of the window

  • in the main pane change "Whole Program Optimization" to "No Whole Program Optimization".

Next, in the tree view (see image below):

  • in the tree view, navigate to "C/C++ -> Code Generation"

  • in the main pane, change "Basic Runtime Checks" to "Default" (i.e. off)

Finally (see image below):

Tags:

No tags.

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Explore Features>>