【发布时间】:2016-03-10 20:49:58
【问题描述】:
假设我们有数字 510 (0000 0001 1111 1110) 这存储在 2 个位置
低半 0000 0001 & 高半 1111 1110
如果我们想要执行右移,我们如何计算进位位?所以结果将是
0000 0011 1111 1100
【问题讨论】:
假设我们有数字 510 (0000 0001 1111 1110) 这存储在 2 个位置
低半 0000 0001 & 高半 1111 1110
如果我们想要执行右移,我们如何计算进位位?所以结果将是
0000 0011 1111 1100
【问题讨论】:
我建议右移前 8 位以查看是否有溢出,如果有溢出,则将左移 8 位并加一个。在下面的示例中,我使用二进制掩码检查下半部分的第 8 位,使用 AND 操作码,检查该位是否已设置。
.
.ORIG x3000
LD R1, DATA ; load the lower 8 bits into R1
LD R3, MASK ; load the mask into R3
LEA R5, DATA ; load the mem location of data into R5
ADD R5, R5, #1 ; offset by 1 to get the upper 8 bits
ADD R1, R1, R1 ; bit shift right the data in R1
AND R4, R1, R3 ; check to see if we have overflowed
BRz NO_CARRY ; if bit[8] is set then we have a carry
CARRY
NOT R3, R3 ; 2's complement
ADD R3, R3, #1 ;
ADD R1, R1, R3 ; remove bit[8] from the lower 8 bits
LDR R3, R5, #0 ; load the value of the 8 upper bits into
; R3
ADD R3, R3, R3 ; bit shift right the upper 8 bits
ADD R3, R3, #1 ; add the carry bit to the upper bits
ST R1, DATA ; store the new value into DATA
STR R3, R5, #0 ; store the new value for the upper bits
BRnzp END
NO_CARRY
LDR R3, R5, #0 ; load the value of the 8 upper bits into
; R3
ADD R3, R3, R3 ; bit shift right the upper 8 bits
ST R1, DATA ; store the new value into DATA
STR R3, R5, #0 ; store the new value for the upper bits
END
HALT ; TRAP x25
; Variables
DATA .FILL b11111110
.FILL b00000001
MASK .FILL b0000000100000000
.END
【讨论】: