I am writing a 8bit integer newton raphson function in MASM X8086-32bit assembly and I think I am stuck in an infinite loop. The editor I have to use for class does not send an error for infinite loops.
Anyways I am not sure where my problem is. I just started MASM a few weeks ago and am kind of lost any help with the infinite loop would be appreciated. My initial x value is defined as 1.
The function is y = 1/2(x+n/x) ===> x/2+ n/2x where n is the number in question. and x is the intitialized value and then the previous iterations y value.
mov ah, 09h
lea dx, prompt ;Prompt User
int 21h
mov ah, 01h
int 21h ;User input
sub al, 30h
mov bh, al
mov bl, x ;Loading for loop
mov al, x
iteration:
mul bl; al*bl = al
mov dl, al ; storing x^2
add al, 01h ; (x+1)^2
mul al
cmp bh, dl
jge doneCheck ; bh - dl ====> n- x^2 => 0
doneCheck:
cmp bh, al; bh-al = ? ====>n - (x+1)^2 == -int
jl done
mov al, 02h; loading 2 in ah
mul bl ; bl*al = 2(bl) = 2x = al
shr bl, 1 ; x/2 = bl
mov cl, al ; storing 2x in cl
mov ah, 0 ; clearing ah
mov ch, 0; clearing ch
mov al, bh ; moving n into ax for division prep
div cx ; ax/cl ====> n/2x ===> p =ah and q = al
add bl, ah ;so this is finally 1/2(x+(n/x)) === (x/2+n/2x) y the new value y is now stored in bl for next loop
mov al, bl ; for next loop
jmp iteration
done:
mov dl, bl; print square root
mov ah, 02h
int 21h