Square Root in 8085 Assembly
Published by Arun Isaac on
I was asked to write 8085 assembly to calculate the square root of a given number in my microprocessors and microcontrollers laboratory exam. Interestingly, I had been thinking of nothing but square root algorithms the entire day and mysteriously enough, I got just what I was thinking about. Some resonance in the universe, I guess! I pulled off a simple iterative search algorithm in the exam, but later got this much more ingenious technique from one of my classmates.
This algorithm is based on the fact that the sum of the first n odd numbers is n^2. So, we keep adding odd numbers together, and thus consequently producing 1^2, 2^2, 3^2, 4^2, etc. Then, we compare our produced squares with the given number and if they match or are reasonably close, we may conclude that we have found the square root.
main: LXI B, 0019h ;given 16 bit number
LXI D, 0000h ;n
LXI H, 0000h ;sum of first n odd numbers
loop: DAD D
DAD D
INX H
INX D
CALL compare16bit
JM loop
MOV A, E
STA 4300h
HLT
compare16bit: MOV A, H
CMP B
RNZ
MOV A, L
CMP C
RET