# PICMicrocontollerMathMethod

## Divide 16 bits by the constant value 15

From: Dmitry Kiryashov

```;high=a.b , low=c.d
movlw   0xF0
andwf   low,F   ;c.0
andwf   high,W  ;a.0

; at this point, the low nibble of `low' is zero
; W contains the high nibble of `high'

xorwf   high,W  ;a.0^a.b
; now W contains low nibble of high, namely 0x0b
;    W  ^ high
;  0xa0 ^ 0xab => 0x0b

xorwf   high,F  ;a.0
;Now the lower nibble of a is cleared.
;    W  ^ high
;  0x0b ^ 0xab => 0xa0  and this is stored back in high

xorwf   high,W  ;a.b
;Now W contains the original value of high
;    W  ^ high
;  0x0b ^ 0xa0 => 0xab

swapf   low,F   ;0.c
swapf   high,F  ;0.a

skpnc
incf    high,F  ;0.a + carry

;11 clocks/words
```

Scott Dattalo says:

BTW, you should be aware that Nik's generator is more accurate than what Dmitry and I generated. The algorithm is based on this formula:
```  1/(A+B) ~= B/A - (B/A)^2 + (B/A)^3 + ...
```

Dmitry and I computed the first two terms, Nik does all three in the generator.

The error is on the order of 1/16/16/16 = 2E-4

For example:

suppose you wanted to divide 65535 by 15. The exact answer is 4369. However, using Dmitry's code you'd get: 4350. Nik's produces: 4365 (I think).

But a slight mod will improve Dmitry's

```> > ;high=a.b , low=c.d
> >
> >         movlw   0xF0
> >         andwf   low,F   ;c.0
> >         andwf   high,W  ;a.0
> >
> >         xorwf   high,W  ;a.0^a.b
>
> >         xorwf   high,F  ;a.0
> >         xorwf   high,W  ;a.b
>
> >
> >         swapf   low,F   ;0.c
> >         swapf   high,F  ;0.a
> >
> >         addwf   low,F   ;0.c + a.b
> >         skpnc
> >         incf    high,F  ;0.a + carry

movf  high,w
skpnc
incf high,f
```

This modification will yield the result: 4366

Code:

• Warren Schroeder of CircuitED shares this code:
Here is a 9 instruction version of Dmitry's code:

swapf   low,w    ; W = d.c
andlw   15       ; W = 0.c
addwf   high,w   ; W = 0.c + a.b
movwf   low      ; W -> low byte
swapf   high,w   ; W = b.a
andlw   15       ; W = 0.a
movwf   high     ; W -> high byte
btfsc   status,c ; check carry from add
incf    high,f   ; 0.a + carry
+

 file: /Techref/microchip/math/div/16byconst15-dk.htm, 3KB, , updated: 2007/9/11 15:59, local time: 2024/3/4 03:36, TOP NEW HELP FIND:  44.220.62.183:LOG IN

 ©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! Divide 16 bits by the constant value 15

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"

.