please dont rip this site

PIC Microcontoller Math Method

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 
	MOVLW	B'00011001'	; 1/10 = binary .0001100110011001 recurring 
	MOVWF	DRHI	; ...loaded into divisor registers
	MOVLW	B'10011001'	;
	MOVLW	16	; Initialise loop counter (16 gives max accuracy)
	RRF	DDHI,F	; Halve the dividend
	RLF	DRLO,F	; Shift the divisor 
	RLF	DRHI,F	; 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
;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
	MOVLW	10	; W = divisor
STEP	SUBWF	DDLO,F	; Subtract 10 from dividend LSB
	GOTO	BUMP	; No. Jump
	MOVF	DDHI,F	; Yes. Is dividend MSB 0?
	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

See also:

file: /Techref/microchip/math/div/16byconst10-at.htm, 2KB, , updated: 2011/12/20 08:41, local time: 2022/1/25 21:27,

 ©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!

<A HREF=""> Divide 16 bits by the constant value 10 by Alec_t</A>

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.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2022 contributors:
o List host: MIT, Site host, 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.

Welcome to!