q_hi equ $F ; upper word of q q_lo equ $4240 ; lower word of q i_local equ 0 ; distance of local variable i from the stack top sum equ 2 ; distance of local variable sum from the stack top q_val equ 10 ; distance of incoming parameter q from the stack top local equ 6 ; number of bytes allocated to local variables org $1000 sq_root rmb 2 ; to hold the sqaure root of q org $1500 ldd #q_lo pshd ldd #q_hi pshd jsr findSqRoot std sq_root ; the next instruction deallocates the space used by outgoing parameters leas 4,sp swi findSqRoot pshy ; save y in the stack ; the next 3 instructions allocate space to local variables leas -local,sp ldd #0 ; initialize local variable i to 0 std i_local,sp ; " std sum,sp ; initialize local variable sum to 0 std sum+2,sp ; " loop ldd i_local,sp ldy #2 emul ; compute 2i addd sum+2,sp ; add 2i to sum std sum+2,sp ; “ tfr y,d ; “ adcb sum+1,sp ; “ stab sum+1,sp ; “ adca sum,sp ; “ staa sum,sp ; “ ; add 1 to sum (need to propagate carry to the most significant byte of sum) ldd #1 addd sum+2,sp std sum+2,sp ldaa sum+1,sp adca #0 staa sum+1,sp ldaa sum,sp adca #0 staa sum,sp ; increment i by 1 ldd i_local,sp addd #1 std i_local,sp ; compare sum to q_val by performing subtraction (need consider borrow) ldd sum+2,sp subd q_val+2,sp ldaa sum+1,sp sbca q_val+1,sp ldaa sum,sp sbca q_val,sp lblo loop ldd i_local,sp ; place sq_root in D before return ; deallocate space used by local variables exit leas local,sp puly rts end