Full Version : Warren's Divide-by-Three Routine (PIC ASM)
avr >>PIC 8051 ZILOG ARM TI H8 ETC >>Warren's Divide-by-Three Routine (PIC ASM)


Admin3- 04-18-2006

An algorithm from Andy Warren for dividing a positive value by three, by knowing that divide by three can be represented by the series:

x/3 = x/2 - x/4 + x/8 - x/16 + x/32 - x/64...

The code to implement this is:

CODE

Div3:                    ;  Divide Contents of "w" by 3

 movwf  Dividend
 clrf   Quotient

Div3_Loop                ;  Loop Until the Dividend == 0

 bcf    STATUS, C
 rrf    Dividend, f     ;  Dividend /2 (ie "x/2" in Series)
 movf   Dividend, w     ;   Is it Equal to Zero?
 btfsc  STATUS, Z
  goto  Div3_Done       ;  If it is, then Stop

 addwf  Quotient        ;  Add the Value to the Quotient

 rrf    Dividend, f     ;  Dividend /2 (ie "x/4" in Series)
 movf   Dividend, w
 btfsc  STATUS, Z
  goto  Div3_Done

 subwf  Quotient, f     ;  Quotient = Quotient-(Dividend / 4)

 goto   Div3_Loop

Div3_Done

 movf   Quotient, w     ;  Return the Quotient

 return



Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.