Useful Assembly

Seems like I’ve had some database issues the last couple of days but luckily it wasn’t anything bad and I could quickly fix it. Site is back up and I thought I’d celebrate by posting a semi useful post.

I’m not much for reading assembly but sometimes it’s fun to do it, mostly because I like to learn new things. Studying the assembly that’s output by your compiler is usually a good idea in order to get a better grasp of what’s happening with your perfectly written code after you press the compile button.

While I usually tend to debug in Visual Studio and switch to Disassembly mode whenever I feel the urge to go wild, you can do it outside of the main editor if you so choose. In fact, sometimes it’s more handy to study small pieces of code outside of the debugger.

I just read a post by Jim Tilander that inspired me to write a small batch file to aid me in this process and I thought of sharing that here. Perhaps someone will be able to get any use out of it.

  1.  
  2. @echo off
  3.  
  4. set editor="path\to\text\editor.exe"
  5. set vcbat="path\to\vcvarsall.bat"
  6.  
  7. rem setup
  8. if "%1" == "" goto init
  9. if "%1" == "init" goto init
  10.  
  11. rem edit
  12. if "%1" == "edit" goto edit
  13.  
  14. rem compile and edit
  15. if "%1" == "check" goto check
  16.  
  17. rem compile to asm
  18. if "%1" == "compile" goto compile
  19.  
  20. :edit
  21. %comspec% /k %editor% %2
  22. goto end
  23.  
  24. :compile
  25. %comspec% /k cl /nologo /c /Fa /Ox /Oy- /Za "%2"
  26. goto end
  27.  
  28. :check
  29. cl /nologo /c /Fa /Ox /Oy- /Za "%2"
  30.  
  31. rem extract file name
  32. for %%x in (%2) do set filename=%%~nx
  33.  
  34. %comspec% /k %editor% %filename%.asm
  35. goto end
  36.  
  37. :init
  38. %comspec% /k "%vcbat%" x86
  39. goto end
  40.  
  41. :end
  42.  

You need to change the two variables at the top. The first one should point to a text editor of choice and the second one should point to the Visual Studio vcvarsall.bat file. It should be found some place looking like this “C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat” for VS2008 and most likely something very similar for other VS versions. Oh, I should probably mention that this will only work with Visual Studio. However, there are instructions for doing the same thing with GCC at Tilander’s blog linked to above.

Paste the above code in a bat file and call it something nice since the file name will be the command you have to use, e.g. “code.bat” and put it somewhere on your system path. To use it, launch a new command line window and type first

  1. code

to setup up all the paths. Afterwards, you can use the following commands in that window:

  1. code edit myfile.asm

to open myfile.asm in your editor for viewing,

  1. code compile myfile.cpp

to compile myfile.cpp file into assembly or

  1. code check myfile.cpp

to compile myfile.cpp and open the resulting asm file in your specified editor all in one go.

The resulting .asm file will be placed in the directory where you currently are when you type in the commands. You should also have a look at the compile commands and tweak those to your liking.

Leave a Reply