James Ashley Hillman of Industrial Interface Research Ltd shares this code:
The archive on this page links to 2 divide 24 by 8 routines.
-PICList post "24bit by 16bit Division" (actually 24 / 8)
-PICList post "Divide 24/8 routine" ASM embedded in C
During testing I discovered that high numbers cause the wrong
answers to be calculated (eg 0xF00000 / 0xFD = wrong answer)
I wrote my own routine which is Nikolai's 24 bits by 16,
modified to divide by 8 bits, remainder is also 8 bits:
;***********************************************************
;Unsigned 24 bit by 8 bit divide routine
;
; Inputs:
; Dividend - x,x+1,x+2 (x+2 - most significant!)
; Divisor - y
; Temporary:
; Counter - counter
; Output:
; Quotient - x,x+1,x+2 (x+2 - most significant!)
; Remainder - x+3
;
; Size: 17
; Timing: 342 cycles (including call and return)
;
; This is basically Nikolai Golovchenko's 24 by 16 bit
; divide routine, with some instructions removed to
; optimise it for an 8 bit divide.
; Thanks to Nikolai for the original post.
;
; James Hillman, 2 December 2005
;***********************************************************
FXD248U:
CLRF x+3 ;remainder
MOVLW d'24'
MOVWF counter
LOOPU248
RLF x,W ;shift dividend left to move next bit to remainder
RLF x+1,F ;
RLF x+2,F ;
RLF x+3,F ;shift carry (next dividend bit) into remainder
RLF x,F ;finish shifting the dividend and save carry in x.0,
;since remainder can be 9 bit long in some cases
;This bit will also serve as the next result bit.
MOVF y,W ;substract divisor from 8-bit remainder
SUBWF x+3,F
;here we also need to take into account the 9th bit of remainder, which
;is in x.0. If we don't have a borrow after subtracting from
;8 bits of remainder, then there is no borrow regardless of 9th bit
;value. But, if we have the borrow, then that will depend on 9th bit
;value. If it is 1, then no final borrow will occur. If it is 0, borrow
;will occur. These values match the borrow flag polarity.
BTFSC STATUS,0 ;if no borrow after 8 bit subtraction
BSF x,0 ;then there is no borrow in result. Overwrite
;x.0 with 1 to indicate no borrow.
;if borrow did occur, x.0 already
;holds the final borrow value (0-borrow,
;1-no borrow)
BTFSS x,0 ;if no borrow after 9-bit subtraction
ADDWF x+3,F ;restore remainder. (w contains the value
;subtracted from it previously)
DECFSZ counter,F
GOTO LOOPU248
RETURN
| file: /techref/microchip/math/div/24by8-jah.htm, 3KB, , updated: 2006/1/26 14:03, local time: 2009/11/21 19:18,
38.107.191.100:LOG IN
|
| ©2009 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! <A HREF="http://piclist.com/techref/microchip/math/div/24by8-jah.htm"> PIC Microcontoller Math Method Divide 24 bit int by 8 bit int to 24 bit int and remainder</A> |
| Did you find what you needed? |
Watch, recieve and send I2C via serial port. 100Kb/s. Also 5 TTL out & 8 in. $15 chip $30 w/ PCB $40 kit $50 A&T |
Robotics nuts!Check out http://www.verinet.com/~dlc/ email: dlc@verinet.com... This guy ROCKS! He has made (and sells but also releases code, docs, etc...) for a number of cool little robotic modules including whiskers, IR proximity detect and remote control, Sonar proximity detect, PWM, Servo, compass. Most of these use the little PIC 12C508 controller which costs basically nothing and is soooo tiny.The 4 servos, 2400 baud serial servo controller is a wonder of magic and he sells the programmed chip for $8. Wow! |
.