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

你能举例说明它们的不同吗?


当前回答

还有一点没有提到,那就是有几种不同类型的汇编代码。在最基本的形式中,指令中使用的所有数字都必须指定为常量。例如:

$1902: BD 37 14 : LDA $1437,X
$1905: 85 03    : STA $03
$1907: 85 09    : STA $09
$1909: CA       : DEX
$190A: 10       : BPL $1902

The above bit of code, if stored at address $1900 in an Atari 2600 cartridge, will display a number of lines in different colors fetched from a table which starts at address $1437. On some tools, typing in an address, along with the rightmost part of the line above, would store to memory the values shown in the middle column, and start the next line with the following address. Typing code in that form was much more convenient than typing in hex, but one had to know the precise addresses of everything.

大多数汇编程序允许使用符号地址。上面的代码应该是这样写的:

rainbow_lp:
  lda ColorTbl,x
  sta WSYNC
  sta COLUBK
  dex
  bpl rainbow_lp

汇编程序将自动调整LDA指令,以便它引用映射到标签ColorTbl的任何地址。使用这种类型的汇编器,编写和编辑代码要比手工输入和维护所有地址容易得多。

其他回答

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

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

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.


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.

也有不同的解释语言。解释型语言依赖于特殊解释器程序的机器码。在基本级别上,解释器解析源代码,并立即将命令转换为新的机器代码并执行它们。现代解释器现在要复杂得多:一次计算源代码的整个部分,在可能的地方缓存和优化,以及处理复杂的内存管理任务。

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.

汇编代码是人类可读的机器代码表示:

mov eax, 77
jmp anywhere

机器代码是纯十六进制代码:

5F 3A E3 F1

我猜你指的是目标文件中的目标代码。这是一种机器代码的变体,不同之处在于跳转是参数化的,这样链接器就可以填充它们。

汇编程序用于将汇编代码转换为机器代码(目标代码)。 链接器链接多个对象(和库)文件以生成可执行文件。

我曾经用纯十六进制写过一个汇编程序(没有汇编程序可用),幸运的是,这是在古老的6502上写的。但我很高兴有奔腾操作码的汇编器。

程序的源文件被编译成目标文件,然后链接器将这些目标文件链接在一起,生成一个包含您的体系结构的机器代码的可执行文件。

当目标文件和可执行文件被文本编辑器打开时,都以可打印和不可打印字符的形式涉及到体系结构的机器代码。

尽管如此,文件之间的二分法是,目标文件可能包含未解析的外部引用(例如printf)。因此,它可能需要链接到其他对象文件。也就是说,需要解析未解析的外部引用,以便通过与其他目标文件(如C/ c++运行时库)的链接来获得良好的可运行的可执行文件。

还有一点没有提到,那就是有几种不同类型的汇编代码。在最基本的形式中,指令中使用的所有数字都必须指定为常量。例如:

$1902: BD 37 14 : LDA $1437,X
$1905: 85 03    : STA $03
$1907: 85 09    : STA $09
$1909: CA       : DEX
$190A: 10       : BPL $1902

The above bit of code, if stored at address $1900 in an Atari 2600 cartridge, will display a number of lines in different colors fetched from a table which starts at address $1437. On some tools, typing in an address, along with the rightmost part of the line above, would store to memory the values shown in the middle column, and start the next line with the following address. Typing code in that form was much more convenient than typing in hex, but one had to know the precise addresses of everything.

大多数汇编程序允许使用符号地址。上面的代码应该是这样写的:

rainbow_lp:
  lda ColorTbl,x
  sta WSYNC
  sta COLUBK
  dex
  bpl rainbow_lp

汇编程序将自动调整LDA指令,以便它引用映射到标签ColorTbl的任何地址。使用这种类型的汇编器,编写和编辑代码要比手工输入和维护所有地址容易得多。

汇编是人类可以理解的简短的描述性术语,可以直接转换为CPU实际使用的机器代码。

虽然汇编程序在某种程度上为人类所理解,但它仍然是低水平的。做任何有用的事情都需要大量的代码。

所以我们使用高级语言,如C, BASIC, FORTAN(好吧,我知道我和自己约会过)。当编译这些代码时,会生成目标代码。早期语言以机器语言作为目标代码。

现在的许多语言,如JAVA和c#,通常编译成字节码,这些字节码不是机器代码,而是在运行时容易解释以产生机器代码的字节码。