Advertisement
Zeda

SqrtHL

Jan 4th, 2012
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;===============================================================
  2. ;*As a note, "8 cycle routine" means that the algorithm runs
  3. ;8 times. After that, cycle refers to clock cycles.
  4. ;
  5. ;This routine was written for the Z80 processor on 30 May 2011.
  6. ;This is a simple, fast, 8 cycle routine to find the square root
  7. ;of hl. If you would like an explanation of the algorithm, or
  8. ;if you have any other questions, you can contact me at:
  9. ;     xedaelnara@gmail.com
  10. ;
  11. ;This is a simple derivation of how I learned it by hand and is
  12. ;much easier in binary :)
  13. ;
  14. ;There are plenty of ways to vary this such as returning a
  15. ;rounded result, returning the z flag set if it is a perfect
  16. ;square, returning the result in bc, returning cubed roots, et
  17. ;cetera.
  18. ;
  19. ;To use this, copy this to the program that needs it and then
  20. ;use "call SqrtHL" as needed :)
  21. ;===============================================================
  22.  
  23. SqrtHL:
  24. ;===============================================================
  25. ;Inputs:
  26. ;     hl is the value to find the square root of
  27. ;Outputs:
  28. ;     a is the square root (the integer part)
  29. ;     bc is not changed
  30. ;     de is not changed
  31. ;     hl is 0
  32. ;     interrupts are turned off
  33. ;Shadow Registers:
  34. ;     a' is not changed
  35. ;     b' is 0
  36. ;     c' is not changed
  37. ;     h'l' is the remainder (the original value minus a^2)
  38. ;Destroys:
  39. ;     d'e'
  40. ;===============================================
  41. ;mnemonic                size  cycles    total
  42. ;===============================================
  43.      di                  ;1      4         4
  44.      exx                 ;1      4         4
  45.      xor a               ;1      4         4
  46.      ld h,a              ;1      4         4
  47.      ld l,a              ;1      4         4
  48.      ld b,8              ;2      7         7
  49. sqrtHLLoop:                                  
  50.        rlca              ;1      4        32
  51.        ld d,0            ;2      7        56
  52.        ld e,a            ;1      4        32
  53.        ex de,hl          ;1      4        32
  54.        add hl,hl         ;1     11        88
  55.        inc l             ;1      4        32
  56.        ex de,hl          ;1      4        32
  57.                                            
  58.        exx               ;1      4        32
  59.        add hl,hl         ;1     11        88
  60.        exx               ;1      4        32
  61.        adc hl,hl         ;2     15       120
  62.                                            
  63.        exx               ;1      4        32
  64.        add hl,hl         ;1     11        88
  65.        exx               ;1      4        32
  66.        adc hl,hl         ;2     15       120
  67.                                            
  68.        sbc hl,de         ;2     15       120
  69.        jr c,$+5          ;5    12|23     96+11x
  70.          inc a           ;--    --        --
  71.          jr $+3          ;--    --        --
  72.        add hl,de         ;1     11        88
  73.        djnz sqrtHLLoop   ;2    13|8       99
  74.      exx                 ;1      4         4
  75.      ret                 ;1     10        10
  76. ;
  77. ;Size  : 37 bytes
  78. ;Speed : 1292+11x cycles
  79. ;    x is the number of bits in the result, so speed ranges from
  80. ;    1292 cycles to 1380. To put this into perspective, at the
  81. ;    slowest setting (6MHz), the calculator can run this over
  82. ;    4500 times in a second
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement