简体   繁体   English

汇编语言和机器语言之间有什么关系?

[英]What's the relationship between assembly language and machine language?

Are assembly language and machine language (for the same underlying system) really the same? 汇编语言和机器语言(对于相同的底层系统)是否真的相同? Are there any differences between these two concepts? 这两个概念之间有什么不同吗?

Assembly language is a convenience mechanism over the machine language. 汇编语言是一种比机器语言更方便的机制。 With assembly language you use mnemonic sequences instead of numeric operation codes and can use symbolic labels instead of manually calculating offsets. 使用汇编语言,您可以使用助记符序列而不是数字操作代码,并且可以使用符号标签而不是手动计算偏移量。 It also protects you from really dumb errors - like typing a malformed processor instruction. 它还可以保护您免受真正的愚蠢错误 - 比如输入格式错误的处理器指令。

Otherwise the assembly language is the equivalent of the machine language. 否则汇编语言相当于机器语言。 Sometimes you will have an old assembler that will not support mnemonics for some instructions of the newer processors - then you can still insert operation codes directly into the program. 有时您会有一个旧的汇编程序,它不支持助记符来处理新处理器的某些指令 - 然后您仍然可以将操作代码直接插入到程序中。

Machine language is the "Bit encoding" of a CPU's opcodes. 机器语言是CPU的操作码的“位编码”。

Assembly langauge is the "Symbolic encoding" of a CPU's opcodes. Assembly langauge是CPU的操作码的“符号编码”。

So for Example Symbolically: 因此,例如符号:

loop:  dec R1    # Decrement register R1
       bnq loop  # Branch if not equal to zero to
                 # address "loop"

Becomes Bit encoding: 成为比特编码:

# Mythical CPU Machine code 4 bits operation,
#                           4 bit "option"
       0x41      # 4 is a "dec" and represents r1;
       0x7E      # 7 is bnq and E means PC -2;

Generally it's a one to one relationship, however some assembly languages will ocationally have extra assembly instructions that map to either multiple machine code instructions or reuse another opcode. 通常它是一对一的关系,但是一些汇编语言将在国际上具有额外的汇编指令,这些汇编指令映射到多个机器代码指令或重用另一个操作码。 Such as using machine code "xor R1,R1" as a "clr R1" or something very similar. 例如使用机器代码“xor R1,R1”作为“clr R1”或类似的东西。

In addition assembly languages will tend to support "macro programming" which in the 80's when assembly was used extensively gave the source code a more "high level" appearance. 此外,汇编语言将倾向于支持“宏编程”,在80年代,当广泛使用汇编时,源代码具有更“高级”的外观。 I've personally written assembly macros that looked like "plot x,y" and "Hex Val" to simplify common operations. 我亲自编写了看起来像“plot x,y”和“Hex Val”的汇编宏来简化常见操作。

For example: 例如:

# Mythically CPU Macro
.macro spinSleep x,y
            ld #x,y
localLoop:  dec y
            brq localLoop
.endmacro
# Macro invocation
            spinSleep 100,R1
# Macro expantion
            ld #100,R1
localLoopM: dec R1
            brq localLoopM   # localLoopM is "Mangled" for localization.

I found a really good explanation, thought to post it here, so that others could read it: 我发现了一个非常好的解释,想在这里发布,以便其他人可以阅读它:

Machine language is the actual bits used to control the processor in the computer, usually viewed as a sequence of hexadecimal numbers (typically bytes). 机器语言是用于控制计算机中处理器的实际位,通常被视为十六进制数字序列(通常为字节)。 The processor reads these bits in from program memory, and the bits represent "instructions" as to what to do next. 处理器从程序存储器中读取这些位,这些位表示下一步操作的“指令”。 Thus machine language provides a way of entering instructions into a computer (whether through switches, punched tape, or a binary file). 因此,机器语言提供了一种将指令输入计算机的方式(无论是通过交换机,穿孔带还是二进制文件)。

Assembly language is a more human readable view of machine language. 汇编语言是一种更易读的机器语言视图。 Instead of representing the machine language as numbers, the instructions and registers are given names (typically abbreviated words, or mnemonics, eg ld means "load"). 指令和寄存器不是将机器语言表示为数字,而是给出名称(通常是缩写词或助记符,例如ld表示“加载”)。 Unlike a high level language, assembler is very close to the machine language. 与高级语言不同,汇编程序非常接近机器语言。 The main abstractions (apart from the mnemonics) are the use of labels instead of fixed memory addresses, and comments. 主要的抽象(除了助记符)是使用标签而不是固定的内存地址和注释。

