为什么C ++编译器在创建机器代码之前将源代码转换为Assembly? [英] Why do C++ compilers translate source code into Assembly before creating machine code?

查看:74
本文介绍了为什么C ++编译器在创建机器代码之前将源代码转换为Assembly?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习C ++,并且我了解到编译器通过编译将源代码从程序转换为机器代码.

I have started learning C++, and I have learned that a compiler turns source code from a program into machine code through compilation.

但是,我了解到C ++编译器实际上是在将Assembly代码转换为机器代码之前的一个临时步骤,将源代码转换为Assembly.此步骤的目的是什么?

However, I've learned that C++ compilers actually translate the source code into Assembly as an interim step before translating the Assembly code into machine code. What is the purpose of this step?

推荐答案

为什么不将其直接转换为机器代码?

Why don`t they translate it directly into the machine code?

首先:无需编写中间汇编语言表示形式.每个编译器供应商都可以直接直接发出机器代码.

First of all: There is no need to write an intermediate assembly language representation. Every compiler vendor is free to emit machine code directly.

但是有很多充分的理由写"字样.中间程序集并将其传递给汇编器以生成最终的可执行文件.重要的是,不需要真正地将文件写入某种介质,但是输出可以直接通过管道传递给汇编器本身.

But there are a lot of good reasons to "write" an intermediate assembly and pass it to an assembler to generate the final executable file. Important is, that there is no need to really write a file to some kind of media, but the output can directly piped to the assembler itself.

供应商使用中间汇编语言的一些原因:

Some of the reasons why vendors are using intermediate assembly language:

  • 汇编器已经可用,并且知道"了.如何生成一些可执行文件格式(例如elf).

  • The assembler is already available and "knows" how to generate some executable file formats ( elf for example ).

某些任务可以推迟到达到组装级别.例如,解决跳跃目标.这是可能的,因为中间组件通常不仅是1:1的表示,而且还包括某种宏组件".除了简单地从经济学中创建位,它还可以做更多的事情.

Some tasks can be postponed until assembly level is reached. Resolving jump targets for example. This is possible because the intermediate assembly is often not only 1:1 representation but some kind of "macro-assembler" which can do a lot more than simply creating bits from mnomics.

在汇编程序级别之后执行链接器.如果编译器直接想要创建可执行文件格式,则也必须这样做.如果必须再次编码,则有很多重复的作业.作为一个例子,所有未知地址"之前的重定位都可以被重新分配.必须在通往可执行文件的途中完成.只需使用汇编器/链接器即可完成工作.

the assembler level is followed by executing the linker. This must also be done if a compiler directly wants to create executable file formats. A lot of duplicated jobs if this must be coded again. As an example all the relocation of before "unknown addresses" must be done on the way to an executable file. Simply use the assembler/linker and the job is done.

中间程序集对于调试目的总是有用的.因此,即使无法从用户请求调试输出也可以省略该中间步骤,但仍然存在执行此中间步骤的艰巨要求.

The intermediate assembly is always useful for debugging purpose. So there is a more or less hard requirement to be able to do this intermediate step, even if it can be omitted if no debug output is requested from the user.

我相信还有更多...

I believe there are are lot more...

不好的一面是:

  • 写作"直接将信息传递给链接器时,文本表示形式和从文本中解析程序所花费的时间更长.

这篇关于为什么C ++编译器在创建机器代码之前将源代码转换为Assembly?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