开发者

How to learn the link between C++ code and the generated assembly code? [closed]

开发者 https://www.devze.com 2023-04-08 21:14 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve thi开发者_JS百科s question? Update the question so it can be answered with facts and citations
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve thi开发者_JS百科s question? Update the question so it can be answered with facts and citations by editing this post.

Closed 8 years ago.

Improve this question

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 simple answer to this is., To compile code and look at it in the debugger.

Debuggers will show you connection between the two in a very harsh way. The next step is to understand compiler theory and then look at the source code of compilers to understand what they try and do.

I think the person interviewing you may have been trying to see if you can understand what kind of effort is involved - rather than actually knowing how to do it.


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 /c as well, if you don't want to actually link the results); for g++, -S (which causes the compiler to stop once it has generated the assembler. In the case of VC++, the assembler will be in a file xxx.asm (where xxx.cpp was the name of the file being compiled), for g++, xxx.s. Try compiling 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号