簡體   English   中英

如何在emu8086中繪制平方根

[英]How to draw the square root in emu8086

我正在嘗試在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

為什么兩個錯誤出現了?

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 ,就會發生溢出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM