【问题标题】:How to draw the square root in emu8086如何在emu8086中画平方根
【发布时间】:2014-01-01 00:54:04
【问题描述】:

我正在尝试在 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

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

【问题讨论】:

  • 您必须确保当您放置新的划分数字时,目标容器在您的案例dxch 可以容纳 div 返回的位数。 stackoverflow.com/questions/13941631/…
  • emu8086 不支持 FPU?如果 emu8086 支持 FPU,FSQRT 会做你想做的事。

标签: assembly x86-16


【解决方案1】:

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。正如DXSI 的副本开头以及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 来保存值,并在第一次除法之前将其设置为零。第二次除法,使用16位除法,保证商不会溢出。
猜你喜欢
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
相关资源
最近更新 更多