简体   繁体   English

用 C++ 编写的程序如何在多个 CPU 架构上运行?

[英]How can a program written in C++ be run on multiple CPU architectures?

I have a C++ program that I want to compile to assembly, and then assembler will compile it to machine code.我有一个 C++ 程序,我想编译成汇编程序,然后汇编程序将它编译成机器码。

Now, as far as I know, in order to transform assembly code to machine code the assembler needs some kind of table to map assembly instructions to the actual machine instructions.现在,据我所知,为了将汇编代码转换为机器代码,汇编程序需要某种表格,将 map 汇编指令转换为实际机器指令。

Which table will the assembler use?汇编器将使用哪个表? Is there a chance that my C++ program won't run on all CPUs, because CPUs use different tables which means that the same machine code will do different things on different CPUs?我的 C++ 程序是否有可能无法在所有 CPU 上运行,因为 CPU 使用不同的表,这意味着相同的机器代码将在不同的 CPU 上执行不同的操作?

The assembler assembles for whatever architecture it has been told to/programed to assemble for.汇编器为它被告知/编程为汇编的任何架构进行汇编。 As the assembly language for each instruction set architecture (ISA) differs, you can only assemble an assembly program written for one architecture for that same architecture.由于每种指令集体系结构 (ISA) 的汇编语言不同,因此您只能为同一体系结构汇编为一种体系结构编写的汇编程序。 It is generally not possible to accidentally or intentionally assemble the program for the wrong architecture.通常不可能意外或故意为错误的体系结构组装程序。

When you use a compiler, the compiler invokes the correct assembler with the correct flags to assemble the assembly code it generated for the architecture you told it to compile for.当您使用编译器时,编译器会调用具有正确标志的正确汇编器来汇编它为您告诉它编译的体系结构生成的汇编代码。 The resulting program will only run on processors of the ISA your have compiled it for.生成的程序只能在您为其编译的 ISA 处理器上运行。 If you want the program to run on processors of a different ISA, you have to compile it for that other ISA.如果您希望程序在不同 ISA 的处理器上运行,您必须为其他 ISA 编译它。

If your program is poorly written, it is possible that it won't compile or work when compiled for other architectures than the one(s) you developed it for.如果您的程序写得不好,那么当为其他架构编译时,它可能无法编译或工作,而不是您为其开发的架构。 Such a program is called an unportable program.这样的程序称为不可移植程序。 However, unless you do weird things or make assumptions about properties of the architecture you are programming for, this is unlikely to happen.然而,除非你做了奇怪的事情或对你正在编程的架构的属性做出假设,否则这不太可能发生。

In general what is call assembly is roughly a human readable (text) form of machine code (binary).一般来说,所谓的汇编大致是机器代码(二进制)的人类可读(文本)形式。

As franji1 said in a comment, in general compilers emit an intermediate abstract machine code from the source.正如 franji1 在评论中所说,通常编译器会从源代码发出中间抽象机器代码。 And this kind of code can easily (it is intended to) be translated to assembly/machine code.并且这种代码可以很容易(它打算)被翻译成汇编/机器代码。

I have a C++ program that I want to compile to assembly, and then assembler will compile it to machine code.我有一个 C++ 程序,我想编译成汇编程序,然后汇编程序将它编译成机器码。

This is what a compiler is designed to.这就是编译器的设计目的。 Compiler is somehow misleading.编译器在某种程度上具有误导性。 Compiler can be the "compiler phase" or "compiler toolchain".编译器可以是“编译器阶段”或“编译器工具链”。 compiler phase is the one that translate your source code to the intermediate abstract form, that then needs to be translated to target assembly/machine code by the assembler .编译器阶段是将您的源代码转换为中间抽象形式的阶段,然后需要由汇编器将其转换为目标程序集/机器代码。 Compilation is commonly what denotes the whole process from source code to executable machine code.编译通常表示从源代码到可执行机器代码的整个过程。

Now, as far as I know, in order to transform assembly code to machine code the assembler needs some kind of table to map assembly instructions to the actual machine instructions.现在,据我所知,为了将汇编代码转换为机器代码,汇编程序需要某种表格,将 map 汇编指令转换为实际机器指令。

Roughly yes.大致是的。 This is what a document like Instruction Set Reference Manual is for: describing how textual form must be translated to byte form.这就是像指令集参考手册这样的文档的用途:描述必须如何将文本形式转换为字节形式。

Which table will the assembler use?汇编器将使用哪个表?

See document...见文件...

Is there a chance that my C++ program won't run on all CPUs, because CPUs use different tables which means that the same machine code will do different things on different CPUs?我的 C++ 程序是否有可能无法在所有 CPU 上运行,因为 CPU 使用不同的表,这意味着相同的机器代码将在不同的 CPU 上执行不同的操作?

You have to generate a byte form of your program for each platform (machine/os).您必须为每个平台(机器/操作系统)生成程序的字节形式。 A compiler is designed to generate a machine code for a given platform that realizes exactly what your source code specifies.编译器旨在为给定平台生成机器代码,该平台准确地实现您的源代码指定的内容。 This is why compilers exist, to free you from writing program in assembly (that is very hard to do).这就是编译器存在的原因,让您免于在汇编中编写程序(这很难做到)。

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

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