【问题标题】:How to multiply two 8 bit numbers using shift and add operations in 8085 microprocessor?如何在 8085 微处理器中使用移位和加法运算将两个 8 位数相乘?
【发布时间】:2020-02-29 11:41:00
【问题描述】:

我们应该使用 8085 微处理器的移位和加法运算将两个 8 位数字相乘。答案应该是一个 16 位的数字。必须使用移位和加法操作

【问题讨论】:

标签: binary 8085


【解决方案1】:

要了解解决方案,您必须熟悉 8085 中的 Rotate 指令,特别是对于此解决方案,您需要了解两件事

  1. RRC指令将位向右循环,LSB可以通过进位标志检查

  2. 将数字乘以 2(二进制为 10)导致左移一位(验证自己)

  3. 将数字与自身相加相当于将数字乘以 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
    

【讨论】:

  • 如果您使用左移而不是右移,则不需要交换操作。此外,我们可以通过在(正确的)右移后检查第二个操作数何时达到零来提前终止。
【解决方案2】:

假设我们要将两个整数 2723 相乘。因为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 的二进制表示(10111)。有了这个观察,我们可以编写以下伪代码来使用移位和加法运算来执行乘法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 寄存器对中可用。

【讨论】:

    猜你喜欢
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    相关资源
    最近更新 更多