[英]What is the difference between Mnemonics and Opcode
In assembly language under the instruction set of a 8085 microprocessor, suppose we have the following operation ADD B
. 在8085微处理器指令集的汇编语言下,假设我们有以下操作
ADD B
I know this means " Add the data of register of B to the Accumulator register and save the contents back to the Accumulator" . 我知道这意味着“ 将B寄存器的数据添加到累加器寄存器,然后将内容保存回累加器” 。
Here what is the mnemonic and what is the opcode. 这是什么助记符,什么是操作码。
ADD
(only ADD and not ADD B
) is the opcode or the mnemonic ? ADD
(仅ADD而不是ADD B
)是操作码还是助记符?
ADD
or ADD B
. ADD
或ADD B
Usually opcode refers to the type of operation (ADD), and register B is an operand. 通常,操作码是指操作类型(ADD),而寄存器B是操作数。 However, with a fixed and small number of operands, the same operation can have different opcode for all possible operands.
但是,使用固定数量的操作数,对于所有可能的操作数,相同的操作可以具有不同的操作码。
Some architectures have many different forms of the same mnemonic. 某些体系结构具有相同助记符的许多不同形式。 Things should be much easier to understand when looking at an example from such an architecture.
从这种架构查看示例时,事情应该更容易理解。
eg x86 has 5 forms of 32bit add. 例如x86有5种形式的32位添加。 (There are just as many forms for 8bit add, except of course that there isn't a 32bit immediate version. 16 and 64bit adds are encoded with prefix bytes in front of the 32bit encodings.)
(除了没有32位立即版本外,8位加法的形式也很多。除了16位和64位加法外,还用32位编码之前的前缀字节进行编码。)
Table format: OPCODE and operand encoding / MNEMONIC / OPERANDS (dest, src) 表格格式:OPCODE和操作数编码/ MNEMONIC / OPERANDS(目标,源代码)
05 id ADD EAX, imm32 # special-case save-one-bye for adding to the accumulator
81 /0 id ADD r/m32, imm32
83 /0 ib ADD r/m32, imm8
03 /r ADD r32, r/m32 # src can be memory
01 /r ADD r/m32, r32 # dest can be memory
So for add eax, edx
, there are two possible encodings: 01 D0
(picked by GNU as) or 03 whatever
(looking up the encoding of the mod/rm byte for the operands in the other order is left as an exercise for the reader.) 因此,对于
add eax, edx
,有两种可能的编码: 01 D0
(由GNU标识为)或03 whatever
(以其他顺序查找操作数的mod / rm字节的编码,作为阅读器的练习) 。)
The /0
means the unused src-reg bits in the mod/rm byte are borrowed as part of the opcode. /0
表示将mod / rm字节中未使用的src-reg位作为操作码的一部分借用。 83 /4 ib
is AND r/m32, imm8
. 83 /4 ib
是AND r/m32, imm8
。 When people say x86 machine code is nasty to decode, this is the kind of thing they're talking about (besides the variable-length nature, and the fact that optional prefix bytes mean the opcode isn't even the first byte... You have to mostly decode an instruction before you can even know how long it is to start decoding the next one. There's a reason parallel 4-wide decoding of x86 instructions is power-hungry.) 当人们说x86机器代码讨厌解码时,这就是他们在谈论的事情(除了可变长度性质之外,以及可选的前缀字节表示操作码甚至不是第一个字节的事实。您几乎必须先解码一条指令,然后才能知道开始解码下一条指令要花费多长时间。这是x86指令并行4宽度解码的原因,这非常耗电。
A more extreme case is that x86 uses mov
for several different kinds of instructions, determined by the operands: 一个更极端的情况是x86将
mov
用于几种不同类型的指令,这些指令由操作数决定:
mov r32, r/m32
(or the reverse) mov r32, r/m32
(或相反) I can't think of a case where two different mnemonics produce the same opcode. 我想不出两种不同助记符产生相同操作码的情况。 But a single mnemonic can produce different opcodes with different operands.
但是单个助记符可以产生具有不同操作数的不同操作码。
The operand can even be encoded into the opcode byte for very commonly used instructions, to save space (this is SergeyA's answer). 对于非常常用的指令,甚至可以将操作数编码为操作码字节,以节省空间(这是SergeyA的答案)。 You could think of x86's
B8
opcode as mov-imm32-to-eax. 您可以将x86的
B8
操作码视为mov-imm32-to-eax。 (the B8
to BF
opcodes are all mov-immediate to register, each with a different destination reg.) 32bit x86 has single-byte opcodes for inc/dec of registers. (
B8
至BF
操作码都是立即移动到寄存器的,每个操作码都有不同的目标寄存器。)32位x86具有用于寄存器inc / dec的单字节操作码。 x86-64 repurposed that contiguous range of 16 opcodes for use as REX prefix bytes (leaving the two-byte inc r/m32
form as the only option for inc eax
.) x86-64重新定义了16个操作码的连续范围,以用作REX前缀字节(保留两个字节的
inc r/m32
格式作为inc eax
的唯一选项。)
The opcode refers to the binary sequence that identifies the instruction. 操作码是指标识指令的二进制序列。 So for the 8085 I believe 0x80 would be the opcode for "ADD B"
因此,对于8085,我相信0x80将是“ ADD B”的操作码
A mnemonic is a human readable name that helps you remember the instructions. 助记符是易于理解的名称,可以帮助您记住说明。 So the string "ADD B" is a mnemonic for 0x80.
因此,字符串“ ADD B”是0x80的助记符。 "ADD B" is a lot easier to remember than 0x80.
“ ADD B”比0x80更容易记住。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.