将C ++首先转换为汇编语言吗 [英] Will C++ first gets converted to assembly

查看:102
本文介绍了将C ++首先转换为汇编语言吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.我是C ++开发人员,多次听到我的源代码将首先转换为程序集,然后将程序集转换为机器代码. 但是在汇编语言的视频教程之一中,讲师明确指出,C/C ++代码直接转换为机器代码. (当然会有链接和加载在那里.)

I have confusion. I am C++ developer and heard many times that my source code will first gets converted to assembly and then assembly will get converted to machine code. But in one of the video tutorial of assembly language, instructor clearly said, C/C++ code directly gets convert to machine code. (Of course there will be linking and loading there).

我看过这样的链接, C ++代码是否可以编译为汇编代码?

I have seen links like this, Does the C++ code compile to assembly codes?

我仍然无法澄清我的疑问.

Still I am not able to clarify my doubt.

如果万一C ++没有首先转换为汇编,则反汇编器如何从二进制文件生成汇编代码.

If in case, C++ does not gets converted to assembly first, how de-assembler generate assembly code from binary.

推荐答案

在过去(很久以前),编译器的工作方式如下:

In the old (very old) days, compilers worked like this:

  1. 编译器在文件中生成汇编代码,并将其写入磁盘,
  2. 汇编器获取了该文件并生成了二进制文件.

这几天,除非您真的想要汇编输出,否则编译器不会生成显式的汇编语言代码.它只会在内存中生成一些程序集,然后将其本身转换为机器代码,并且仅将机器代码写入文件. 这是您的讲师在说C/C ++直接转换为机器代码时的意思.

These days, unless you really want assembly output, your compiler does not generate explicit assembly language code. It will just generate some assembly in memory but then convert it to machine code themselves and only write the machine code to file. This is what your instructor meant when saying that C/C++ gets directly converted to machine code.

您还应该知道一件事.机器代码与汇编语言基本相同.在汇编语言中,指令具有名称并使用字符串编写,但是这些是机器代码中使用的相同指令(一对一映射).这很重要,所以我重复一遍:机器代码和汇编都是相同的,只是用不同的符号表示.

There is one more important thing you should know. Machine code is basically the same thing as assembly language. In assembly languages, instructions have names and written using strings, but these are the same instructions (one-to-one mapping) that are used in machine code. This is important so I repeat myself: machine code and and assembly are the same, only written in different notations.

这就是为什么可以分解任何二进制文件的原因;因为要将某些内容从机器代码转换为汇编,您只需更改表示形式(将每条指令及其操作数从二进制转换为助记符"形式.

This is why any binary can be disassembled; because to convert something from machine code to assembly, you just have to change the representation (translate each instruction and its operands from binary to "mnemonic" form.

因此,出于性能原因,现代编译器实际上可能不会生成表示指令的实际字符串(例如mov rax, 42).如果没有人想要装配输出,为什么要浪费内存和处理它的处理能力呢?但是,它们当然会生成等效的机器代码,这对于编译器来说是更快的.

So, modern compilers may actually not generate the actual strings that represents instructions (e.g. mov rax, 42) for performance reasons. If nobody wants the assembly output, why waste the memory and processing power generating it? But of course they do generate the equivalent machine code, which is faster for a compiler to generate.

这篇关于将C ++首先转换为汇编语言吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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