Sarthak
Sarthak

Reputation: 218

How to multiply two 8 bit numbers using shift and add operations in 8085 microprocessor?

We are supposed to multiply two 8 bit numbers using shift and add operations of 8085 microprocessor. Answer should be a 16 bit number. Use of shift and add operation is compulsory

Upvotes: 0

Views: 2202

Answers (2)

codeR
codeR

Reputation: 322

Suppose we want to multiply two integers 27 and 23. Since 23 (10111 in binary) can be written as 2*11 + 1 = 2*(2*5 + 1) + 1 = ... = 2*(2*(2*(2*(2*0 + 1) + 0) + 1) + 1) + 1. Thus, x * 23 can be expressed as: 2*(2*(2*(2*(2*0 + x) + 0) + x) + x) + x. Observe that the addend terms in each step follows the binary representation of 23 (1, 0, 1, 1, 1). With this observation, we can write the following pseudocode to perform the multiplication x * y using shift and add operations.

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

Let x=27 (0x1B) and y=23 (0x17) be two 8-bit intergers, the follwing microprogram performs the required multiplication. As the multiplication may require 16 bits to store the result we use the HL register pair for the calculation.

      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

The result, 27*23 = 621 (0x026D), is available in the HL register pair.

Upvotes: 0

Aayush Neupane
Aayush Neupane

Reputation: 1246

To understand the solution you must be familiar with Rotate instructions in 8085,particularly for this solution you need to understand two things

  1. RRC instruction rotates bits to the right and LSB can be checked from carry flag

  2. multiplying number by 2(10 in binary) results in left shif by one bit (verify yourself)

  3. adding number with itself is equivaent to multiplying number by 2(10 in binary) and hence also shift bits by 1 bit

     #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
    

Upvotes: 1

Related Questions