Assembly 如何在emu8086中求平方根
我试图在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
;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位除法以确保商不会溢出。