有多“低”? C 是否作为“低级”?语言?

发布于 2024-10-17 11:41:10 字数 91 浏览 10 评论 0原文

我们经常听说C是低级语言,那么它到底低到什么程度呢?我知道的最低级别是使用指针的内存管理。还有其他层次我还没有发现吗? “接近硬件”是什么意思? C 离硬件有多“近”?

We often hear that C is a low-level language, but how low does it go? The lowest level I am aware of is memory management using pointers. Are there further levels I have yet to discover? What does "close to the hardware" mean? How "close to the hardware" is C?

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

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

发布评论

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

评论(12

自此以后,行同陌路 2024-10-24 11:41:10

使用标准术语。高级语言和低级语言。

高级语言是为了方便编写该语言的人而设计的。

较低级语言是为了便于计算机运行该语言而设计的。

C 只是汇编语言的一个进步,汇编语言实际上是机器代码的人工翻译。它并不比机器代码低,但人们不能很好地阅读十六进制,因此汇编被认为是最低级别的编程语言。

大多数 C 操作可以翻译成少于 10 条机器指令。有些可以翻译成单个机器指令(取决于许多情况)。相比之下,许多高级语言可能需要数十到数千条机器指令来实现特定操作。

语言的“级别”大多是一个抽象的概念。它没有多大用处,除了将一种语言与另一种语言进行比较,试图了解您是否需要用一种或另一种语言编写更多的源代码,或者您是否需要使用一种语言来了解更多关于机器架构的信息。到另一个。

Use the standard terminology. Higher level languages and Lower level languages.

High level languages are designed for the ease of the person writing the language.

Lower level languages are designed for the ease of the computer running the language.

C is just a step up from assembly language, which is practically a human translation of machine code. It doesn't get any lower than machine code, but people don't read hexadecimal very well, so assembly is considered the lowest level programming language.

Most C operations can be translated into less than ten machine instructions. Some can be translated into a single machine instruction (depending on many circumstances). By comparison, many high-level languages might require dozens to thousands of machine instructions to implement a particular operation.

The "level" of a language is mostly an abstract concept. It is not useful for much except comparing one language to another in the context of trying to understand if you will have to write more source code in one or another, or if you will have to know more about the machine's architecture in one language as compared to another.

九公里浅绿 2024-10-24 11:41:10

C 有时被称为“可移植汇编语言”。本质上,您可以在汇编中执行的任何操作都可以在 C 中执行(通过语言内置的功能或通过内联汇编)。

C is sometimes referred to as "portable assembly language". Essentially anything that you can do in assembly, you can do in C (either through features built into the language or through inline assembly).

过去的过去 2024-10-24 11:41:10

C 只不过是 ASM 的程序包装。一个优秀的 C 程序员知道该语言背后的内容,应该能够查看代码片段并写出该代码将编译成的 ASM。这些 ASM 指令可以 1:1 转换为二进制机器指令。它几乎比汇编高出一步,因此您可以编写 C 代码来执行计算机能够执行的任何操作,但是,由于处于如此低的级别,就您必须具体说明的内容而言,它相对原始。例如,内存就被这样对待,而不是抽象成像“堆”这样的结构。在 OO 语言中创建一个“新”对象就这么简单;等效的 C 代码将涉及该对象所有成员的大小总和的 malloc,以及指向该块内每个对象成员的指针。

C is little more than a procedural wrapper around ASM. A good C programmer, who knows what's behind that language, should be able to look at a code snippet and write out the ASM that the code would compile into. Those ASM instructions can be translated 1:1 to binary machine instructions. It's pretty much one step above assembly, and as such you can write C code that does ANYTHING the computer is capable of, BUT, being at such a low level, it is relatively primitive in terms of what you have to be specific about. For instance, memory is treated as such, and not abstracted into constructs like the "heap". Creating a "new" object in an OO language is as simple as saying so; the equivalent C code would involve a malloc for the sum of the size of all the members of that object, and pointers to each of the object's members within the block.

笑忘罢 2024-10-24 11:41:10

创建硬件(CPU、主板等)的人也提供其机器语言(汇编语言)。这本身就成为最低级别。然后其他人编写了一个 C 编译器,它将 C 语法转换为该硬件的本机汇编语言(如果我错了,请纠正我)。使用C,您可以使用汇编,因此达到了最低级别!

A person who creates hardware (cpu, motherboard, etc) also provide its machine language (assembly language). That becomes the lowest level itself. Then some other person writes a compiler for C which converts C syntax into native assembly language for that hardware (please correct me if I am wrong). With C you can use assembly hence lowest level reached!!

苍景流年 2024-10-24 11:41:10

您可以在通用操作系统上管理的内存通常是虚拟内存,因此通常都是骗局- 你的C指针不包含物理内存地址,还有比这更深的一层。

要做到这一点,需要您对 MMU 进行编程,这在 C 语言中不容易完成,通常需要一些非常依赖于体系结构的程序集,并且您的代码需要以某种形式的特权模式运行(如操作系统内核确实如此)

还可能有各种其他协处理器或整个范围< a href="http://en.wikipedia.org/wiki/SSE3" rel="nofollow">指令集 在 C 中很难直接获取。

The memory you can manage on general purpose OS' are usually virtual memory, so it usually all a fraud - your C pointers doesn't contain the physical memory addresses, there's a whole layer deeper than that.

Getting below that requires you to program the MMU , which isn't easily done in C, it usually requires some very architecture dependant assembly and your code would need to run in some form of privileged mode(as the OS kernel does)

There might also be various other co-processors or whole range of instruction sets it can be hard to get at directly in C.

清晨说晚安 2024-10-24 11:41:10

“低”的定义有点困难,但是 C 通过扩展支持许多系统调用、goto,甚至汇编混合。

The definition of 'low' is a bit difficult, but C supports an number of system calls, gotos, and even assembly mixing through extensions.

携余温的黄昏 2024-10-24 11:41:10

为了回答这个问题,您需要定义“低级别”的含义。

C 语言可以让你做任何你能用汇编做的事情,但不是所有事情。如果你想做一些奇怪/特别/不寻常的事情,你可能需要使用汇编,尽管对于大多数(/所有?)真正的目的,C 就足够了,假设你有一个适合你的硬件的良好编译器。


编辑:进一步详细说明这一点...

C 不会为您提供任何有关它将被编译成的汇编指令的保证,并且仅提供有关将在内存中分配对象的一些部分指示。

一般来说,如果您需要以特定方式在内存中保存内容,或者如果您需要运行特定的汇编指令序列,您可能必须使用其他语言(可能直接汇编),否则一般C就可以了。

当然,我们可以找到尽可能多的特殊情况:如果您有一些特殊的汇编操作可以让您做一些高级的事情,C 可能无法利用这些操作。

C 被认为可以取代普通(也许我可以说通用)处理器上的汇编,但它在具有本机功能汇编的处理器或许多其他处理器上并不理想上下文。


你不能只说“C 的级别如何?”,你需要做相反的事情:定义你需要什么样的低级别东西,然后看看 C 是否能够做到这一点。

You need to define what you mean by low level in order to answer this question.

C lets you do kind of everything you could do with assembly, but well not everything. If you want to do something weird/particular/unusual you could need to use assembly, although for most(/all?) real purposes C will be more then enough, assuming you've got a good compiler for your hardware.


EDIT: Elaborating this a little further...

C doesn't give you any guarantee about the assembly instruction it will be compiled into, and only gives some partial indications about objects will be allocated in memory.

In general If you need to have stuff in memory in a particular way, of if you need a particular sequence of assembly instruction to be ran, you'll probably have to use some other language (probably assembly directly), otherwise in general C will be fine.

Of course we can find as many special cases as we want to: if your has got some special assembly operations that let you do fancy high level stuff, C won't probably be able to exploit these.

C has been thought to replace assembly on normal (maybe I could say general purpose) processors, it won't be ideal on processors with a native functional assembly or in many other contexts.


You cannot just say "how level is C?", you need to do the opposite: define what kind of low level stuff you need and then see if C is able to do that.

泅渡 2024-10-24 11:41:10

注册怎么样?局部变量声明上的关键字,要求编译器将其存储在 CPU 寄存器中 - 尽管它可能会忽略您的建议。

然而,GNU C 将此扩展为您可以指定您想要使用的特定寄存器!

register int foo asm ("a5");

How about register? A keyword on a local variable declaration that asks the compiler to store it in a CPU register - though it may ignore your suggestion.

However, GNU C extends this to the point where you can specify a particular register you'd like to use!

register int foo asm ("a5");
﹎☆浅夏丿初晴 2024-10-24 11:41:10

C 模糊的主要内容(在某种程度上它模糊了任何东西)是在一些边缘情况下的流控制。我在汇编中编写的唯一不能很好地转换为 C 的内容涉及在任意点跳入代码块,让它从该位置执行,定期检查某个状态条件,并在找到需要时,存储当前程序位置某处并退出。然后下一次,我跳回到上次退出的代码块的同一位置。我猜你会称它们为合作协程。

C 解决方案是使用两个独立的线程,通过信号量进行通信。但线程并不是 C 语言本身的一部分。

The main thing that C obscures, to the extent that it obscures anything at all, is flow control, in a few edge cases. The only thing I've ever written in assembly that didn't translate well to C involved hopping into a block of code at an arbitrary point, having it execute from that position, periodically checking a certain status condition and upon finding the need, storing the current program location somewhere and exiting. Then next time, I jumped back in at the same position as that block of code last exited. Co-operative coroutines, I guess you'd call them.

The C solution was to use two separate threads, communicating via semaphores. But threads aren't part of the C language itself.

↘人皮目录ツ 2024-10-24 11:41:10

如今,C 在许多情况下被用作“可移植汇编语言”,因为该语言的语义几乎只是底层机器架构之上的一层薄薄的饰面。

在过去,查看 C 代码行并很好地了解 C 代码将生成的实际机器语言是什么是相当简单的。

当将其与周围的开发环境结合起来时,可以很容易地完美控制数据结构的内存布局和整体内存布局。请注意,这种 C 代码总体上是不可移植的,因为它依赖于 C 标准允许灵活且基于实现的编译器的实现细节。但总的来说,C 代码本身大部分是可移植的。这是该语言的一个方面,这导致了它在 Unix 等系统中的流行,因为它允许操作系统的大部分内容可以跨机器移植,并且尽可能少地使用汇编语言等实现的具体实现方面。

如今,随着现代编译器及其优化,C 可以不再那么直白。由于编译器所做的实际优化,您在 C 代码中看到的内容可能不会按字面意思翻译成您在汇编中获得的内容。

一个简单的人为示例如下:

int func(int a, int b) {
    int c;
    int d;

    d = 10;
    return a * d;
}

在简单的编译器中,实现可能会在堆栈上分配 4 个 int,其中 2 个用于参数,2 个用于工作变量。一个明显的优化是根本不分配“c”变量,因为它未被使用。进一步的优化是忽略“d”,而简单地使用常量 10。甚至更聪明的编译器也可能会看到这个 func 的使用方式,并自动将其内联到调用函数中,从而完全消除子例程调用。

因此,一方面,您可以看到代码并说“这就是汇编中的样子”,从而将其视为汇编之上的低级包装器。另一方面,您可以打开现代编译器的优化,使它们更少的文字,从而保留您在代码中表达的语义,即使不是实际的完美实现。

C today is used in many cases as a "portable assembly language" because the semantics of the language can be pretty much just a thin veneer above the underlying machine architecture.

In the past, it was fairly straight forward to look at lines of C code and have a good idea what the actual machine language would be that the C code would generate.

When taken in it entirety along with its surrounding development environment, it is straightforward to have perfect control over memory layout of data structures and overall memory placement. Mind, this kind of C code is, as a whole, non portable as it relies on implementations details of the compiler that the C standard allows to be flexible and implementation based. But in the large, the C code itself can be MOSTLY portable. This is aspect of the language is what led to its popularity in such systems as Unix in allowing the bulk of the OS to be portable across machines with as little implementation specific aspects of an implementation being in, say, assembly language, as possible.

Today, with modern compilers and their optimizations, C can be less literal. What you see in the C code may not be translated literally in to what you get in the assembly because of the actual optimizations made by the compiler.

A simple contrived example is something like:

int func(int a, int b) {
    int c;
    int d;

    d = 10;
    return a * d;
}

In a simple compiler, an implementation may well allocate 4 int's on the stack, 2 for the arguments, and 2 for the work variables. An obvious optimization is to not allocate the 'c' variable at all, as it is unused. A further optimization is to ignore 'd' as well, and simply use the constant 10. And even smarter compiler may well see how this func is being used and inline it automatically in the calling function, eliminating the subroutine call completely.

So, on the one hand you can see the code and say "this is what it will look like in assembly", and thus treat it as a low level wrapper on top of assembly. On the other, you can turn the modern compilers optimizations up so that they are less literal, thus keeping the semantics of what you're expressing in code, if not the actual perfect implementation.

败给现实 2024-10-24 11:41:10

我所说的低级就是每个人说 C 是低级语言时的意思。如果我知道的话我就不用问这个问题了。 – ecounysis 27 秒前

这根本不是问题的样子。

如果您想知道“低级”的意思是什么,维基百科上有您可能想阅读的文章。

http://en.wikipedia.org/wiki/Low-level_programming_language

http://en.wikipedia.org/wiki/High-level_programming_language

当他们说 C 是低级语言时他们没有谈论任何程度的水平语言。语言要么是高级语言,要么是低级语言。它们是两种不同的类型或类别。

What I mean by low level is what everybody means when they say C is a low-level language. If I knew that I wouldn't have to ask the question. – ecounysis 27 secs ago

Thats not what the question looked like at all.

If what you want to know is what "low-level" means when they say that, wiki has articles you may want to read.

http://en.wikipedia.org/wiki/Low-level_programming_language

http://en.wikipedia.org/wiki/High-level_programming_language

When they say C is a low-level language they aren't talking about any degree. Either a language is higher level or low level. They are two different types or Categories.

执笏见 2024-10-24 11:41:10

你问这个问题的事实表明你对这个问题的天真。
只是事情没那么简单。

到底-
C 不具备汇编语言的完全灵活性,但也很接近。
它缺少尾递归等功能,

但对于大多数用途,C 可以完成您可能需要的所有功能

The fact that you ask this question expresses your naivete of the subject.
It's just not that simple.

In the end-
C does not have the complete flexibly of a assembly language, but it comes close.
It's missing features such as Tail Recursion

But for most purposes, C does everything you would -probably need-

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