Programming:Square Root

From CPCWiki - THE Amstrad CPC encyclopedia!
Jump to: navigation, search

Square Root of 16bit value by Ricardo Bittencourt

;Square root of 16-bit value
;In:  HL = value
;Out:  D = result (rounded down)

.Sqr16	ld de,#0040
	ld a,l
	ld l,h
	ld h,d
	or a
	ld b,8
.Sqr16_Loop
	sbc hl,de
	jr nc,Sqr16_Skip
	add hl,de
.Sqr16_Skip
	ccf
	rl d
	add a,a
	adc hl,hl
	add a,a
	adc hl,hl
	djnz Sqr16_Loop
	ret