汇编语言和编译语言 [英] Assembly language and compiled languages

查看:79
本文介绍了汇编语言和编译语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将两种语言都翻译成机器代码,汇编程序比编译语言快吗?

How is assembly faster than compiled languages if both are translated to machine code?

我说的是真正的编译语言,这些语言可以翻译成机器代码.不是C#或Java,它们先编译成中间语言,然后再由软件解释器等编译成本机代码.

I'm talking about truly compiled languages which are translated to machine code. Not C# or Java which are compiled to an intermediate language first and then compiled to native code by a software interpreter, etc.

维基百科上,我发现了一些不确定的东西与此相关.是因为从高级语言进行翻译会产生额外的机器代码吗?还是我的理解错误?

On Wikipedia, I found something which I'm not sure if it's in any way related to this. Is it because that translation from a higher level language generates extra machine code? Or is my understanding wrong?

称为汇编程序的实用程序用于将汇编语言语句转换为目标计算机的机器代码.汇编程序执行从助记符语句到机器指令和数据的或多或少的同构转换(一对一映射). 这与高级语言形成对比,在高级语言中,一条语句通常会导致很多机器指令..

推荐答案

好吧,确实与您的问题有些关系.关键是编译器由于各种原因有时会生成效率低下的机器代码,例如无法完全分析代码,插入自动范围检查,自动检查对象是否为null等.

Well, it relates a bit to your question, indeed. The point is that compilers produce inefficient machine code at times for various reasons, such as not being able to completely analyze your code, inserting automatic range checks, automatic checks for objects being null, etc.

另一方面,如果您手工编写汇编代码并知道自己在做什么,那么尽管编译器的行为可能有所调整,但您可能会写出比编译器更有效的内容.例如,通常您可以告诉它不要进行范围检查.

On the other hand if you write assembler code by hand and know what you're doing, then you can probably write some things much more efficient than the compiler, although the compiler's behavior may be tweaked and you can usually tell it not to do range checking, for example.

但是,大多数人不会编写比编译器更好的汇编代码,这仅仅是因为编译器是由知道很多真正奇怪但很酷的优化的人编写的.同样,在很多情况下,诸如展开循环之类的代码通常也很麻烦编写自己的代码,并使编写的代码更快.

Most people, however, will not write better assembler code than a compiler, simply because compilers are written by people who know a good deal of really weird but really cool optimizations. Also things like loop unrolling are usually a pain to write yourself and make the resulting code faster in many cases.

虽然通常计算机执行的所有内容都是机器代码,但是运行的代码却有很大不同,这取决于您在机器和程序员之间放置了多少抽象级别.对于汇编程序,这是一个级别,对于Java,还有更多...

While it's generally true that everything that a computer executes is machine code, the code that runs differs greatly depending on how many abstraction levels you put between the machine and the programmer. For Assembler that's one level, for Java there are a few more ...

许多人错误地认为,较高抽象层的某些优化可以带来较低的收益.不一定是这种情况,并且编译器可能只是在理解您要执行的操作时遇到了麻烦,而未能正确地对其进行优化.

Also many people mistakenly believe that certain optimizations at a higher abstraction layer pay off at a lower one. This is not necessarily the case and the compiler may just have trouble understanding what you are trying to do and fail to properly optimize it.

这篇关于汇编语言和编译语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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