buf equ 0 ; distance of buf from the top of the stack i equ 4 ; distance of i from the top of the stack R equ 5 ; distance of R from the top of the stack Q equ 9 ; distance of Q from the top of the stack divisor equ 21 ; distance of divisor from the top of the stack dividend equ 25 ; distance of dividend from the top of the stack quotient equ 29 ; distance of quotient from the top of the stack remainder equ 33 ; distance of remainder from the top of the stack local equ 13 ; number of bytes for local variables dvdend_hi equ $5678 ; dividend to be tested dvdend_lo equ $4c28 ; " dvsor_hi equ $20 ; divisor to be tested dvsor_lo equ $2484 ; " org $1000 quo rmb 4 ; memory locations to hold the quotient rem rmb 4 ; memory locations to hold the remainder org $1500 ; starting address of the program lds #$1500 ; initialize stack pointer leas -8,sp ; make a hole of 8 bytes to hold the result ldd #dvdend_lo pshd ldd #dvdend_hi pshd ldd #dvsor_lo pshd ldd #dvsor_hi pshd jsr div32 ; call the divide subroutine ; the following instruction deallocates the stack space used by the divisor and dividend leas 8,sp ; the following 4 instructions get the quotient from the stack puld std quo puld std quo+2 ; the following 4 instructions get the remainder from the stack puld std rem puld std rem+2 swi ; ************************************************************************************* ; The following subroutine divide an unsigned 32-bit integer by another unsigned ; 32-bit integer ; ************************************************************************************* div32 pshd pshx pshy ; the following instruction allocates space for local variables leas -local,sp ldd #0 std R,sp ; initialize register R to 0 std R+2,sp ldd dividend,sp std Q,sp ; place dividend in register Q ldd dividend+2,sp std Q+2,sp ldaa #32 staa i,sp ; initialize loop count loop lsl Q+3,sp ; shift register pair Q and R to the right rol Q+2,sp ; by 1 bit rol Q+1,sp ; " rol Q,sp ; " rol R+3,sp ; " rol R+2,sp ; " rol R+1,sp ; " rol R,sp ; " ; the following 8 instructions subtract the divisor from register R ldd R+2,sp subd divisor+2,sp std buf+2,sp ldaa R+1,sp sbca divisor+1,sp staa buf+1,sp ldaa R,sp sbca divisor,sp bcs smaller ; the following 6 instructions store the difference back to R register staa R,sp ldaa buf+1,sp staa R+1,sp ldd buf+2,sp std R+2,sp ldaa Q+3,sp oraa #01 ; set the least significant bit of Q register to 1 staa Q+3,sp bra looptest smaller ldaa Q+3,sp anda #$FE ; set the least significant bit of Q register to 0 staa Q+3,sp looptest dec i,sp lbne loop ; the following 4 instructions copy the remainder into the hole ldd R,sp std remainder,sp ldd R+2,sp std remainder+2,sp ; the following 4 instructions copy the quotient into the hole ldd Q,sp std quotient,sp ldd Q+2,sp std quotient+2,sp leas local,sp ; deallocate local variables puly pulx puld rts end