16-bit division operation in 8086
uses the 32 bit value formed by the contents of registers DX:AX
as dividend, the quotient is returned in AX
and the remainder in DX
. If the quotient is too big to fit in 16 bits, an INT 0
happens.
So when you are doing:
mov ax, si ;from here starts this formula: x = (x + n / x) / 2
div dx ;I am getting a divide error -overflow here
You are dividing not the value of AX
by DX
, but the value of DX:AX
by DX
. As DX
begins with a copy of SI
, as well as AX
does, your dividend is SI*65536+SI
.
SI*65536+SI
divided by SI
yields 65537
, which is bigger than AX
can hold, hence, division error.
About this one:
mov ch, 2
div ch ;the same divide error -overflow here
8-bit division uses AX
as dividend, returning the quotient in AL
and remainder in AH
. Once again, if the quotient is too big for AL
, an overflow happens. In your case, the overflow will happen whenever AX
has a value equal or bigger than 512
.