如何学习 C++ 之间的联系代码和生成的汇编代码?

发布于 2024-12-06 10:54:08 字数 1432 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不甘平庸 2024-12-13 10:54:08

对此的简单答案是,编译代码并在调试器中查看它。

调试器将以非常严厉的方式向您展示两者之间的联系。下一步是了解编译器理论,然后查看编译器的源代码以了解它们尝试做什么。

我认为面试你的人可能一直想看看你是否能理解其中涉及到什么样的努力,而不是真正知道如何去做。

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.

迎风吟唱 2024-12-13 10:54:08

首先要做的就是学习汇编程序和机器代码。
有一些非常好的机器代码文档可以在
英特尔网站(尽管它可能比您需要的更详细)。那里
是两种广泛使用的常见汇编程序格式:
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 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文