汇编代码 vs 机器代码 vs 目标代码? [英] Assembly code vs Machine code vs Object code?

查看:29
本文介绍了汇编代码 vs 机器代码 vs 目标代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标代码、机器代码和汇编代码有什么区别?

What is the difference between object code, machine code and assembly code?

你能举一个直观的例子来说明它们的区别吗?

Can you give a visual example of their difference?

推荐答案

机器代码 是可以由 CPU 直接执行的二进制(1 和 0)代码.如果您在文本编辑器中打开机器代码文件,您会看到垃圾,包括不可打印的字符(不,不是那些 不可打印的字符;)).

Machine code is binary (1's and 0's) code that can be executed directly by the CPU. If you open a machine code file in a text editor you would see garbage, including unprintable characters (no, not those unprintable characters ;) ).

目标代码是尚未链接到完整程序中的机器代码的一部分.它是构成完整产品的特定库或模块的机器代码.它还可能包含在已完成程序的机器代码中找不到的占位符或偏移量.链接器将使用这些占位符和偏移量将所有内容连接在一起.

Object code is a portion of machine code not yet linked into a complete program. It's the machine code for one particular library or module that will make up the completed product. It may also contain placeholders or offsets not found in the machine code of a completed program. The linker will use these placeholders and offsets to connect everything together.

汇编代码是纯文本和(有些)人类可读的源代码,主要与机器指令直接 1:1 模拟.这是通过对实际指令、寄存器或其他资源使用助记符来实现的.示例包括用于 CPU 跳转和乘法指令的 JMPMULT.与机器代码不同,CPU 不理解汇编代码.您可以使用汇编器编译器将汇编代码转换为机器代码,尽管我们通常认为编译器与高级编程语言相关联,这些语言进一步抽象为CPU 指令.

Assembly code is plain-text and (somewhat) human read-able source code that mostly has a direct 1:1 analog with machine instructions. This is accomplished using mnemonics for the actual instructions, registers, or other resources. Examples include JMP and MULT for the CPU's jump and multiplication instructions. Unlike machine code, the CPU does not understand assembly code. You convert assembly code to machine code with the use of an assembler or a compiler, though we usually think of compilers in association with high-level programming language that are abstracted further from the CPU instructions.

构建一个完整的程序涉及使用汇编语言或更高级别的语言(如 C++)为程序编写源代码.源代码被组装(对于汇编代码)或编译(对于高级语言)为目标代码,并且各个模块链接在一起成为最终程序的机器代码.在非常简单的程序的情况下,可能不需要链接步骤.在其他情况下,例如在 IDE(集成开发环境)中,链接器和编译器可以一起调用.在其他情况下,可能会使用复杂的ma​​ke 脚本或解决方案 文件来告诉环境如何构建最终应用程序.

Building a complete program involves writing source code for the program in either assembly or a higher level language like C++. The source code is assembled (for assembly code) or compiled (for higher level languages) to object code, and individual modules are linked together to become the machine code for the final program. In the case of very simple programs the linking step may not be needed. In other cases, such as with an IDE (integrated development environment) the linker and compiler may be invoked together. In other cases, a complicated make script or solution file may be used to tell the environment how to build the final application.

还有解释性语言表现不同.解释型语言依赖于特殊解释器程序的机器代码.在基本层面,解释器解析源代码并立即将命令转换为新的机器代码并执行它们.现代解释器现在要复杂得多:一次评估整个源代码部分,在可能的情况下缓存和优化,以及处理复杂的内存管理任务.

There are also interpreted languages that behave differently. Interpreted languages rely on the machine code of a special interpreter program. At the basic level, an interpreter parses the source code and immediately converts the commands to new machine code and executes them. Modern interpreters are now much more complicated: evaluating whole sections of source code at a time, caching and optimizing where possible, and handling complex memory management tasks.

最后一种程序类型涉及使用运行时环境虚拟机.在这种情况下,程序首先被预编译为较低级别的中间语言字节码.字节码然后由虚拟机加载,它会及时将其编译为本机代码.这里的优势是虚拟机可以利用程序运行时可用的优化以及针对特定环境的优化.编译器属于开发人员,因此必须生成可以在许多地方运行的相对通用(优化程度较低)的机器代码.但是,运行时环境或虚拟机位于最终用户的计算机上,因此可以利用该系统提供的所有功能.

One final type of program involves the use of a runtime-environment or virtual machine. In this situation, a program is first pre-compiled to a lower-level intermediate language or byte code. The byte code is then loaded by the virtual machine, which just-in-time compiles it to native code. The advantage here is the virtual machine can take advantage of optimizations available at the time the program runs and for that specific environment. A compiler belongs to the developer, and therefore must produce relatively generic (less-optimized) machine code that could run in many places. The runtime environment or virtual machine, however, is located on the end user's computer and therefore can take advantage of all the features provided by that system.

这篇关于汇编代码 vs 机器代码 vs 目标代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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