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.
发布评论
评论(2)
对此的简单答案是,编译代码并在调试器中查看它。
调试器将以非常严厉的方式向您展示两者之间的联系。下一步是了解编译器理论,然后查看编译器的源代码以了解它们尝试做什么。
我认为面试你的人可能一直想看看你是否能理解其中涉及到什么样的努力,而不是真正知道如何去做。
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.
首先要做的就是学习汇编程序和机器代码。
有一些非常好的机器代码文档可以在
英特尔网站(尽管它可能比您需要的更详细)。那里
是两种广泛使用的常见汇编程序格式:
Microsoft 基于原始的 Intel 汇编器,而 g++ 使用
完全不同的东西(基于原始的 Unix 汇编器
PDP-11),所以你必须选择一个(尽管汇编语法
它本身很少是一个真正的问题——了解个人的想法
指示做更重要)。
一旦您了解如何阅读汇编程序:大多数编译器
有输出汇编程序的选项:对于 VC++,使用
/Fa
(也可以使用/c
,如果您不想实际链接结果);对于 g++,
-S
(其中导致编译器在生成汇编程序后停止。在
对于 VC++,汇编器将位于文件
xxx.asm
中(其中xxx.cpp
是正在编译的文件的名称),对于 g++,
xxx.s
。尝试编译一些代码,进行不同级别的优化,然后
在编辑器中查看汇编器。
最后,如果问这个问题,很可能是因为面试官
关心性能问题;他真正感兴趣的是
您是否了解各种操作的相对成本(或风险)
多线程时涉及;例如哪些操作是原子的,等等)
在这种情况下,指出诸如此类的问题可能不会有什么坏处
局部性(决定缓存命中的百分比)通常更多
重要的是个体操作。
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
(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.cpp
was 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.