简体   繁体   English

C ++编译器使用哪种类型的程序集?

[英]What type of assembly do C++ compilers use?

So as I'm understood c++ code is comprised of assembly code, and when I compile a program it is read as its assembly equivelent and then run by the compiler. 因此,据我所知,c ++代码由汇编代码组成,当我编译程序时,它被视为等效于汇编,然后由编译器运行。 I'm also understood that assembly syntax and features change from model to model of proccessor. 我也了解到汇编语法和功能因处理器的型号而异。 If this is so, how do compilers manage to compile programs without being littered with bugs? 如果是这样的话,编译器如何在不被bug困扰的情况下设法编译程序? I mean, it can't be possible for a compiler to hold every assembly language variant created, is it? 我的意思是,编译器不可能保存所创建的每种汇编语言变体,对吗?

I think you're confusing assembly code with machine code. 我认为您正在将汇编代码与机器代码混淆。 It's not the same. 这是不一样的。 Machine code is what the CPU executes - a byte stream of instructions and data. CPU执行的是机器码-指令和数据的字节流。 Assembly is a human readable representation of machine code. 汇编是人类可读的机器代码表示。

It's indeed true that all C++ code is compiled into machine code, eventually. 的确,所有C ++代码最终都会被编译成机器代码。 Yes, the instruction set changes between CPUs and CPU versions. 是的,指令集在CPU和CPU版本之间变化。 Compilers have the notion of "target architecture" - when you compile, you have an option of specifying one. 编译器具有“目标体系结构”的概念-进行编译时,可以选择指定一个。 If you don't, the architecture of the current machine is usually assumed. 如果不这样做,通常会采用当前计算机的体系结构。 Yes, compiler vendors have to extend an effort to support every flavor of CPU that they intend to support. 是的,编译器供应商必须加倍努力以支持他们打算支持的每种CPU。 Fortunately, there's not that many. 幸运的是,没有那么多。 Besides, in the C compilation process, code generation is not even the most complex step, so the majority of compiler's own code is not CPU specific. 此外,在C编译过程中,代码生成甚至不是最复杂的步骤,因此大多数编译器自己的代码都不特定于CPU。

Some compilers work via assembly - rather than generating machine code, they generate assembly and feed that to an assembler for the final stage of compilation. 一些编译器通过汇编工作-而不是生成机器代码,而是生成汇编并将其馈送到汇编器以进行最终编译。 With that kind of design, your compiler normally assumes a certain flavor of assembler to be present on the system - typically GNU assembler ( as ). 通过这种设计,您的编译器通常假定系统上会存在某种形式的汇编程序-通常是GNU汇编程序( as )。

I think you've misunderstood the meaning of "assembly code". 我认为您误解了“汇编代码”的含义。

C++ code does not "consist" of assembly code; C ++代码不“包含”汇编代码。 it consists of, well, C++ code. 它由C ++代码组成。

A compiler translates this C++ code, ultimately into executable machine code that can be run on a computer (usually under the direction of an operating system). 编译器这种C ++代码,最终转换成可以在计算机上(通常是在操作系统的方向)上运行的可执行机器代码。

Assembly code is a human-readable symbolic representation of machine code. 汇编代码是机器代码的人类可读符号表示。 Typically a line of assmembly code corresponds to a single CPU instruction of machine code. 通常,一行汇编代码对应于一条机器代码的CPU指令。 Assembly is a much lower level language than C++ (or even C). 汇编是比C ++(甚至C) 低得多的语言。

Some C++ compilers generate assembly code as an intermediate step; 一些C ++编译器生成汇编代码作为中间步骤。 the assembly code is then translated into executable machine code. 然后将汇编代码转换为可执行的机器代码。 Other C++ compilers skip that step and generate machine code directly (though they may have an option to produce a human-readable assembly listing). 其他C ++编译器跳过该步骤并直接生成机器代码(尽管他们可以选择生成易于理解的汇编列表)。

Typically each compiler accepts input in a single high-level language (C, C++, etc.) and generates output for one CPU (x86, ARM, MIPS, etc). 通常,每个编译器都接受一种高级语言(C,C ++等)的输入,并为一个CPU(x86,ARM,MIPS等)生成输出。 Compilers are commonly designed in phases, so that the portion that processes the high-level input language can be combined with the portion that generates machine-specific code. 编译器通常是分阶段进行设计的,因此可以将处理高级输入语言的部分与生成机器特定代码的部分进行组合。 gcc is designed this way. gcc是按这种方式设计的。 There are front ends that process a number of different input languages, and code generators that generate code for different CPUs. 有一些前端处理多种不同的输入语言,还有一些代码生成器,它们为不同的CPU生成代码。 Thus if you already have an Ada front end and a MIPS back end, it's not too difficult to join them together to create an Ada compiler that generates MIPS machine code. 因此,如果您已经拥有Ada前端和MIPS后端,则将它们结合在一起以创建一个生成MIPS机器代码的Ada编译器并不是困难。

As for how compilers manage to do with without being "littered with bugs", well, it's just a lot of work. 至于编译器如何处理而不会“被臭虫打扰”,这是很多工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM