asn
asn

Reputation: 2607

Shift and add method for multiplying numbers give garbage output

I have made a procedure that multiplies 2 numbers using add and shift method and stores the result in a buff variable.

Here, buff1 and buff2 are initialised in the data section of the code as word type.

buff1 dw 0AH

buff2 dw 03H

And buff is declared in .bss section as 4 byte.

buff resb 4

Here is the procedure that does the multiplication :

shift_add:
    movzx eax,word[buff1]     ;2byte
    movzx ebx,word[buff2]
    mov edx,0H
    mov rcx,16  ;integer
    backs:
        shr bl,1
        jnc haha
        add edx,eax
    haha:
        shl eax,1
    loop backs  
    mov dword[buff],edx
    print buff,4
ret

Why is the above code giving me garbage output ?enter image description here

Upvotes: 0

Views: 114

Answers (1)

toncsi
toncsi

Reputation: 303

try shr ebx,1 instead of shr bl,1.

Upvotes: 1

Related Questions