Author Topic: Multiply by shifting  (Read 4683 times)

wallanchan

  • Guest
Multiply by shifting
« on: October 31, 2013, 11:43:24 AM »
Teacher want us to do a LAB; however, I'm stuck. He want us to use data2 to tell how many time data1 is to be shifted and add up to get result. Please, someone help me understand this !! My code is after the LAB instructions

-------------------------------------------------------------------------------------------------------------------------------------
Multiplying by shifting

shift left 1 time => x2
Shift left 2 time => x4
Shift left 3 time => x8


Data want to multiply
10110110 = data1 = B6H
01101010 = data2 = 6AH

data2 position = 2+8+32+64

Data1 shift according to data2
-----------------------------------------
0000000101101100 <- shl 1 = x2
0000010110110000 <- shl 3 = x8
0001011011000000 <- shl 5 = x32
0010110110000000 <- shl6  = x64
-----------------------------------------------
0100101101011100 = 4B5C = result sum up

My code
---------------------------------------------------------------------------------------------------------------
org 100h

mov bx, 6      ; data1 test value
mov dx, 15     ; data2 test value

;=====================================================
mov cx, 8      ; loop counter

    loop1:   
       
        shr dx, 1  ; shift right 1time       
        jc loop2   ; if carry is one jump to loop2
       

        loop loop1

ret     


;=====================================================   
    loop2:       
        shl bx, 1       
        add ax, bx ; 
        jmp loop1
    loop loop2