home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / SQRT.AZM / SQRT.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  2.4 KB  |  82 lines

  1. ;----------------------------------------------------------------
  2. ;         This is a module in the ASMLIB library
  3. ;
  4. ;     Square Root Algorithm Uses Newton Iteration Method     
  5. ;     Based on a 10 loop pass for full 16 bit accuracy     
  6. ;     Formula based on is Newguess=(x/oldguess+oldguess)/2     
  7. ;                                  
  8. ;     Takes an Unsigned 16 Bit integer input             
  9. ;     produces a 16 bit result                  
  10. ;                                  
  11. ;         Written By Steve Sparrow             
  12. ;     Copyright (c) 1980, SME Systems Melbourne         
  13. ;                                  
  14. ;     Enter with DE = Number to Take Square Root of         
  15. ;     Exit with  HL = Result                     
  16. ;                                  
  17. ;                                  
  18. ; Modification                  Who    Date
  19. ; ------------                                  ---------------
  20. ; Modified for Z80                    S.S.    23/8/83
  21. ; To suit the ASMLIB library and rmac.        R.C.H.  27/8/83
  22. ;                                  
  23. ;----------------------------------------------------------------
  24. ;
  25.     name    'sqrt'
  26.     public    sqrt
  27.     maclib    z80
  28. ;
  29. sqrt:    xchg                ; load value into DE from HL
  30.     mvi    a,10            ; perform 10 iterations
  31.     lxi    d,1            ; smallest guess initially
  32. sqr1:    push    psw            ; save iteration count
  33.     push    h            ; save x
  34.     push    d            ; save guess1
  35.     call    div16            ; hl = x / guess
  36.     pop    d
  37.     dad    d            ; hl = x / guess + guess
  38.     ora    a
  39.     mov    a,h
  40.     rar
  41.     mov    h,a            ; shift result right (hl/2)
  42.     mov    a,l
  43.     rar
  44.     mov    l,a            ; hl = x / guess + guess
  45.     xchg                ; de gets new guess
  46.     pop    h            ; restore x
  47.     pop    psw            ; get iteration counter
  48.     dcr    a
  49.     jnz    sqr1            ; loop if not done
  50.     mov    a,e            ; e has low result
  51.     xchg                ; result in hl, remainder in de
  52.     ret
  53. ;
  54. ;****************************************************************
  55. ;*    16 By 16 Bit Unsigned Divide routine By S Sparrow    *
  56. ;*    HL=Dividend  DE=Divisor                    *
  57. ;*    Uses the Non Restoring Method during calculation    *
  58. ;*    exit with h = quotient l = remainder             *
  59. ;****************************************************************
  60. ;
  61. div16:    mov    a,h
  62.     mov    c,l            ; a and c gets dividend
  63.     mvi    b,16            ; 16 bit counter
  64.     lxi    h,0
  65. ;
  66. div16a:    ralr    c            ; left shift reg c
  67.     ral                ; shift in carry
  68.     dadc    h            ; left shift 
  69.     dsbc    d            ; trial subtraction
  70.     jrnc    div16b            ; skip if no carry
  71.     dad    d            ; restore accum
  72. div16b:    cmc                ; calculate result bit
  73.     djnz    div16a            ; loop for 16 bits
  74.     ralr    c            ; shift result left
  75.     ral
  76.     mov    h,a            ; copy result to hl
  77.     mov    l,c
  78.     ret
  79. ;
  80.     end
  81. ;
  82.