
First of all, this is a fallacy (are you feeling the urge to argue with me? Please hold on, let me explain slowly).
No matter what language is used, the program ultimately runs on the CPU, and only the CPU can execute the program. The CPU does not know what assembly language, C language, or even Java, PHP, Python, etc. are; it does not understand the many layers of interpretation and compilation that the instructions have gone through before reaching it. Regardless of the language, the compiler ultimately translates everything into machine instructions. Therefore, in this regard, the machine instructions generated by an assembly language compiler are no different from those generated by a C compiler.
So why is it said that assembly language is faster?
I believe it should be said that the number of instructions generated by assembly language is fewer, which makes it seem faster. It is not that assembly language itself is inherently powerful; rather, assembly language is a symbolic representation of machine instructions. This means that each symbol in assembly language corresponds to one machine instruction, and they are one-to-one. Thus, writing a program in assembly language is akin to directly writing machine instructions. The assembly language compiler does not add extra statements, so programs written in assembly language are more direct, and the CPU does not waste time executing irrelevant instructions, which naturally makes it faster.
Now, let’s take a look at what the C compiler does for us. To make it easier for C programmers to code, the C compiler does a lot of work behind the scenes. Moreover, for reasons of generality, usability, or other considerations, the C compiler often adds extra C language code to support the program, which increases the actual amount of C code significantly. Additionally, during the compilation phase, C code is first compiled into assembly code, which is then translated into machine instructions by the assembler. Since the C code has become redundant, the compiled assembly code will naturally also be redundant, leading to a greater number of machine instructions.
Most people prefer to write programs in C because C is powerful and easier to master. However, this advantage comes at a cost. C programmers do not have to consider stack switching or which segment to use. These necessary considerations are left to the compiler. Furthermore, for reasons of generality, functionality, and even safety, additional code must be written behind the scenes. For instance, when printing a string, the C language’s printf() does a lot of work, including checking the data type to be printed and handling formatting, decimal precision, etc. In contrast, in assembly language, you only need to move a character to the video memory address; the string is just a few more mov operations. You see, C language does a lot behind the scenes to make it easier for developers.
In summary: High-level languages like C need to consider many factors for generality, often adding extra code. Therefore, the compiled assembly code tends to be larger, with many parts being peripheral functions that do not directly contribute to the core functionality. It is more direct to write the functional parts directly in assembly language. After C language is compiled into machine instructions, the generated machine instructions naturally include these extra parts, resulting in the execution of some seemingly “useless” instructions, which is why it is slower than directly using assembly language.