字节码和汇编语言是同一回事吗? [英] Are Bytecode and Assembly Language the same thing?

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

问题描述

这个问题似乎很奇怪,但是我仍在尝试掌握虚拟机的概念.我已经阅读了几个答案,但是如果Java字节码(以及MSIL)与汇编语言相同,我还是听不懂.据我了解,字节码和汇编都被编译为机器代码,因此从抽象的角度来讲,它们处于同一级别,即比机器代码高出一步.字节码也只是一种汇编语言,即一种人类可读的机器代码形式.如果是,那么为什么仍然使用汇编语言?为什么不使用字节码(可在不同机器之间移植)而不是汇编语言(特定于单个机器体系结构)进行编程?谢谢

The question might seem odd, but I am still trying to grasp the concepts of virtual machines. I have read several answers, but I still don't get if Java bytecode (and MSIL as well) is the same as assembly language. As far as I understand both bytecode and assembly gets compiled to machine code, so speaking in terms of abstraction they are at the same level, i.e. one step above machine code. So is bytecode just an assembly language, i.e. a human readable form of machine code. If yes, then why is assembly language still used? Why not programming in bytecode (which is portable across different machines) instead of assembly language (which is specific to a single machine architecture)? Thanks

推荐答案

否.

Java字节码是二进制编程语言,不是人类可读形式",除非您认为一堆数字可读,或者使用反汇编程序将其反转为字节码文本助记符,或者最终是Java源代码形式本身.

Java bytecode is binary programming language, not in "human readable form", unless you consider bunch of number readable, or you use disassembler to reverse it into the bytecode text mnemonics, or eventually the Java source form itself.

汇编程序通常是目标机器的实际指令的文本助记符,彼此之间以1:1映射,因此汇编程序源中的一条指令将直接转换为一条机器代码指令(尽管某些CPU和汇编程序存在某些例外,例如,许多RISC汇编器会根据需要将立即数加载寄存器"转换为多条指令-以加载任何立即数,而本机代码只能加载特定位,并且您必须通过多条指令来组合整个值)

Assembly is usually text mnemonics of the actual instructions of the target machine, mapped 1:1 with each other, so one instruction in assembler source will translate directly into one machine code instruction (although some exceptions exists with some CPUs and assemblers, like for example many RISC assemblers will translate "load register with immediate value" into multiple instructions as needed - to load any immediate value, while the native machine code can load only particular bits, and you have to compose the whole value by several instructions).

与大多数CPU机器代码相比,Java字节码是一种相当高级的抽象语言,指令和内存模型的重叠非常小.唯一的相似之处在于,字节码以二进制形式存储,就像机器代码一样.

Java bytecode is quite high-level abstraction language compared to most of CPUs machine codes, having very tiny overlap of the instructions and memory model. The only similarity is, that bytecode is stored in binary form, just like machine code.

原则上,JVM是解释器.它将字节码即时转换为机器码.就是这样,在编译时由编译器以其他语言完成.

The JVM is interpreter in principle, ie. it translates the bytecode on the fly into machine code. That's the thing, which is done in other languages by compiler during compile time.

现代JVM并不是经典的纯解释器,而是在执行之前使用"JIT"(Just In Time)编译器将一小段Java字节码编译为本地机器代码,并使用缓存来避免对已知的第二次编译.class文件,并使用性能数据的运行时跟踪来更好地指示JIT编译器,哪些字节码应进行大量优化(经常运行或内部循环),而哪些应尽快进行编译,而不关注性能.

The modern JVMs are not classic pure interpreters, but use "JIT" (Just In Time) compiler to compile small pieces of java bytecode into native machine code, just ahead of it's execution, using caches to avoid second compilation of already known .class files, and also using runtime tracking of performance data to better instruct JIT compiler, which bytecode should be optimized heavily (run often or inner loop), and which should be just compiled ASAP, without focus on performance.

因此,对于现代JVM,很难谈论解释器,它是一个非常复杂的复杂解决方案. C#经常走得更远,有时会为通用平台提供一部分预编译为机器代码的二进制文件(字节码形式仅作为不常见平台的后备).

So with modern JVM it's hard to talk about interpreters, it's quite sophisticated and complex solution. C# goes quite often even one step further, delivering sometimes part of binaries pre-compiled into machine code for common platforms (having the bytecode form only as an fallback for uncommon platforms).

这一切(甚至不相似)都不会发生在机器代码中.它只是在CPU上执行.

None of this (not even similar) happens with machine code. It just executes on the CPU.

这篇关于字节码和汇编语言是同一回事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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