# PICMicrocontollerMathMethod

## Divide 16 bits by the constant value 10 by Alec_t

```;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
BTFSC	STATUS,C	; Carry generated?
INCF	QTHI,F	; Yes. Bump quotient MSB
MOVF	DDHI,W	; No. Get the partial result 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: 2022/1/25 21:27, TOP NEW HELP FIND:  52.203.18.65:LOG IN

 ©2022 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?Please DO link to this page! Digg it! / MAKE! Divide 16 bits by the constant value 10 by Alec_t

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded."

 PICList 2022 contributors: o List host: MIT, Site host massmind.org, Top posters @20220125 * Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.