UDIVD16
         LDA   #0
         BEQ   UDIVMD
UREMD16
         LDA   #2
UDIVMD
         STA   _RSLTI
         PLA
         STA   RETADR
         PLA
         STA   RETADR+1
         PLA
         STA   _DVSOR
         PLA
         STA   _DVSOR+1
         PLA
         STA   _DVEND
         PLA
         STA   _DVEND+1
         JSR   UDIV
         BCC   DIVOK      
DIVERR   JMP   EREXIT
DIVOK    JMP   OKEXIT
SDIVD16
         LDA   #0         
         BEQ   SDIVMD      
SREMD16
         LDA   #2         
         BNE   SDIVMD
SDIVMD
         STA   _RSLTI     ;RESULT INDEX;0=Q,2=R
         PLA
         STA   RETADR
         PLA
         STA   RETADR+1
         PLA
         STA   _DVSOR
         PLA
         STA   _DVSOR+1
         PLA
         STA   _DVEND
         PLA
         STA   _DVEND+1
         LDA   _DVEND+1
         EOR   _DVSOR+1
         STA   _SQUOT
         LDA   _DVEND+1
         STA   _SREMD
         LDA   _DVSOR+1
         BPL   CHKDE       
         LDA   #0         
         SEC
         SBC   _DVSOR
         STA   _DVSOR
         LDA   #0
         SBC   _DVSOR+1
         STA   _DVSOR+1
CHKDE
         LDA   _DVEND+1
         BPL   DODIV      
         LDA   #0         
         SEC
         SBC   _DVEND
         STA   _DVEND
         LDA   #0
         SBC   _DVEND+1
         STA   _DVEND+1
DODIV
         JSR   UDIV
         BCS   EREXIT     
         LDA   _SQUOT
         BPL   DOREM      
         LDA   #0
         SEC
         SBC   _DVEND
         STA   _DVEND
         LDA   #0
         SBC   _DVEND+1
         STA   _DVEND+1
DOREM
         LDA   _SREMD
         BPL   OKEXIT     
         LDA   #0
         SEC
         SBC   _DVEND+2
         STA   _DVEND+2
         LDA   #0
         SBC   _DVEND+3
         STA   _DVEND+3
         JMP   OKEXIT
EREXIT
         LDA   #0
         STA   _DVEND
         STA   _DVEND+1   ;QUOTIENT = 0
         STA   _DVEND+2
         STA   _DVEND+3   
         STA   RETURN
         STA   RETURN+1
         LDA   #2
         STA   RETLEN
         SEC   
         BCS   DVEXIT
OKEXIT
         CLC   
DVEXIT
         LDX   _RSLTI     ;GET INDEX TO RESULT

         LDA   _DVEND,X
         TAY
         LDA   _DVEND+1,X
         TAX
         STY   RETURN
         STX   RETURN+1
         LDA   #2
         STA   RETLEN
         LDA   RETADR+1
         PHA
         LDA   RETADR
         PHA
         RTS
UDIV
         LDA   #0
         STA   _DVEND+2
         STA   _DVEND+3
         LDA   _DVSOR
         ORA   _DVSOR+1
         BNE   OKUDIV     
         SEC
         RTS
OKUDIV
         LDX   #16        
DIVLP
         ROL   _DVEND     ;SHFT CARRY INTO BIT 0 OF DVEND
         ROL   _DVEND+1   ;WHICH WILL BE THE QUOTIENT AND
         ROL   _DVEND+2   ;SHFT DVEND AT THE SAME TIME
         ROL   _DVEND+3
CHKLT
         SEC
         LDA   _DVEND+2
         SBC   _DVSOR
         TAY   
         LDA   _DVEND+3
         SBC   _DVSOR+1   ;SUB HIGHBYTES W RES IN A
         BCC   DECCNT     
         STY   _DVEND+2   
         STA   _DVEND+3   ;VEN(1)=DVEND(1)-DVSOR
DECCNT
         DEX
         BNE   DIVLP
         ROL   _DVEND     ;SHFT IN LAST CAR FOR QUOT
         ROL   _DVEND+1
         CLC   
         RTS
_DVSOR   DS    2          
_DVEND   DS    4          
         
_SQUOT   DS    1          
_SREMD   DS    1          
_RSLTI   DS    1          
