补充第i位 [英] Complementing ith bit
问题描述
假定AX包含一个介于0到15之间的数字.现在必须对BX中的"AXth"位进行补码.
例如,AX包含值 6
,那么BX中的第6位应该被补码.
Assuming AX contains a number between 0 and 15. Now the "AXth" bit in BX has to be complemented.
For example, AX contains the value 6
then the 6th bit in BX should be complemented.
我该如何实现?
推荐答案
xor
操作正好适合于此.
(所有代码均采用Intel语法.)
(All code in Intel syntax.)
mov cl, al
将位的索引移动到 cl
;8086支持 shl
一位或 cl
位.
Moves the bit's index to cl
; the 8086 supports either shl
by one bit or by cl
bits.
mov ax, 01h
在清除所有其他位的同时设置 ax
的第一位.
Sets first bit of ax
while clearing all other bits.
shl ax, cl
将设置的位向左移动;设置 ax
中的 cl
位(如您的示例中的第6位).
Shift the set bit to the left; sets the cl
th bit in ax
(as in your example, the 6th bit).
xor bx, ax
对(code)bx 中的相应位进行补码(取反). xor
之所以起作用,是因为
Complements (inverts) the corresponding bit in bx
. xor
works because
0 xor 0 = 0
1 xor 0 = 1
0 xor 1 = 1
1 xor 1 = 0
请注意,对于x86处理器系列中的更高版本的处理器,可以使用较短的变体来实现.
Note that for later processors of the x86 processor line there are shorter variants in order to accomplish that.
这篇关于补充第i位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!