Floating point division in pic

Thread Starter

RG23

Joined Dec 6, 2010
304
I used the following division subroutine

Rich (BB code):
LOOP501:
 
RLF AARGB1, F ;and shift in next bit of result///////AARGB1:AARGB0 16 bit dividend
RLF AARGB0, F
RLF REMB0, F
RLF count301, F ;save carry in counter////////////count301 is initialised to 16
 
MOVF count209, W ///////count209 is the 8 bit divisor
BTFSS CARRY
INCFSZ count209, W
SUBWF REMB0, W ;keep that byte in W untill we make sure about borrow
SKPNC ;if no borrow
BSF count301, 0 ;set bit 0 of counter (saved carry)
BTFSC count301, 0 ;if no borrow
GOTO UOK46LL ;jump
MOVF REMB0, W ;read high byte of remainder to W
;to not change it by next instruction
UOK46LL
MOVWF REMB0 ;store high byte of remainder
bcf CARRY ;copy bit 0 to carry
RRF count301, F ;and restore counter
DECFSZ count301, f ;decrement counter
GOTO LOOP501 ;and repeat loop if not zero
 
 
RLF AARGB1, F 
RLF AARGB0, F
RETURN
 
Last edited by a moderator:

THE_RB

Joined Feb 11, 2008
5,438
If you don't need need top speed a very simple and reliable way is by successive subtraction, (it's simpler than it sounds) you just keep subtracting the 8bit number until it's done. And count the number of subtractions.

Rich (BB code):
// example; 1000 / 20
i = 1000;
result = 0;
while(i >= 20)
{
  i -= 20;
  result ++;
}
// when it gets to here you have the result
 
Top