【发布时间】:2020-02-29 11:41:00
【问题描述】:
我们应该使用 8085 微处理器的移位和加法运算将两个 8 位数字相乘。答案应该是一个 16 位的数字。必须使用移位和加法操作
【问题讨论】:
-
使用谷歌应该很容易找到策略,例如tutorialspoint.com/…到目前为止你尝试了什么?
我们应该使用 8085 微处理器的移位和加法运算将两个 8 位数字相乘。答案应该是一个 16 位的数字。必须使用移位和加法操作
【问题讨论】:
要了解解决方案,您必须熟悉 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
【讨论】:
假设我们要将两个整数 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 寄存器对中可用。
【讨论】: