testNumHi equ $1CE ; Q value high word testNumLo equ $4000 ; Q value low word QL equ 17 ; stack offset for Q lower word QH equ 15 ; stack offset for Q upper word SAR equ 7 ; stack offset for successive approximation register mask equ 5 ; stack offset for mask for performing approximation buf_lo equ 3 ; stack offset for buffer lower word buf_hi equ 1 ; stack offset for buffer upper word lp_cnt equ 0 ; stack offset for loop count locVAR equ 9 ; space for local variables org $1000 sq_root ds.w 1 org $1500 lds #$1500 ldd #testNumLo pshd ldd #testNumHi pshd jsr SqRoot32b ; compute the square root of a 32-bit number leas 4,SP ; deallcate space used by parameters std sq_root ; save the square root swi ; **************************************************************************** ; The following subroutine computes the square root of a 32-bit integer using ; the sucessive approximation method. The 32-bit number is pushed to this ; subroutine. ; **************************************************************************** SqRoot32b pshx pshy leas -locVAR,SP ; allocate local variables movb #16,lp_cnt,sp ; initialize loop count movw #$8000,mask,SP movw #0,SAR,SP ; initialize SAR to 0 sqLoop ldd mask,SP anda SAR,SP ; guess the current bit to be 1 andb SAR+1,SP ; " tfr D,Y emul ; compute the square root subd QL,SP ; compute the SAR*SAR with Q tfr Y,D ; " sbcb QH+1,SP ; " sbca QH,SP ; " bcs guessR ; guess correctly for bit i bra nextbit guessR ldd mask,SP andb SAR+1,SP ; set bit i to 1 anda SAR,SP ; " std SAR,SP nextbit ldd mask,SP ; set the bit i - 1 of mask to 1 lsrd ; " std mask,SP ; " dec lp_cnt,SP ; have we done with all 16 bits? bne sqLoop ldd SAR,SP leas locVAR,SP puly pulx rts end