Shack-Hartmann wavefront sensing in general requires careful registration of the reimaged telescope primary mirror to the Shack-Hartmann mask or lenslet array. The registration requirements are particularly demanding for applications in which segmented mirrors are phased using a physical optics generalization of the Shack-Hartmann test. In such cases the registration tolerances are less than 0.1% of the diameter of the primary mirror. We present a pupil registration algorithm suitable for such high accuracy applications that is based on the one used successfully for phasing the segments of the Keck telescopes. The pupil is aligned in four degrees of freedom (translations, rotation, and magnification) by balancing the intensities of subimages formed by small subapertures that straddle the periphery of the mirror. We describe the algorithm in general terms and then in the specific context of two very different geometries: the 492 segment Thirty Meter Telescope, and the seven "segment" Giant Magellan Telescope. Through detailed simulations we explore the accuracy of the algorithm and its sensitivity to such effects as cross talk, noise/counting statistics, atmospheric scintillation, and segment reflectivity variations.