An assembly language program (ie a text file) is translated to machine language by an assembler. 汇编语言程序(即文本文件)由汇编程序转换为机器语言。 A disassembler performs the reverse function (although the comments and the names of labels will have been discarded in the assembler process). 反汇编程序执行反向功能(尽管标签的注释和名称将在汇编程序进程中被丢弃)。

Source : What is difference between machine language and assembly language? 来源: 机器语言和汇编语言有什么区别?

In Assembly, instructions are easier-to-understand representations of CPU instructions. 在汇编中,指令是更容易理解的CPU指令表示。

But the assembler also makes, for example, addressing easier: 但是汇编程序也使得例如更容易解决:

  • In machine language you have to know the distance (in address space) between where you are and where you want to jump to 在机器语言中,您必须知道您所在位置和要跳转到的位置之间的距离(在地址空间中)
  • In Assembly language you call one address "iWantToJumpHere" and then you can say "jump iWantToJumpHere" 在汇编语言中,您调用一个地址“iWantToJumpHere”,然后您可以说“jump iWantToJumpHere”

This makes assembly much easier to maintain, especially when the distance between the addresses changes. 这使得装配更容易维护,尤其是当地址之间的距离改变时。

machine language is what the chip understands Assembly is what you understand 机器语言是芯片理解汇编是你理解的

Every assembly instruction has a machine language equivalent. 每个汇编指令都有一个等效的机器语言。 x86 has a few single-byte instructions, but they're variable length and can be up to 15 bytes long (including optional prefixes) x86有一些单字节指令,但它们的长度可变,最长可达15个字节(包括可选的前缀)

 machine code bytes |   x86 assembly language
8D B0 00 36 65 C4    lea    esi, [eax - 1000000000]     
BB 00 FC FF FF       mov    ebx, -1024
43                   inc    ebx
41                   inc    eax
3B CA                cmp    ecx,edx
C3                   ret

C5 F5 72 D2 01       vpsrld ymm1,ymm2,0x1        ; AVX2
C5 F5 D4 6D 88       vpaddq ymm5,ymm1,YMMWORD PTR [ebp-0x78]
C5 CD D4 AD 68 ff ff ff vpaddq ymm5,ymm6,YMMWORD PTR [ebp-0x98]

汇编级语言是通过允许程序员编写助记符而不是二进制代码(机器代码)来使编程变得简单的第一个有意识的步骤。

Machine Language: 机器语言:

  1. It is the first generation programing language 它是第一代编程语言
  2. It is much difficult than Assembly Language 它比汇编语言困难得多
  3. Difficult to understand by the human 很难被人类理解
  4. It is in the form of 0s and 1s 它的形式为0s和1s
  5. Machine Language varies from platform 机器语言因平台而异
  6. It cannot be changed easily 它不容易改变
  7. It does not support modification 它不支持修改
  8. The risk of existence of error is high 存在错误的风险很高
  9. Binary code is hard to memorize 二进制代码难以记忆
  10. No need of compiler 不需要编译器

Assembly Language: 汇编语言:

  1. It is a second generation programming language 它是第二代编程语言
  2. It is less difficult than machine language 它比机器语言困难
  3. Easy to understand by the human 人类容易理解
  4. It uses symbolic instructions, descriptive names for data items and memory location 它使用符号指令,数据项的描述性名称和内存位置
  5. The Assembly Language is platform dependent 汇编语言取决于平台
  6. It is easily Modifiable 它很容易修改
  7. It's support changes 这是支持变化
  8. The risk of occurrence of error is reduced 发生错误的风险降低
  9. Memorability is high 可记忆性很高
  10. It is needed compiler and knows as assembler 它需要编译器并且知道为汇编程序

Assembly language is first converted to machine language by the assembler. 汇编语言首先由汇编程序转换为机器语言。 which is stored in memory (RAM) processor/cup fetch it and store in from memory to register and follow the instruction set one after one. 存储在存储器(RAM)处理器/杯中的存储器从存储器中取出并存储到寄存器中并依次跟随指令集。

Assembly Language is the symbolic encode of opcode (operation code) that is understand by humans and only use to instruct computer processor (hardware operation) and robot (robotic operation) to perform specific tasks. 汇编语言是人类理解的操作码(操作码)的符号编码,仅用于指示计算机处理器(硬件操作)和机器人(机器人操作)执行特定任务。 This is an understandable language to human. 这对人类来说是一种可以理解的语言。 This language is only use to instruct hardware operation and definitely not use to create software programme. 该语言仅用于指示硬件操作,绝对不用于创建软件程序。 A assembler is use to convert this symbolic encode part of opcode (operation code) into machine language. 汇编程序用于将操作码(操作代码)的这个符号编码部分转换为机器语言。 Operation Code (Opcode) is a part of machine language. 操作代码(操作码)是机器语言的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM