The code written in answer 1 is the only exact. I written similar code, that divide D1 by D2:
Function Div32Bit(D1:LongInt;D2:Word):LongInt; Assembler;
Asm
LEA SI,D1
Mov CX,[SS:SI]
Mov AX,[SS:SI+2]
{AX:CX contains number to divide by}
Mov BX,D2
{BX contains number that divide}
XOr DX,DX
Div BX
XChg AX,CX
Div BX
{CX:AX contains the result of division}
{DX contains the rest of division}
Mov DX,CX
{DX:AX contains the result of division and is the function's result}
End;
But this method isn't valid to divide two signed number.
To divide two signed number:
Function IDiv32Bit(D1:LongInt;D2:Integer):LongInt; Assembler;
Asm
LEA SI,D1
Mov CX,[SS:SI]
Mov AX,[SS:SI+2]
{AX:CX contains number to divide by}
Cmp AX,32768
CmC
SbB SI,SI
XOr CX,SI
XOr AX,SI
Sub CX,SI
SbB AX,SI
{AX:CX contains the absolute value of the number to divide by}
Mov BX,D2
{BX contains number that divide}
Cmp BX,32768
CmC
SbB DX,DX
XOr BX,DX
Sub BX,DX
{BX contains the absolute value of the number that divide}
XOr SI,DX
{SI contains the sign of division}
XOr DX,DX
Div BX
XChg AX,CX
Div BX
{CX:AX contains the absolute value of the result of division}
{DX contains the absolute value of the rest of division}
XOr AX,SI
XOr CX,SI
Sub AX,SI
SbB CX,SI
{CX:AX contains the result of division}
Mov DX,CX
{DX:AX contains the result of division and is the function's result}
End;