[英]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
。 由於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
,就會發生溢出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.