<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Division...extending bit width in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129810#M2199</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;If you actually require to do binary-to-numeric ASCII conversion, the following thread may also be of interest -&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;A href="http://forums.freescale.com/freescale/board/message?board.id=8BITCOMM&amp;amp;message.id=3288&amp;amp;query.id=8231#M3288" target="_blank"&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;A href="http://forums.freescale.com/freescale/board/message?board.id=8BITCOMM&amp;amp;message.id=3288&amp;amp;query.id=8231#M3288" target="test_blank"&gt;http://forums.freescale.com/freescale/board/message?board.id=8BITCOMM&amp;amp;message.id=3288&amp;amp;query.id=8231#M3288&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;It addresses binary-to-BCD conversion, however a numeric ASCII output is closely related.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 20 Apr 2007 09:21:51 GMT</pubDate>
    <dc:creator>bigmac</dc:creator>
    <dc:date>2007-04-20T09:21:51Z</dc:date>
    <item>
      <title>Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129802#M2191</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi All&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The Freescale Integer Math app note (AN1219) does not use the DIV instruction for extending division to 32 bit dividend/16 bit divisor, the author resorts to shift and subtract. I have not been able to find an algorithm that takes a advantage of a hardware divide instruction.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Does anyone have working code that uses DIV, or is it inherently inefficient and thus pointless?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;TIA&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Tom&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Apr 2007 22:45:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129802#M2191</guid>
      <dc:creator>DustyStew</dc:creator>
      <dc:date>2007-04-05T22:45:50Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129803#M2192</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi, Tom:&lt;BR /&gt;&lt;BR /&gt;I don't think it is that extending the DIV instruction to 32 bits is inefficient. I think it is simply not possible.&lt;BR /&gt;&lt;BR /&gt;Unlike multiplying, dividing is non-deterministic. So it can't be broken up into pieces like a multiply can. Hence, the shift/subtract algorithm that operates on the full 32 bits.&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Apr 2007 03:03:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129803#M2192</guid>
      <dc:creator>rocco</dc:creator>
      <dc:date>2007-04-06T03:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129804#M2193</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Reducing multi byte divisions to single byte division is possible.&lt;BR /&gt;The basic idea is to use the single byte division for a good estimate of the first bits of the quotient, then subtract the estimate times the dividend from the divisor and restart.&lt;BR /&gt;See Donald E. Kunth's "The Art of Computer Programming", Volume 2 for details, but Knuth really shows the concepts. Applying this to a HC08 is another story.&lt;BR /&gt;&lt;BR /&gt;For the HC08 I did not see an actual implementation and I wonder too how it compares to compare, subtract and shift. My estimates are that it is faster, but also larger and more complicated. Also the time it takes to divide would depend on the arguments, but I guess the worst case is still a bit faster.&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;BR /&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Apr 2007 20:25:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129804#M2193</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2007-04-06T20:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129805#M2194</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;OK...those two answers add up to one answer which make sense. DIV can be used in an extended divide operation, but it is one of those algorithms that converges on an answer given repeated iterations.&lt;BR /&gt;&lt;BR /&gt;My thought had been that I might be able to shift the divisor (and/or dividend) until there was a 1 in the MSB...ie skip the leading zeros to the left of the first 1 bit. And somehow that would lead to an answer. &lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt; That's about as far as my thinking got on it.&lt;BR /&gt;&lt;BR /&gt;The other algorithm I have become aware of is multiplication by the reciprocal, though I have not read the article yet and so at the moment don't know how to derive the reciprocal.&lt;BR /&gt;&lt;BR /&gt;Thanks very much for your answers, that is very useful in keeping me off of a blind alley!&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 07 Apr 2007 05:09:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129805#M2194</guid>
      <dc:creator>DustyStew</dc:creator>
      <dc:date>2007-04-07T05:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129806#M2195</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;I ran across this earlier today in one of my old projects and it jogged my memory ... someone, somewhere was asking about this. I just now remembered where it was so thought I'd stick it up here in case it's found to be helpful. It may not be exactly what the original poster was looking for, but it's at least close. Maybe it can be adapted to fit.&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;PRE&gt;;
