Code:
;Find the square root and display the new frac. This uses the babylonian algorithm.
sRoot proc USES ebx, num:SWORD, den:SWORD, num2:SWORD, den2:SWORD
LOCAL dholder:SWORD ;store the past result
LOCAL nholder:SWORD ;store the past result
LOCAL counter:SDWORD
mov counter,0
invoke divFrac, num2,den2,10,1
mov num,cx
mov den,dx
invoke addFrac, 10,1,num,den
mov num,cx
mov den,dx
invoke mulFrac, 1,2,num,den
mov num,cx
mov den,dx
top:
invoke divFrac, num2,den2,num,den
mov nholder,cx
mov dholder,dx
invoke addFrac, num,den,nholder,dholder
mov nholder,cx
mov dholder,dx
invoke mulFrac, 1,2,nholder,dholder
mov num,cx
mov den,dx
inc counter
cmp counter,10
je done
jne top
done:
ret
sRoot endp
;addFrac and mulFrac accept similar value as divFrac
; divides two fractions puts numerator in ecx and denominator in edx
divFrac PROC USES eax ebx, num1:SWORD, den1:SWORD, num2:SWORD, den2:SWORD
LOCAL rNum:SDWORD ;store orignal value
LOCAL rDen:SDWORD
mov ax,0 ;set ax to 0
mov ax,num1 ;move num1 to ax register
imul ax,den2 ;sign multiply den2 to ax
movsx ecx,ax ;mov ax with sign ext (SWORD) to ecx register
mov ax,0
mov ax,num2
imul ax,den1
movsx edx,ax
mov rNum,ecx
mov rDen,edx
invoke gcd,rNum,rDen
ret ;return address
divFrac ENDP