# PICMicrocontrollerBasicMath DivisionMethods

## Divide unsigned 8 bit integer in W by the constant value 5

```;-------------------------------------------------------------------------------
; U08DIV5	v.20110117 	By Ihsan V. TORE
;
; 8 bit unsigned integer divide by 5
; Calculated by subtracting 160:80:40:20:10: 5 from the byte successively, and
; adding result 32:16: 8: 4: 2: 1 respectively if subtractions are positive.
;
; Code : 26 instructions
; Time : 26 cycles
; Ram  :  1 bytes
;
; Args : W        : u08 : dividend
; Retv : result   : u08 : x DIV 5
;        W        : u08 : x MOD 5
;
; Note : Heavily optimized for speed.
; Lic. : MIT.
;-------------------------------------------------------------------------------
U08DIV5:
clrf    result          ; result = 0

addlw   .256-.160       ; W -= 160
rlf     result,    f    ; load cf to result bit
btfss   result,    0    ; skip if positive (CF == 1)

addlw   .256-.80        ; W -= 80
rlf     result,    f    ; load cf to result bit
btfss   result,    0    ; skip if positive (CF == 1)

addlw   .256-.40        ; W -= 40
rlf     result,    f    ; load cf to result bit
btfss   result,    0    ; skip if positive (CF == 1)

addlw   .256-.20        ; W -= 20
rlf     result,    f    ; load cf to result bit
btfss   result,    0    ; skip if positive (CF == 1)

addlw   .256-.10        ; W -= 10
rlf     result,    f    ; load cf to result bit
btfss   result,    0    ; skip if positive (CF == 1)

addlw   .256-.5         ; W -= 5
rlf     result,    f    ; load cf to result bit
btfss   result,    0    ; skip if positive (CF == 1)

return                  ; return the result

;-------------------------------------------------------------------------------
; Here is the version optimized for size if code space is more important
;-------------------------------------------------------------------------------
; U08DIV5	v.20110116 	By Ihsan V. TORE
;
; 8 bit unsigned integer divide by 5
; Calculated by subtracting 160:80:40:20:10: 5 from the byte successively, and
; adding result 32:16: 8: 4: 2: 1 respectively if subtractions are positive.
;
; Code : 12 instructions
; Time : 61 cycles
; Ram  :  2 bytes
;
; Args : dividend : u08 : dividend
; Retv : result   : u08 : x DIV 5
;	 dividend : u08 : x MOD 5
;
; Note : Heavily optimized for size.
; Lic. : MIT.
;-------------------------------------------------------------------------------
U08DIV5:
clrf	result			; prelude :)
movlw	.160			; W = subtract = 160
movwf	subtract
U08DIV5_LOOP:
subwf	dividend,	W	; W = dividend - subtract
skpnc				; skip if negative  (CF == 0)
movwf	dividend		; else dividend = W (CF == 1)
rlf	result,		F	; load CF to corresponding result bit
U08DIV5_NEXT:				; CF == 0 here always
rrf	subtract,	F	; subtract /= 2
skpnc				; if carry return
return
movfw	subtract		; W = subtract
goto	U08DIV5_LOOP		; loop

```
+

Ihsan Volkan Tore Says:

This might easily be "reinventing the wheel". I have not seen any routine like this before (probably because I did not sought enough) :). But I think this will be wellcome by all lazy coders like me when needed. Please note that, the first version without the loop can be useful for divisions by 10,20,40 and 80. You can accomplish this by returning after the reloader addlw instructions of respective subtractions. You can omit the last btfss and addlw instructions if you do not need the modulo. Cut paste run and have fun...
+
 file: /Techref/microchip/math/div/8_bits_by_the_constant_5.htm, 4KB, , updated: 2011/1/17 23:42, local time: 2023/12/7 12:57, owner: IVT-TORETEK-TAA, TOP NEW HELP FIND:  18.206.48.243:LOG IN

 ©2023 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! PIC Microcontroller Basic Math Division Method s

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"

.