用C ++编写的程序如何在多个CPU架构上运行? [英] How can a program written in C++ be run on multiple CPU architectures?

查看:100
本文介绍了用C ++编写的程序如何在多个CPU架构上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要编译为汇编的C ++程序,然后汇编器会将其编译为机器代码.

I have a C++ program that I want to compile to assembly, and then assembler will compile it to 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.

汇编器将使用哪个表?我的C ++程序是否有可能无法在所有CPU上运行,因为CPU使用不同的表,这意味着相同的机器代码将在不同的CPU上执行不同的工作吗?

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?

推荐答案

汇编程序将针对已被告知/已针对其进行编程的任何体系结构进行汇编.由于每种指令集体系结构(ISA)的汇编语言都不同,因此您只能汇编为同一体系结构的一种体系结构编写的汇编程序.对于错误的体系结构,通常不可能偶然或有意地组装该程序.

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. It is generally not possible to accidentally or intentionally assemble the program for the wrong architecture.

使用编译器时,编译器将使用正确的标志调用正确的汇编器,以汇编为您要编译的体系结构生成的汇编代码.生成的程序将仅在您为其编译的ISA处理器上运行.如果要让程序在其他ISA的处理器上运行,则必须为其他ISA进行编译.

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. If you want the program to run on processors of a different ISA, you have to compile it for that other 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.

这篇关于用C ++编写的程序如何在多个CPU架构上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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