My question is specifically for Windows C++ compilers and Visual Studio, but I got offered to interview for a job in finance where they wanted somebody very technical to write real-time multi-threaded code who could analyse at assembly level the code generated by a C++ compiler.
What are the methods one can apply to learn the link between C++ code and the generated assembly and achieve this level of proficiency ?
The first thing to do would be to learn the assembler and machine code.
There is some very good documentation of the machine code available at
the Intel site (although it may be more detailed than you need). There
are two common assembler formats in widespread use: the one used by
Microsoft is based on the original Intel assembler, where as g++ uses
something completely different (based on the original Unix assembler for
PDP-11), so you’ll have to choose one (although the assembler syntax
itself is rarely a real problem—knowing what the individual
instructions do is more important).
Once you have some idea of how to read assembler: most compilers
have options to output assembler: for VC++, use
/Fa(and/cas well,if you don’t want to actually link the results); for g++,
-S(whichcauses the compiler to stop once it has generated the assembler. In the
case of VC++, the assembler will be in a file
xxx.asm(wherexxx.cppwas the name of the file being compiled), for g++,
xxx.s. Trycompiling some code, with different levels of optimization, and then
look at the assembler in an editor.
Finally, if the question is asked, it’s probably because the interviewer
is concerned about performance issues; what he’s really interested in is
whether you know the relative cost of various operations (or the risks
involved when multithreading; e.g. what operations are atomic, etc.) In
which case, it probably wouldn’t hurt to point out that issues like
locality (which determines the percent of cache hits) are often more
important that the individual operations.