XOR寄存器,寄存器(汇编器)
有时我们必须分析汇编代码(IA32), 我经常遇到这样的指令:
xor ax, ax
或与其他寄存器一起使用:xor dx, dx
, xor al, al
, ...
到底是什么这个做什么? (ax 异或 ax 总是给出 0 ?)
From time to time we have to analyze pieces of assembler code (IA32),
and more than often i come across an instruction that looks like this:
xor ax, ax
or with other registers aswell: xor dx, dx
, xor al, al
, ...
What exactly does this do ? (ax xor ax always gives 0 ?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将寄存器设置为 0 是一种常见的汇编程序习惯用法。
xor ax, ax
对应于ax = ax ^ ax
,正如您已经注意到的,它实际上是ax = 0 。
如果我没记错的话,主要优点是它的代码大小小于
mov ax, 0
It's a common assembler idiom to set a register to 0.
xor ax, ax
corresponds toax = ax ^ ax
which, as you already noticed, is effectivelyax = 0
.If I recall correctly the main advantage is that its code-size is smaller than
mov ax, 0
这正是它的作用——将寄存器的内容清零
That is exactly what it does -- zero the contents of a register
xor %ax, %ax,如前面的评论中所述,对应于 ax = ax xor ax。这实质上设置了 ax = 0。此外,它还会影响/修改一些 EFLAGS,例如 OF、CF、SF、PF 或 ZF。在这种情况下,将设置 PF 和 ZF 标志。
SF - 指示最后一次操作的结果是否产生最高有效位设置为 1 的值
。 PF - 指示最后一次操作结果的二进制表示形式中设置的位数是奇数还是偶数。
ZF - 如果数学/逻辑运算的结果为零则设置它,否则重置。
下面显示了使用 GDB 片段的示例。
指令:xor %ax,%ax
在“xor”之前
在“xor”之后
xor %ax, %ax, as stated in earlier comments corresponds to ax = ax xor ax. This essentially set ax = 0. In addition, it also affects/modifies some of the EFLAGS such as OF, CF, SF, PF or ZF. In this case, PF and ZF flags will be set.
SF - Indicates whether the result of the last operation resulted in a value whose most significant bit is set to 1.
PF - Indicates if the number of set bits is odd or even in the binary representation of the result of the last operation.
ZF - It is set if the result of the mathematical/logical operation is zero or reset otherwise.
Example is shown below using GDB snippets.
Instruction: xor %ax,%ax
Before "xor"
After "xor"