繁体   English   中英

LC3组装-如何切换二进制数的最低位?

[英]LC3 Assembly - How do I toggle the lowest order bit in a binary number?

我想知道是否有办法切换二进制的低位。
例如:
01000001->切换低序位-> 01000000
仅更改右侧的最后一位。
如果您不熟悉LC3,则只能执行以下操作:

这通常可以通过XOR操作来完成。

; field is R0
; R1 is mask of bits to toggle
; R2 is scratch
LD R0, #0b01000001
LD R1, #0b00000001
NOT R2, R0
AND R2, R2, R1
NOT R1, R1
AND R0, R0, R1
ADD R0, R0, R2
; result in R0

如果最低位为0,则需要加1。

如果最低位是1,则需要加-1。

因此,如果输入的是一个 ,那么:

 x = a and 1    ; x = 0/1 depending on lowest bit
 x = x + x      ; x = 0/2
 x = not x      ; x = -1/-3
 x = x + 2      ; x = +1/-1
 r = a + x      ; will toggle lowest bit of original a

我假设LC3中使用了两个补码负整数值,所以NOT 2 == -3-1 + 2 = +1

不幸的是,我不了解LC3,所以我只是希望我的步骤相当简单,可以通过LC3指令实现,并且该算法有意义。


另一种选择是使用NOT来切换该位(非加法):

 x = a and (not(1)) ; (0xFFFE in case of 16b word)
     ; x = copy of all bits of A except bottom bit (bottom bit = 0)
 y = not(a) and 1   ; extract toggled bottom bit (1/0)
 r = x + y          ; compose the value back from the upper:lowest bit(s)

编辑:这是米奇在他的回答中所做的。

暂无
暂无

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

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