Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 如何在emu8086中求平方根_Assembly_X86 16 - Fatal编程技术网

Assembly 如何在emu8086中求平方根

Assembly 如何在emu8086中求平方根,assembly,x86-16,Assembly,X86 16,我试图在emu8086中进行计算,但是似乎没有可用的根函数,我一直在尝试自己进行计算,我想我得到了它,但是我在两行中得到了错误: ;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 contin

我试图在emu8086中进行计算,但是似乎没有可用的根函数,我一直在尝试自己进行计算,我想我得到了它,但是我在两行中得到了错误:

  ;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
;si是我需要平方根的数字。
mov-bx,0;将循环设置为从0开始
mov-dx,si;将数字移动到dx中进行处理
sqrt:;只要bx

为什么会出现这两个错误?

8086
中的16位除法运算使用寄存器
DX:AX
内容形成的32位值作为红利,商在
AX
中返回,余数在
DX
中返回。如果商太大,无法容纳16位,则会发生
INT 0

因此,当您进行以下操作时:

mov ax, si  ;from here starts this formula: x = (x + n / x) / 2
div dx      ;I am getting a divide error -overflow here
不是将
AX
的值除以
DX
,而是将
DX:AX
的值除以
DX
。由于
DX
SI
的副本开始,就像
AX
一样,您的红利是
SI*65536+SI

SI*65536+SI
除以
SI
得到
65537
,该值大于
AX
所能承受的值,因此产生除法误差

关于这个:

mov ch, 2
div ch      ;the same divide error -overflow here

8位除法使用
AX
作为红利,返回
AL
中的商和
AH
中的余数。同样,如果商对于
AL
来说太大,则会发生溢出。在您的情况下,每当
AX
的值等于或大于
512

时,就会发生溢出,您必须确保在放置新的除数时,案例中的目标容器
dx
ch
可以容纳div返回的位数。emu8086中不支持FPU?如果emu8086支持FPU,FSQRT会做你想做的事情。有办法解决吗?我想是的。不要使用寄存器DX保存值,并在第一次除法之前将其设置为零。对于第二个除法,使用16位除法以确保商不会溢出。