
RoundSqrtE
By:
Zeda on
Jan 4th, 2012 | syntax:
Z80 Assembler | size: 1.60 KB | hits: 100 | expires: Never
;===============================================================
sqrtE:
;===============================================================
;Input:
; E is the value to find the square root of
;Outputs:
; A is E-D^2
; B is 0
; D is the rounded result
; E is not changed
; HL is not changed
;Destroys:
; C
;
xor a ;1 4 4
ld d,a ;1 4 4
ld c,a ;1 4 4
ld b,4 ;2 7 7
sqrtELoop:
rlc d ;2 8 32
ld c,d ;1 4 16
scf ;1 4 16
rl c ;2 8 32
rlc e ;2 8 32
rla ;1 4 16
rlc e ;2 8 32
rla ;1 4 16
cp c ;1 4 16
jr c,$+4 ;4 12|15 48+3x
inc d ;-- -- --
sub c ;-- -- --
djnz sqrtELoop ;2 13|8 47
cp d ;1 4 4
jr c,$+3 ;3 12|11 12|11
inc d ;-- -- --
ret ;1 10 10
;===============================================================
;Size : 29 bytes
;Speed : 347+3x cycles plus 1 if rounded down
; x is the number of set bits in the result.
;===============================================================