C 编译器可以编译为通用汇编吗?

发布于 2024-12-06 10:59:28 字数 259 浏览 2 评论 0原文

我正在上计算机组织课程,我们正在用汇编语言编写程序。然而,由于这是一门课程,我没有对任何事物或任何现实世界的用途有更广泛的了解。我们使用 Altera 的 Nios II 汇编语言。教授没有告诉我们任何关于当前生产中使用哪些汇编语言、语义是什么,或者 C 代码如何编译为所有汇编语言的信息。

在简短的介绍之后,我是否正确假设 C 代码可以编译为多种汇编语言?如果是这样,它会做什么来达到所有这些汇编语言 - 将其解析为通用汇编语言,然后从那里翻译它?或者每种不同的汇编语言都有单独的过程?

I'm in a Computer Organization class, and we're programming stuff in assembly. However, since this is a class, I'm not getting a broader sense of anything, or any real-world use. We're using Altera's Nios II Assembly language. The professor hasn't told us anything about which assembly languages are used in current production, and what the semantics are, or how C code compiles to ALL of the assembly languages.

Following that brief intro, am I correct in assuming that there are several assembly languages that C code compiles to? If so, what does it do to reach all of those assembly languages - parse it into a generic assembly language and then translate it from there? Or is there a separate process for each different assembly language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-12-13 10:59:28

不需要将 C 编译为任何特定程序集或根本任何程序集,这些留给编译器的实现者,而不是语言规范的一部分。通常,每个 CPU 制造商都会开发一个 C 编译器来针对其特定架构。

不过,还有更通用的编译器,例如 GCC 和 Clang,它们可以针对许多不同的指令集。

以 Clang 为例,它基于低级虚拟机,它是一个具有“中级虚拟机”的抽象机。表示”语言,LLVM IR。为 LLVM 可以针对的每种架构编写一个后端,用于将 LLVM IR 转换为指令集,然后任何编译为 LLVM IR 的编译器都可以针对 LLVM 支持的 CPU。

编译器将根据您传递给它的参数来决定在运行时以哪个后端为目标。编译器通常有一个默认后端,它是在构建编译器本身时通过配置设置的(可能默认为您构建编译器的体系结构)。

GCC 可能使用类似的方法和一些中间表示,但我不确定细节。还有一个 GCC 后端也可以针对 LLVM。

There is no requirement to compile C to any specific assemblies or any assembly at all, those are left to the implementor of a compiler, and not part of the language specification. Typically, every CPU manufacturer will develop a C compiler to target their specific architecture.

There are more generic compilers like GCC and Clang though, which can target many different instruction sets.

To use Clang as an example, it is based on the Low Level Virtual Machine, which is an abstract machine with an "intermediate representation" language, LLVM IR. A back-end is written for each architecture that LLVM can target for converting LLVM IR to the instruction set, and then any compiler which compiles to LLVM IR can then target the CPUs supported by LLVM.

The compiler will decide which back-end to target at runtime based on the arguments you pass to it. The compiler typically has a default back-end which is set when building the compiler itself, through the configuration (which will probably default to the architecture you're building the compiler on).

GCC probably uses a similar approach with some intermediate representation, but I'm not sure of the details. There's also a GCC back-end which can target LLVM too.

独﹏钓一江月 2024-12-13 10:59:28

C 可以转换为编译器支持的任何类型的汇编程序。是否有中间表示也取决于编译器。

请注意,如果您在 C 代码中编写直接汇编程序(例如,您正在用 C 语言编写硬件驱动程序,但需要执行一些非常低级的操作),那么您已将代码锁定到该特定平台的汇编语言。 C(和编译器)将不会采用嵌入式 x86 汇编器的某些代码并将其转换为(例如)MIPS 或 PPC。

C can be converted into any kind of assembler the compiler(s) support. Whether there's an intermediate representation is up to the compiler as well.

Note that if you're writing direct assembler inside your C code (e.g. you're writing a hardware driver in C but need to do some very low-level stuff), then you've locked your code to that particular platform's assembler language. C (and the compiler) will NOT take some code with embedded x86 assembler and translate that into (say) MIPS or PPC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文