Integer square root

An interesting calculation. Worth some words for the lessons it teaches.

This algorithm is a port of my PDP11 code from the 1970s. I had then only a vague understanding of why it worked. No less vague now. It has to do with the difference between squares being successive odd numbers?

It ported very nicely. On the '11 I used registers; on the C18 I use the stack. But the '11 had more shifts and compares.

34 bit square root

The whole block is compiled with P9 set, since all + operations will use carry.

2*3

A 3-word add with carry (a 54-bit left shift):

step

Similar to a divide step:

sqrt

go

An infinite loop that reads a 34-bit input and returns its square-root.