;; Assume ALL numbers are unsigned!!!

;; Assume R2 contains a number to be divided by 10
;; Put the Quotient in R0 and the remainder in R1
        .ORIG    x3000
div10   ADD	 R0,R2,#0           ;; R0 <- R2
        AND      R1,R1,#0           ;; R1 <-  0
        AND      R3,R3,#0           
        ADD      R3,R3,#4           ;; R3 <-  4

;; left shift R0 four bits,
;; put bits shifted out into R1
SHFT4   ADD      R1,R1,R1
        ADD      R0,R0,#0
        BRzp     noSetA
        ADD      R1,R1,#1
noSetA  ADD      R0,R0,R0
        ADD      R3,R3,#-1
        BRp      SHFT4


;; At the beinning of this loop
;;   R2 = abcd efgh ijkl mnop
;;   R1 = 0000 0000 0000 abcd
;;   R0 = efgh ijkl mnop 0000
;; As we go through the loop, we test R1
;;   If R1>10,
;;      we need to put a 1 in the quotient
;;        being formed at the end of R0,
;;      and we need to subract 10 from R1
;;   Then shift R1-R0 as a unit, placing
;;     msb of R0 into R1


;; Go through the divide loop 12 times
        ADD      R3,R3,#12          ;; R3 <- 12
;;      Testing the abcd bits, to see if abcd > 10
DIVLP   ADD      R4,R1,#-10         ;; test if bits in R1 > 10
        BRn      lt10
        ADD      R0,R0,#1           ;; if so, add 1 to R0
        ADD      R1,R4,#0           ;; and set R1 <- R1-10
lt10    ADD      R1,R1,R1           ;; shift R1 over 1 position
        ADD      R0,R0,#0           ;; move msb or R0 to lsb of R1
        Brzp     noSetB
        ADD      R1,R1,#1
noSetB  ADD      R0,R0,R0           ;; shift R0 over 1 position
        ADD      R3,R3,#-1
        BRp      DIVLP

;; Need to make one more adjustment ...
        ADD      R4,R1,#-10
        BRn      DONE
        ADD      R0,R0,#1
        ADD      R1,R4,#0
        
DONE    HALT
	
	.END
