;Subroutine to divide by 10 using the binary fraction equivalent of 0.1. ;Argument: 16-bit dividend in DDHI,DDLO. ;Returns: 16-bit quotient in QTHI,QTLO. METHOD1 CLRF QTHI ; Clear quotient accumulator CLRF QTLO ; MOVLW B'00011001' ; 1/10 = binary .0001100110011001 recurring MOVWF DRHI ; ...loaded into divisor registers MOVLW B'10011001' ; MOVWF DRLO ; MOVLW 16 ; Initialise loop counter (16 gives max accuracy) MOVWF COUNT ; SHIFT BCF STATUS,C ; RRF DDHI,F ; Halve the dividend RRF DDLO,F ; RLF DRLO,F ; Shift the divisor RLF DRHI,F ; ...so the left-most bit goes into carry BTFSC STATUS,C ; Was the left-most bit a '1'? CALL ADDIT ; Yes, so accumulate the partial dividend DECF COUNT,F ; Bump the loop count BTFSC STATUS,Z ; Is it zero? RETURN ; Yes. Done. GOTO SHIFT ; No. Loop for next divisor bit RETURN ;Subroutine to accumulate partial results. Called by METHOD1. ;Argument: Current partial result in DDHI,DDLO. ;Returns: Accumulated quotient in QTHI,QTLO. ADDIT MOVF DDLO,W ; Get the partial result LSB ADDWF QTLO,F ; Add it to the quotient LSB BTFSC STATUS,C ; Carry generated? INCF QTHI,F ; Yes. Bump quotient MSB MOVF DDHI,W ; No. Get the partial result MSB ADDWF QTHI,F ; Add it to the quotient MSB RETURN ; Return ;Subroutine to divide by 10 by repeated subtraction of 10. ;Argument: 16-bit dividend in DDHI,DDLO. ;Returns: 16-bit quotient in QHI,QLO, remainder in DDLO. METHOD2 CLRF QHI ; Clear quotient registers CLRF QLO ; MOVLW 10 ; W = divisor STEP SUBWF DDLO,F ; Subtract 10 from dividend LSB BTFSC STATUS,C ; Carry? GOTO BUMP ; No. Jump MOVF DDHI,F ; Yes. Is dividend MSB 0? BTFSC STATUS,Z ; GOTO DONE ; Yes. Done DECF DDHI,F ; No. Decrement dividend MSB BUMP INCF QLO,F ; Bump quotient LSB BTFSC STATUS,Z ; Is it 0? INCF QHI,F ; Yes. Increment quotient MSB GOTO STEP ; Loop until dividend is <0 DONE ADDWF DDLO,F ; Correct the dividend LSB RETURN
|file: /Techref/microchip/math/div/16byconst10-at.htm, 2KB, , updated: 2011/12/20 08:41, local time: 2023/12/7 12:49,
|©2023 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?|
<A HREF="http://www.massmind.org/techref/microchip/math/div/16byconst10-at.htm"> Divide 16 bits by the constant value 10 by Alec_t</A>
|Did you find what you needed?|
Welcome to massmind.org!
Welcome to www.massmind.org!