Reputation: 39
as part of an assignment i have been trying to multiply two 32 bit numbers and store the result in a 64bit place. However, my result is incorrect. please help me figure why
[org 0x0100]
jmp start
multiplicand: dd 100122,0
multiplier: dd 66015
result: dd 0,0
start:
initialize: mov cl,16
mov bl,1
checkbit: test bl,[multiplier]
jz decrement
multiply: mov ax, [multiplicand]
add [result],ax
mov ax, [multiplicand+2]
adc [result+2], ax
mov ax, [multiplicand+4]
adc [result+4], ax
decrement: shl bl,1
shl [multiplicand],1
rcl [multiplicand+2],1
rcl [multiplicand+4],1
dec cl
jnz checkbit
mov ax, 0x4c00
int 0x21
the answer in afd debugger is F6B3A6 (16587802 IN DEC) whereas it should be 189F5C9A6 (6609553830 in dec). I have gone through the debugger but am unable to find anything wrong with the code.
Upvotes: 0
Views: 1543
Reputation: 62068
See the comments for a few d'oh's:
[org 0x0100]
jmp start
multiplicand: dd 100122,0
multiplier: dd 66015
result: dd 0,0
start:
initialize: mov cl,32 ; multipliers are 32-bit, so 32 iterations, not 16
mov bl,1
checkbit: test bl,[multiplier]
jz decrement
multiply: mov ax, [multiplicand]
add [result],ax
mov ax, [multiplicand+2]
adc [result+2], ax
mov ax, [multiplicand+4]
adc [result+4], ax
mov ax, [multiplicand+6] ; forgot this
adc [result+6], ax ; forgot this
decrement: ; shl bl,1 ; bl is 8-bit, but you need to test 32
shr word [multiplier+2],1 ; so, shift multiplier right instead
rcr word [multiplier],1 ; of shifting bl left
shl word [multiplicand],1 ; this is NASM, I'd rather tell
rcl word [multiplicand+2],1 ; the operand size here
rcl word [multiplicand+4],1 ; because it's unclear
rcl word [multiplicand+6],1 ; forgot this
dec cl
jnz checkbit
mov ax, 0x4c00
int 0x21
Upvotes: 2
Reputation: 30449
Replace mov cl,16
by mov cl,32
.
Don't forget [multiplicand+6]
.
Upvotes: 0