Advertisement
Zeda

RoundSqrtE

Jan 4th, 2012
390
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 rounded result
  10. ;     E is not changed
  11. ;     HL is not changed
  12. ;Destroys:
  13. ;     C
  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.         cp d                ;1      4         4
  36.         jr c,$+3            ;3    12|11     12|11
  37.           inc d             ;--    --        --
  38.         ret                 ;1     10        10
  39. ;===============================================================
  40. ;Size  : 29 bytes
  41. ;Speed : 347+3x cycles plus 1 if rounded down
  42. ;   x is the number of set bits in the result.
  43. ;===============================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement