I am trying to do a calculation in emu8086, however there doesn't seem to be a root function available, I have been trying to make it myself, and I think I got it, however i'm getting errors on two lines:

  ;si is the number that I need the square root of.
  mov bx, 0   ;sets the cycle to start at 0
  mov dx, si  ;moves the number into dx for processing
  sqrt:       ;the loop, it continues as long as bx < si/2
  mov ax, si  ;from here starts this formula: x = (x + n / x) / 2
  div dx      ;I am getting a divide error -overflow here
  add ax, dx  
  mov ch, 2
  div ch      ;the same divide error -overflow here
  mov dx, ax
  mov ax, si
  mov ch, 2
  div ch
  cmp bx, ax  ;the formula repeats until bx = si/2
  jl sqrt
  cmp bx, ax  ; if bx = si/2, exit the loop.
  jge cnt:

  ;sqrt si
  cnt:
  mov si, dx

why are the two errors showing up?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top