home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8601.arc
/
BARR.JAN
next >
Wrap
Text File
|
1986-01-31
|
2KB
|
63 lines
cseg segment para public 'CODE'
org 100h
assume cs:cseg,ds:cseg,es:cseg,ss:cseg
main proc near
nop
call sqrt
jmp main
main endp
; Michael Barr's 32-bit Square Root Routine
;
; Call with CX:BX = argument
; Returns BX = result
;
sqrt proc near ;CX:BX = argument
push ax ;save other registers
push dx
push di
jcxz sqrt3
mov dx,cx ;prepare first try
mov di,-1
sqrt1: shl dx,1 ;estimating size of arg.
jc sqrt2 ;to guess initial try
shl dx,1
jc sqrt2
shr di,1
jmp sqrt1
sqrt2: mov dx,cx ;restore argument
mov ax,bx
cmp dx,di ;prevent overflow
jae sqrt4
div di
cmp ax,di ;comp quotient and divisor
jae sqrt4
add di,ax ;average them
rcr di,1
jmp sqrt2 ;and do it again
sqrt3: mov dx,bx ;prepare first try
mov di,0ffh
or bx,bx ;lower half zero?
jnz sqrt1 ;no, jump
mov di,bx
sqrt4: mov bx,di ;return result in BX
pop di
pop dx
pop ax
ret
sqrt endp
cseg ends
end main
cseg ends
end main