I have code example for multiplying two 16 bit numbers on 8086 and trying to update it for two 32 bit numbers multiplying.
start:
MOV AX,0002h ; 16 bit multipli
Solution n. 2 seems that not work if the product is large more then 32 Bit. Furthermore the shift instructions are wrong. This solution work correctly:
Procedure _PosLongIMul2; Assembler;
{INPUT:
DX:AX-> First factor (destroyed).
BX:CX-> Second factor (destroyed).
OUTPUT:
BX:CX:DX:AX-> Multiplication result.
TEMP:
BP, Di, Si}
Asm
Jmp @Go
@VR:DD 0 {COPY of RESULT (LOW)}
DD 0 {COPY of RESULT (HIGH)}
@Go:Push BP
Mov BP,20H {32 Bit Op.}
XOr DI,DI {COPY of first op. (LOW)}
XOr SI,SI {COPY of first op. (HIGH)}
Mov [CS:OffSet @VR ],Word(0)
Mov [CS:OffSet @VR+2],Word(0)
Mov [CS:OffSet @VR+4],Word(0)
Mov [CS:OffSet @VR+6],Word(0)
@01:ShR BX,1
RCR CX,1
JAE @00
Add [CS:OffSet @VR ],AX
AdC [CS:OffSet @VR+2],DX
AdC [CS:OffSet @VR+4],DI
AdC [CS:OffSet @VR+6],SI
@00:ShL AX,1
RCL DX,1
RCL DI,1
RCL SI,1
Dec BP
JNE @01
Mov AX,[CS:OffSet @VR]
Mov DX,[CS:OffSet @VR+2]
Mov CX,[CS:OffSet @VR+4]
Mov BX,[CS:OffSet @VR+6]
Pop BP
End;
This works between two unsigned integer.
If you want to multiply a 32 Bit unsigned integer for a 16 Bit unsigned integer, you can use the Mul instruction as follow:
Function Mul32Bit(M1:LongInt;M2:Word):LongInt; Assembler;
Asm
LEA SI,M1
Mov AX,[SS:SI]
Mov CX,[SS:SI+2]
{CX:AX contains number to multiply by}
Mov BX,M2
{BX contains number that multiply}
Mul BX
XChG AX,CX
Mov SI,DX
Mul BX
Add AX,SI
AdC DX,0
{DX:AX:CX contains the result of multiplication}
Mov DX,AX
Mov AX,CX
{DX:AX contains the partial result of m. and is the function's result}
End;