我们应该使用 8085 微处理器的移位和加法运算将两个 8 位数字相乘。答案应该是一个 16 位的数字。必须使用移位和加法操作
问问题
857 次
2 回答
1
要了解解决方案,您必须熟悉 8085 中的 Rotate 指令,特别是对于此解决方案,您需要了解两件事
RRC 指令将位向右循环,并且可以从进位标志检查 LSB
将数字乘以 2(二进制中的 10)导致左移一位(验证自己)
将数字与自身相加相当于将数字乘以 2(二进制为 10),因此也将位移动 1 位
#ORG 8000 //initializing operands LXI H,7000H //data is stored in 7000H MOV E,M MVI D,00H INX H MOV A,M MVI C,08H LXI H, 0000H //multiplication algorithm starts here LOOP : RRC JNC SKIP DAD D //left shift is performed by adding number with itself //three lines just below this comment is shifting DE pair to left by 1 bit SKIP: XCHG //exchange HL and DE pair DAD H //Add HL with itself and store in HL XCHG //exchange HL and DE DCR C JNZ LOOP SHLD 7050H HLT #ORG7000 #DB 25,2A
于 2020-12-14T10:40:36.673 回答
0
假设我们想将两个整数27
和相乘23
。因为23
(10111
二进制)可以写成
2*11 + 1 = 2*(2*5 + 1) + 1 = ... = 2*(2*(2*(2*(2*0 + 1) + 0) + 1) + 1) + 1
. 因此,x * 23
可以表示为:2*(2*(2*(2*(2*0 + x) + 0) + x) + x) + x
。观察每个步骤中的加数项遵循23
( 1
, 0
, 1
, 1
, 1
) 的二进制表示。有了这个观察,我们可以编写以下伪代码来x * y
使用移位和加法运算来执行乘法运算。
let x be the first operand and y be the second one
set sum = 0
repeat
set sum = sum * 2
left shift y by one place
if the overflow bit after the shift is set then
set sum = sum + x
until y ≠ 0
output the sum as the result of x*y
让x=27
( 0x1B
) 和y=23
( 0x17
) 是两个 8 位整数,以下微程序执行所需的乘法。由于乘法可能需要 16 位来存储结果,我们使用HL
寄存器对进行计算。
LXI D,001BH ; DE <- 27(x)
MVI A,17H ; A <- 23(y)
LXI H,0000H ; HL <- 0(sum)
LOOP: DAD H ; sum <- sum*2
STC
CMC ; clear the carry flag before rotate
RAL ; y <- y<<1
JNC SKIP ; if overflow bit from y was not set
DAD D ; sum <- sum + x
SKIP: ORA A ; to update the zero flag
JNZ LOOP
HLT
结果27*23 = 621
( 0x026D
) 在HL
寄存器对中可用。
于 2021-06-25T04:44:36.563 回答