; Divide 5 bytes at M (base page) by AccA, result to M.
;
BIGDIV PSHA  ;SAVE DIVISOR ON STACK.
 LDHX #M ;(M must be on base page)
 PSHH  ;REMAINDER ON STACK (INITIALLY 0).
BIGD040 LDA 0,X ;NEXT BYTE OF DIVIDEND.
 PULH  ;RESTORE REMAINDER.
 PSHX  ;SAVE PTR TO DVDND.
 LDX 2,SP ;THE DIVISOR.
 DIV  ;DIVIDE H:A BY DIVISOR.(H=REMAINDER)
 PULX  ;GET BACK PTR TO DIVIDEND.
 PSHH  ;SAVE REMAINDER.
 CLRH
 STA 0,X ;REPLACE DVDND BYTE WITH QUOTIENT.
 INCX  ;PTR TO NEXT BYTE OF DIVIDEND.
 CPX #M+4 ;ALL DONE?
 BLS BIGD040 ;IF NOT, LOOP.
 AIS #2 ;DISCARD REMAINDER &amp;amp; DIVISOR.
 RTS
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Apr 2007 06:50:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129806#M2195</guid>
      <dc:creator>Wings</dc:creator>
      <dc:date>2007-04-19T06:50:49Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129807#M2196</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;If the divisor is an 8-bit value, it is straight forward&amp;nbsp;to use the DIV instruction, as Wings has shown.&amp;nbsp; Effectively, a "long division" process is used, with a byte value replacing each decimal value.&amp;nbsp; As for normal decimal long division, the process may also be extended to include a fractional value to the right of the binary point.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;However,&amp;nbsp;the problem is using DIV&amp;nbsp;when a 16-bit value is required for&amp;nbsp;the divisor, and&amp;nbsp;other, more fundamental methods are used.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;An exception&amp;nbsp;could be if the divisor can be factored into two 8-bit values, i.e. (Value / D1) / D2 where D = D1*D2,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;or even ((&lt;/FONT&gt;&lt;FONT size="2"&gt;Value / D1)*M) / D2 where D&amp;nbsp;~= D1*D2 / M, and M is a relatively low integer value.&amp;nbsp; But this is likely applicable only in special cases.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Apr 2007 11:44:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129807#M2196</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-04-19T11:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129808#M2197</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Thanks for that code. Assuming I can work out how to get the remainder (it appears you have it but you are simply discarding it) I would use for it in the conversion of long numbers into ASCII. I am reckoning that the fastest conversion is...&lt;BR /&gt;&lt;BR /&gt;1. divide by 100&lt;BR /&gt;2. convert remainder to BCD with the DAA instruction&lt;BR /&gt;3. Add '0' to each nybble and put them into the text buffer&lt;BR /&gt;4. repeat til dividend is 0&lt;BR /&gt;&lt;BR /&gt;But now I am digressing. &lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Apr 2007 22:37:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129808#M2197</guid>
      <dc:creator>DustyStew</dc:creator>
      <dc:date>2007-04-19T22:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129809#M2198</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Based on Wing's code, I created a version of the divide by byte routine which uses the stack to pass the dividend and divisor. There is no pointer required, so the remainder can stay in H. No loop counter, it's inline code, so takes a few more bytes.&lt;BR /&gt;&lt;BR /&gt;I tested this, but with a different calling convention. SO I SURE HOPE THIS VERSION WORKS!&lt;BR /&gt;&lt;BR /&gt;;* NAME: divide_by_8bit&lt;BR /&gt;;* DESCRIPTION: Divide a 16/24/32 bit number by an 8 bit number&lt;BR /&gt;;* Push the dividend and divisor before calling.&lt;BR /&gt;;*&lt;BR /&gt;;* ARGS: SP+3= 8 bit divisor SP+4= dividend&lt;BR /&gt;;* RETURNS: SP+3= 8 bit remainder SP+4= quotient&lt;BR /&gt;&lt;BR /&gt;DIVIDEND_BITS EQU 32&lt;BR /&gt;&lt;BR /&gt;divide_by_8bit:&lt;BR /&gt;clrh&lt;BR /&gt;lda 4,SP ; get 1st byte of dividend&lt;BR /&gt;ldx 3,SP ; get divisor&lt;BR /&gt;div ; divide H:A by X (H=remainder)&lt;BR /&gt;sta 4,SP ; replace dividend with quotient&lt;BR /&gt;; leave remainder in H&lt;BR /&gt;lda 5,SP ; same for 2nd byte&lt;BR /&gt;ldx 3,SP&lt;BR /&gt;div&lt;BR /&gt;sta 5,SP&lt;BR /&gt;&lt;BR /&gt;IF DIVIDEND_BITS &amp;gt; 16&lt;BR /&gt;lda 6,SP ; same for 3rd byte&lt;BR /&gt;ldx 3,SP&lt;BR /&gt;div&lt;BR /&gt;sta 6,SP&lt;BR /&gt;&lt;BR /&gt;IF DIVIDEND_BITS &amp;gt; 24&lt;BR /&gt;lda 7,SP ; same for 4th byte&lt;BR /&gt;ldx 3,SP&lt;BR /&gt;div&lt;BR /&gt;sta 7,SP&lt;BR /&gt;ENDIF&lt;BR /&gt;&lt;BR /&gt;ENDIF&lt;BR /&gt;pshh&lt;BR /&gt;pula&lt;BR /&gt;sta 3,SP ; remainder replaces divisor&lt;BR /&gt;clrh&lt;BR /&gt;rts&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 20 Apr 2007 00:21:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129809#M2198</guid>
      <dc:creator>DustyStew</dc:creator>
      <dc:date>2007-04-20T00:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129810#M2199</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;If you actually require to do binary-to-numeric ASCII conversion, the following thread may also be of interest -&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;A href="http://forums.freescale.com/freescale/board/message?board.id=8BITCOMM&amp;amp;message.id=3288&amp;amp;query.id=8231#M3288" target="_blank"&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;A href="http://forums.freescale.com/freescale/board/message?board.id=8BITCOMM&amp;amp;message.id=3288&amp;amp;query.id=8231#M3288" target="test_blank"&gt;http://forums.freescale.com/freescale/board/message?board.id=8BITCOMM&amp;amp;message.id=3288&amp;amp;query.id=8231#M3288&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;It addresses binary-to-BCD conversion, however a numeric ASCII output is closely related.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 20 Apr 2007 09:21:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129810#M2199</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-04-20T09:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: Division...extending bit width</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129811#M2200</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Fixing my own code...no need to reload divisor each time, as DIV does not change X.&lt;BR /&gt;&lt;BR /&gt;divide_by_8bit:&lt;BR /&gt;clrh&lt;BR /&gt;lda 4,SP ; get 1st byte of dividend&lt;BR /&gt;ldx 3,SP ; get divisor&lt;BR /&gt;div ; divide H:A by X (H=remainder)&lt;BR /&gt;sta 4,SP ; replace dividend with quotient&lt;BR /&gt;; leave remainder in H&lt;BR /&gt;lda 5,SP ; same for 2nd byte&lt;BR /&gt;div&lt;BR /&gt;sta 5,SP&lt;BR /&gt;IF DIVIDEND_BITS &amp;gt; 16&lt;BR /&gt;lda 6,SP ; same for 3rd byte&lt;BR /&gt;div&lt;BR /&gt;sta 6,SP&lt;BR /&gt;IF DIVIDEND_BITS &amp;gt; 24&lt;BR /&gt;lda 7,SP ; same for 4th byte&lt;BR /&gt;div&lt;BR /&gt;sta 7,SP&lt;BR /&gt;ENDIF&lt;BR /&gt;ENDIF&lt;BR /&gt;pshh&lt;BR /&gt;pula&lt;BR /&gt;sta 3,SP ; remainder replaces divisor&lt;BR /&gt;clrh&lt;BR /&gt;rts&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 25 Apr 2007 14:17:56 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Division-extending-bit-width/m-p/129811#M2200</guid>
      <dc:creator>DustyStew</dc:creator>
      <dc:date>2007-04-25T14:17:56Z</dc:date>
    </item>
  </channel>
</rss>

