please dont rip this site

PIC Microcontroller Basic Math Division Methods

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   .160            ; else reload W    (CF == 0)
  
    addlw   .256-.80        ; W -= 80
    rlf     result,    f    ; load cf to result bit
    btfss   result,    0    ; skip if positive (CF == 1)    
    addlw   .80             ; else reload W    (CF == 0)

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

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

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

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

    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...
+

Ihsan Volkan Tore Says: " Yes, digging into piclist, I've found that this algorithm is called kenyan method. I reinvented the wheel...again... :) " +


file: /Techref/microchip/math/div/8_bits_by_the_constant_5.htm, 4KB, , updated: 2011/1/17 23:42, local time: 2024/11/5 02:26, owner: IVT-TORETEK-TAA,
TOP NEW HELP FIND: 
3.149.239.236:LOG IN
©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!

 ©2024 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="http://techref.massmind.org/techref/microchip/math/div/8_bits_by_the_constant_5.htm"> PIC Microcontroller Basic Math Division Method s</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?

 

Welcome to massmind.org!

 
Quick, Easy and CHEAP! RCL-1 RS232 Level Converter in a DB9 backshell
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .