假设我们有号码510(0000 0001 1111 1110) 这存储在2个位置
lowHalf 0000 0001& highHalf 1111 1110
如果我们想要执行右移,我们如何计算进位?所以结果将是
0000 0011 1111 1100
答案 0 :(得分:0)
我建议右移前8位以查看是否有溢出,如果有溢出,则移动8位左边的位并加1。在我下面的例子中,我使用二进制掩码检查下半部分的第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