home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj8601.zip / BARR.JAN next >
Text File  |  1986-01-31  |  2KB  |  63 lines

  1. cseg    segment    para public 'CODE'
  2.  
  3.     org    100h
  4.  
  5.     assume    cs:cseg,ds:cseg,es:cseg,ss:cseg
  6.  
  7. main    proc    near
  8.     nop
  9.     call sqrt 
  10.     jmp    main
  11. main    endp
  12.  
  13.  
  14. ; Michael Barr's 32-bit Square Root Routine
  15. ; Call with CX:BX = argument
  16. ; Returns   BX    = result
  17. ;
  18.  
  19. sqrt      proc      near           ;CX:BX = argument
  20.           push      ax             ;save other registers
  21.           push      dx
  22.           push      di
  23.           jcxz      sqrt3
  24.           mov       dx,cx          ;prepare first try
  25.           mov       di,-1
  26. sqrt1:    shl       dx,1           ;estimating size of arg.
  27.           jc        sqrt2          ;to guess initial try
  28.           shl       dx,1
  29.           jc        sqrt2
  30.           shr       di,1
  31.           jmp       sqrt1
  32. sqrt2:    mov       dx,cx          ;restore argument
  33.           mov       ax,bx
  34.           cmp       dx,di          ;prevent overflow
  35.           jae       sqrt4
  36.           div       di
  37.           cmp       ax,di          ;comp quotient and divisor
  38.           jae       sqrt4
  39.           add       di,ax          ;average them
  40.           rcr       di,1
  41.           jmp       sqrt2          ;and do it again
  42. sqrt3:    mov       dx,bx          ;prepare first try
  43.           mov       di,0ffh
  44.           or        bx,bx          ;lower half zero?
  45.           jnz       sqrt1          ;no, jump
  46.           mov       di,bx
  47. sqrt4:    mov       bx,di          ;return result in BX
  48.           pop       di
  49.           pop       dx
  50.           pop       ax
  51.           ret
  52. sqrt      endp
  53.  
  54.  
  55. cseg    ends
  56.  
  57.     end    main
  58. 
  59. cseg    ends
  60.  
  61.     end    main
  62.