Search code examples
assembly32-bitcpu-architecturex86-16

Assembly 8088: 32-bit signed Multiplication with addition and bit manipulation using 16-bit registers


I am trying to write assembly program to multiply two 32-bit signed numbers and store the answer in 64-bit number but my code only gives me the correct answer up to 32-bits. I have goggled it and searched many sites but can't satisfy myself. I can't get the problem although i debugged it many times.This is my first question as a student so I am really sorry if I am unclear.Thanks :)

; 32bit multiplication 
[org 0x0100]
multiplicand dd 0,9000008             ; 32bit multiplicand 64bit space 
multiplier: dd 45009                  ; 32bit multiplier 
result: dd 0,0                        ; 64bit result 
start:
    mov cx,32                         ; initialize bit count to 32
    mov bx, [multiplier]              ; load multiplier in dx and bx
    mov dx, [multiplier+2]            

checkbit:
    sar dx,1                          ; move right most bit in carry 
    rcr bx,1
    jnc skip                          ; skip addition if bit is zero
    mov ax,[multiplicand]
    add word[result],ax
    mov ax,[multiplicand+2]
    adc word[result+2],ax
    mov ax,[multiplicand+4]
    adc word[result+4],ax
    mov ax,[multiplicand+6]
    adc word[result+6],ax
    mov ax,[multiplicand+8]
    adc word[result+8],ax
    mov ax,[multiplicand+10]
    adc word[result+10],ax
    mov ax,[multiplicand+12]
    adc word[result+12],ax
    mov ax,[multiplicand+14]
    adc word[result+14],ax

skip:
    shl word [multiplicand],1
    rcl word [multiplicand+2],1
    rcl word [multiplicand+4],1
    rcl word [multiplicand+6],1
    rcl word [multiplicand+8],1
    rcl word [multiplicand+10],1
    rcl word [multiplicand+12],1
    rcl word [multiplicand+14],1
    dec cx
    jnz checkbit

mov ax,0x4c00                         ; terminate program
int 0x21

I think my logic is right but then i can't get what the error is. Any help is appreciated. :)


Solution

  • multiplicand dd 0,9000008             ; 32bit multiplicand 64bit space  
    

    You setup 64 bit space but your code modifies 128 bit space!!

    Also because of little endeanness you should swap the dwords of the multiplicand.