Advertisement
Zeda

SqrtE

Jan 4th, 2012
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;===============================================================
  2. sqrtE:
  3. ;===============================================================
  4. ;Input:
  5. ;     E is the value to find the square root of
  6. ;Outputs:
  7. ;     A is E-D^2
  8. ;     B is 0
  9. ;     D is the result
  10. ;     E is not changed
  11. ;     HL is not changed
  12. ;Destroys:
  13. ;     C=2D+1 if D is even, 2D-1 if D is odd
  14.  
  15.         xor a               ;1      4         4
  16.         ld d,a              ;1      4         4
  17.         ld c,a              ;1      4         4
  18.         ld b,4              ;2      7         7
  19. sqrtELoop:
  20.         rlc d               ;2      8        32
  21.         ld c,d              ;1      4        16
  22.         scf                 ;1      4        16
  23.         rl c                ;2      8        32
  24.  
  25.         rlc e               ;2      8        32
  26.         rla                 ;1      4        16
  27.         rlc e               ;2      8        32
  28.         rla                 ;1      4        16
  29.  
  30.         cp c                ;1      4        16
  31.         jr c,$+4            ;4    12|15      48+3x
  32.           inc d             ;--    --        --
  33.           sub c             ;--    --        --
  34.         djnz sqrtELoop      ;2    13|8       47
  35.         ret                 ;1     10        10
  36. ;===============================================================
  37. ;Size  : 25 bytes
  38. ;Speed : 332+3x cycles
  39. ;   x is the number of set bits in the result. This will not
  40. ;   exceed 4, so the range for cycles is 332 to 344. To put this
  41. ;   into perspective, under the slowest conditions (4 set bits
  42. ;   in the result at 6MHz), this can execute over 18000 times
  43. ;   in a second.
  44. ;===============================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement