<?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 32 bit by 16 bit in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242740#M19621</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Ed,&lt;/P&gt;&lt;BLOCKQUOTE&gt;
&lt;P&gt;Ed Jiang wrote:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Thanks for sharing the code. I have a question: how can I apply your code? for example, I used it in a way as follows, but it didn't work.&lt;/P&gt;
&lt;P&gt;//////////////////////////////////////////////////////////////////////////////&lt;/P&gt;
&lt;P&gt;void UDVD32(unsigned long x, unsigned int y);&lt;/P&gt;
&lt;P&gt;//////////////////////////////////////////////////////////////////////////////&lt;/P&gt;
&lt;P&gt;signed long *sumS32;&lt;/P&gt;
&lt;P&gt;unsigned int *counterU8;&lt;/P&gt;
&lt;P&gt;signed int outTemp;&lt;/P&gt;
&lt;P&gt;UDVD32(*sumS32, *counterU8);&lt;/P&gt;
&lt;P&gt;outTemp = -(signed int)*sumS32;&lt;/P&gt;
&lt;P style="min-height: 8pt; padding: 0px;"&gt;&lt;/P&gt;
 &lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your request seems a little confused!&amp;nbsp; The original post seemed to require an assembly sub-routine for the division process - with code duly provided by Encoder.&amp;nbsp; However, your later post does imply that you actually require a C function for the same process.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The attached code provides a suite of arithmetic functions, that includes a 64-bit by 32-bit unsigned division function.&amp;nbsp; The functions actually make use of HLI assembly code within each function to optimise speed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Of course, it is possible to adapt the C function as a "pure" assembly sub-routine.&amp;nbsp; I have attempted to do this within the second attachment.&amp;nbsp; This code snippet is untested - errors are&amp;nbsp; quite possible.&amp;nbsp; As with the C functions, the ASM sub-routine makes use of the stack for intermediate data storage, and does not require use of additional memory within zero-page RAM.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;All functions and the sub-routine assume unsigned division process.&amp;nbsp; If you are working with signed quantities, you will need to separately determine the sign of the resulting quotient, and then convert the dividend and divisor to positive values (and cast to&amp;nbsp; unsigned), prior to calling the function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 01 Feb 2013 01:41:01 GMT</pubDate>
    <dc:creator>bigmac</dc:creator>
    <dc:date>2013-02-01T01:41:01Z</dc:date>
    <item>
      <title>division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242735#M19616</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone has the assembly code for dividing 32 bit by 16 bit, i.e., unsigned long x, unsigned int?&lt;/P&gt;&lt;P&gt;in Freescale intrinsic_math.asm, there is only 32 bit by 8 bit division, as follows.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Ed&lt;/P&gt;&lt;P&gt;;/*****************************************************************************&lt;/P&gt;&lt;P&gt;;*&lt;/P&gt;&lt;P&gt;;* Module: unsigned int udiv32i8to16(unsigned long x, unsigned char y)&lt;/P&gt;&lt;P&gt;;*&lt;/P&gt;&lt;P&gt;;* Description:&lt;/P&gt;&lt;P&gt;;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Unsigned integer dividing 32 bit by 8 bit from 16 bit Uword. &lt;/P&gt;&lt;P&gt;;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The Result is saturated at 0xFFFF if overflow occures.&lt;/P&gt;&lt;P&gt;;*&lt;/P&gt;&lt;P&gt;;* Returns:&amp;nbsp;&amp;nbsp; x/y&lt;/P&gt;&lt;P&gt;;* Returns:&amp;nbsp;&amp;nbsp; Out3:Out2:Out1:Out0 = (x3:x2:x1:x0)/(y0) = x/y&lt;/P&gt;&lt;P&gt;;*&lt;/P&gt;&lt;P&gt;;* Arguments: y (in) L1-&amp;gt;5,SP; L2-&amp;gt;6,SP;&lt;/P&gt;&lt;P&gt;;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; H1-&amp;gt;3,SP; H2-&amp;gt;4,SP;&lt;/P&gt;&lt;P&gt;;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y (in) A&lt;/P&gt;&lt;P&gt;;*&lt;/P&gt;&lt;P&gt;;* Range Issues: None&lt;/P&gt;&lt;P&gt;;*&lt;/P&gt;&lt;P&gt;;* Special Issues: HCS08S -Cs08 Option backend. The result is saturated&lt;/P&gt;&lt;P&gt;;*&lt;/P&gt;&lt;P&gt;;*****************************************************************************/&lt;/P&gt;&lt;P&gt;udiv32i8to16:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PSHA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TAX&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; y -&amp;gt; X&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TST 4,SP&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BNE udiv32Sat&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDA 5, SP&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PSHA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PULH&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDA 6, SP&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DIV&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BCS udiv32Sat&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PSHA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDA 8, SP&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DIV&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TAX&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;z0 -&amp;gt; X&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PULH&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;z1 -&amp;gt; H&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PULA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RTS&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;udiv32Sat:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDHX #0FFFFh&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PULA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RTS&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Jan 2013 15:16:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242735#M19616</guid>
      <dc:creator>edjiang</dc:creator>
      <dc:date>2013-01-30T15:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242736#M19617</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ed,&lt;/P&gt;&lt;P&gt;This is a solution. The routine uses some zero-page ram&amp;nbsp; for Quotient, Divisor and Result (2x 4byte) and some bytes more (3) from stack. It is all noted.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;STRONG style="font-size: 8pt;"&gt;&amp;nbsp; &lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; variables declaration (IntAcc1 &amp;amp; IntAcc2)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;Quotient:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;Remainder:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;IntAcc1: ; 4-bytes integer accum. #1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;TEMP1 ds 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;TEMP2 ds 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;Dividend:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;TEMP3 ds 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;TEMP4 ds 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;Divisor:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;IntAcc2: ; 4-bytes integer accum. #2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;TEMP5 ds 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;TEMP6 ds 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;TEMP7 ds 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;TEMP8 ds 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;**************************************************&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; 32 x 16 UNSIGNED DIVIDE&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; This routine takes the 32-bit dividend stored in&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; IntAcc1:IntAcc1+3 and divides it by the 16-bit&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; divisor stored in IntAcc2:IntAcc2+1.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; The quotient replaces the dividend and the&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; remainder replaces the divisor.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; Time length: 1720us @ 2MHz BusClock with call&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;**************************************************&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;UDVD32 pshh ; save H-reg value&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;psha&amp;nbsp;&amp;nbsp;&amp;nbsp; ; save accumulator&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;pshx&amp;nbsp;&amp;nbsp;&amp;nbsp; ; save H-reg value&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;ais #-3 ; reserve three bytes of&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; temp storage&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda #32&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta 3,SP&amp;nbsp;&amp;nbsp;&amp;nbsp; ; loop cntr for nr. shifts&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Divisor ; get divisor Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta 1,SP&amp;nbsp;&amp;nbsp;&amp;nbsp; ; put divisor Msb in&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; working storage&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Divisor+1 ; get divisor lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta 2,SP ; put divisor lsb in&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; working storage&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; Shift all four bytes of dividend 16 bits to the&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; right and clear both bytes of the temporary&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; remainder location&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;mov Dividend+1,Dividend+3 ; shift divid. lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;mov Dividend,Dividend+2&amp;nbsp;&amp;nbsp; ; shift 2nd byte&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; of dividend&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;mov Dividend-1,Dividend+1 ; shift 3rd byte&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; of dividend&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;mov Dividend-2,Dividend&amp;nbsp;&amp;nbsp; ; shift divid. Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;clr Remainder&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; zero remain. Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;clr Remainder+1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; zero remain. lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; Shift each byte of dividend and remainder one bit&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; to the left&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;ShftLP lda Remainder ;get remainder Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;rola ;shift remainder Msb into&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;carry&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;rol Dividend+3 ;shift dividend lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;rol Dividend+2 ;shift 2nd byte of dividend&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;rol Dividend+1 ;shift 3rd byte of dividend&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;rol Dividend ;shift dividend Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;rol Remainder+1 ;shift remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;rol Remainder ;shift remainder Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; Subtract both bytes of the divisor from remainder&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Remainder+1 ;get remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sub 2,SP ;subtract divisor lsb from&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta Remainder+1 ;store new remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Remainder ;get remainder Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sbc 1,SP ;subtract divisor Msb from&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;remainder msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta Remainder ;store new remainder Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Dividend+3 ;get low byte of&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;dividend/quotient&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sbc #0 ;dividend low bit holds&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;subtract carry&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta Dividend+3 ;store low byte of&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;dividend/quotient&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; Check dividend/quotient lsb. If clear, set lsb of&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; quotient to indicate successful subraction, else&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; add both bytes of divisor back to remainder&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;brclr 0,Dividend+3,SetLsb ;check for a carry&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;from subtraction and add&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;divisor to remainder if set&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Remainder+1 ;get remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;add 2,SP ;add divisor lsb to&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta Remainder+1 ;store remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Remainder ;get remainder Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;adc 1,SP ;add divisor Msb to&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;remainder Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta Remainder ;store remainder msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Dividend+3 ;get low byte of dividend&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;adc #0 ;add carry to low bit of&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;dividend&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta Dividend+3 ;store low byte of dividend&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;bra Decrmt ;do next shift and subtract&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;SetLsb bset 0,Dividend+3 ;set lsb of quotient to&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;indicate successive&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; subtraction&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;Decrmt dbnz 3,SP,ShftLP ;decrement loop counter and&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;do next shift&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; Move 32-bit dividend into IntAcc1:IntAcc1+3 and put&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; 16-bit remainder in IntAcc2:IntAcc2+1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Remainder ;get remainder msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta 1,SP ;temporarily store&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;remainder msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda Remainder+1 ;get remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta 2,SP ;temporarily store rem.r lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;mov Dividend,Quotient&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;mov Dividend+1,Quotient+1 ;shift all four&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;bytes of quotient&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;mov Dividend+2,Quotient+2 ;16 bits to the&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;;left&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;mov Dividend+3,Quotient+3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda 1,SP ;get final remainder Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta IntAcc2 ;store final remainder Msb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;lda 2,SP ;get final remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;sta IntAcc2+1 ;store final remainder lsb&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; Deallocate local storage, restore register values,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;; and return from subroutine&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;ais #3 ;deallocate temporary storage&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;pulx ;restore X-reg value&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;pula ;restore accumulator value&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;pulh ;restore X-reg value&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;rts&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorry the editor on the forum made a mess with the original formatting, but it is a minor problem: copy it on a good word editor with fixed character size (courier new). The routine is fully tested and I use it in my programs in assembler. Anyway the original text is attached in asm format.&lt;/P&gt;&lt;P&gt;Good luck,&lt;/P&gt;&lt;P&gt;Salvatore&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Jan 2013 18:12:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242736#M19617</guid>
      <dc:creator>Encoder1</dc:creator>
      <dc:date>2013-01-30T18:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242737#M19618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;here is an old Appnote for the HC08 with a 32 Bit by 16 Bit Division.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://cache.freescale.com/files/microcontrollers/doc/app_note/AN1219.pdf?fsrch=1&amp;amp;WT_TYPE=Application%20Notes&amp;amp;WT_VENDOR=FREESCALE&amp;amp;WT_FILE_FORMAT=pdf&amp;amp;WT_ASSET=Documentation&amp;amp;sr=1" style="font-size: 10pt; line-height: 1.5em;" title="http://cache.freescale.com/files/microcontrollers/doc/app_note/AN1219.pdf?fsrch=1&amp;amp;WT_TYPE=Application%20Notes&amp;amp;WT_VENDOR=FREESCALE&amp;amp;WT_FILE_FORMAT=pdf&amp;amp;WT_ASSET=Documentation&amp;amp;sr=1"&gt;http://cache.freescale.com/files/microcontrollers/doc/app_note/AN1219.pdf?fsrch=1&amp;amp;WT_TYPE=Application%20Notes&amp;amp;WT_VENDOR=…&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards Eckhard&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Jan 2013 23:17:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242737#M19618</guid>
      <dc:creator>eckhard</dc:creator>
      <dc:date>2013-01-30T23:17:58Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242738#M19619</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi &lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Salvatore&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for sharing the code. I have a question: how can I apply your code? for example, I used it in a way as follows, but it didn't work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Ed&lt;/P&gt;&lt;P&gt;//////////////////////////////////////////////////////////////////////////////&lt;/P&gt;&lt;P&gt;void UDVD32(unsigned long x, unsigned int y);&lt;/P&gt;&lt;P&gt;//////////////////////////////////////////////////////////////////////////////&lt;/P&gt;&lt;P&gt;signed long *sumS32;&lt;/P&gt;&lt;P&gt;unsigned int *counterU8;&lt;/P&gt;&lt;P&gt;signed int outTemp;&lt;/P&gt;&lt;P&gt;UDVD32(*sumS32, *counterU8);&lt;/P&gt;&lt;P&gt;outTemp = -(signed int)*sumS32;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Jan 2013 09:36:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242738#M19619</guid>
      <dc:creator>edjiang</dc:creator>
      <dc:date>2013-01-31T09:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242739#M19620</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am sorry Ed,&lt;/P&gt;&lt;P&gt;I am not able to fully understand your C code: I never studied it and use only assembler. Obviously there is no problem to use my assembler routine if correctly called by a C code, but i am not able to say how! I hope someone else can do.&lt;/P&gt;&lt;P&gt;The routine works perfectly in assembler when you fill "IntAcc1" 4 byte long buffer with the Dividend and "IntAcc2" 2 byte long buffer with the Divisor, then call UDVD32. The result will override the divisor (quotient) in IntAcc1 buffer and the remainder will override the divisor in IntAcc2. &lt;SPAN style="text-decoration: underline;"&gt;To work both IntAcc1 and IntAcc2 must reside on page zero&lt;/SPAN&gt;.&lt;/P&gt;&lt;P&gt;Please note that my routine was taken and updated from AN1219 cited by Eckhard as it is internally written on, so they are essentially the same.&lt;/P&gt;&lt;P&gt;Salvatore&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Jan 2013 22:17:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242739#M19620</guid>
      <dc:creator>Encoder1</dc:creator>
      <dc:date>2013-01-31T22:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242740#M19621</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Ed,&lt;/P&gt;&lt;BLOCKQUOTE&gt;
&lt;P&gt;Ed Jiang wrote:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Thanks for sharing the code. I have a question: how can I apply your code? for example, I used it in a way as follows, but it didn't work.&lt;/P&gt;
&lt;P&gt;//////////////////////////////////////////////////////////////////////////////&lt;/P&gt;
&lt;P&gt;void UDVD32(unsigned long x, unsigned int y);&lt;/P&gt;
&lt;P&gt;//////////////////////////////////////////////////////////////////////////////&lt;/P&gt;
&lt;P&gt;signed long *sumS32;&lt;/P&gt;
&lt;P&gt;unsigned int *counterU8;&lt;/P&gt;
&lt;P&gt;signed int outTemp;&lt;/P&gt;
&lt;P&gt;UDVD32(*sumS32, *counterU8);&lt;/P&gt;
&lt;P&gt;outTemp = -(signed int)*sumS32;&lt;/P&gt;
&lt;P style="min-height: 8pt; padding: 0px;"&gt;&lt;/P&gt;
 &lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your request seems a little confused!&amp;nbsp; The original post seemed to require an assembly sub-routine for the division process - with code duly provided by Encoder.&amp;nbsp; However, your later post does imply that you actually require a C function for the same process.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The attached code provides a suite of arithmetic functions, that includes a 64-bit by 32-bit unsigned division function.&amp;nbsp; The functions actually make use of HLI assembly code within each function to optimise speed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Of course, it is possible to adapt the C function as a "pure" assembly sub-routine.&amp;nbsp; I have attempted to do this within the second attachment.&amp;nbsp; This code snippet is untested - errors are&amp;nbsp; quite possible.&amp;nbsp; As with the C functions, the ASM sub-routine makes use of the stack for intermediate data storage, and does not require use of additional memory within zero-page RAM.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;All functions and the sub-routine assume unsigned division process.&amp;nbsp; If you are working with signed quantities, you will need to separately determine the sign of the resulting quotient, and then convert the dividend and divisor to positive values (and cast to&amp;nbsp; unsigned), prior to calling the function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Feb 2013 01:41:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242740#M19621</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2013-02-01T01:41:01Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242741#M19622</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Mac,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your reply. What I am looking for is the algorithm which can perform the division and can be called easily in C code. I am doing an averaging to a signal collected by ADC module. I only concern about the quotient, in other words, I need only the quotient and skip the remainder. &lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;Therefore,&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt; it would be perfect if there is a return of the quotient value when the sub-routine (or C function) is call, f&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;or example Quotient_average = &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;UDVD32&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;(*sumS32, counterU8Loc);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;I modified my code in order to call the algorithm provided by Salvatore.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;#pragma DATA_SEG SHORT MY_ZEROPAGE&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;unsigned long IntAcc1;&lt;/P&gt;&lt;P&gt;unsigned int IntAcc2;&lt;/P&gt;&lt;P&gt;unsigned int &lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Quotient_average&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;.........&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;unsigned long *sumS32;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;unsigned int *counterU8;&lt;/P&gt;&lt;P&gt;IntAcc1 = *sumS32;&lt;/P&gt;&lt;P&gt;IntAcc2 = &lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;*counterU8;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;UDVD32;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Quotient_average = (unsigned int)IntAcc1;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;In the meantime, I compare the quotient value with&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt; the result using another algorithm 32 bit divided by 8 bit. This value should be more constant because it is an average of more samples, but it isn't. Therefore, I am sure the division of 32 bit by 16 bit still fails. Can you give me a hint how I should modified my code? &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Regards,&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Ed&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Feb 2013 14:57:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242741#M19622</guid>
      <dc:creator>edjiang</dc:creator>
      <dc:date>2013-02-01T14:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242742#M19623</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Ed,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For the division process associated with calculating an arithmetic average, you probably do not need to handle a 16-bit divisor (unless you are averaging more than 255 samples).&amp;nbsp; A 32 by 8 bit division process will suffice in most cases.&amp;nbsp; This function will execute significantly faster since the DIV instruction can be utilised within a "long division" process.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I would be aiming to make the number of samples to be averaged an exact power of two.&amp;nbsp; In this case, the division process simply involves a right shift of the data. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With 256 samples, the division process is trivial.&amp;nbsp; Simply ignore the LS byte, and read the next 16 bits.&amp;nbsp; This may be accommodated using a union between the 32-bit dividend, and a structure containing the 16-bit result.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 03 Feb 2013 06:52:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242742#M19623</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2013-02-03T06:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242743#M19624</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Mac,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That is a very good idea! I will make the sampling a power of two times. By the way, I inserted the C-function you provided, and it works pretty well. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks a lot to you and others (&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Salvatore&lt;/SPAN&gt; and Eckhard) who replied to my question!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Have a nice day!&lt;/P&gt;&lt;P&gt;Ed&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Feb 2013 09:39:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242743#M19624</guid>
      <dc:creator>edjiang</dc:creator>
      <dc:date>2013-02-04T09:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242744#M19625</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Mac,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I greatly appreciate the Math32.zip source code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I used several of the functions with expected results except for "word TBLINTPL( word *table, byte tabndx, byte intplfrac);". Was it intended for "tabndx" to specify the second of the pair of values to be used. I expect "tabndx" to specify the first (e.g. table[0]). I only get the behavior I expect if I delete the "AIX #-2 // Point to entry_1 data" instruction.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again for the many helpful posts,&lt;/P&gt;&lt;P&gt;Kevin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Mar 2013 16:25:11 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242744#M19625</guid>
      <dc:creator>KEC</dc:creator>
      <dc:date>2013-03-18T16:25:11Z</dc:date>
    </item>
    <item>
      <title>Re: division 32 bit by 16 bit</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242745#M19626</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Kevin,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;
&lt;P&gt;KEC wrote:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I used several of the functions with expected results except for "word TBLINTPL( word *table, byte tabndx, byte intplfrac);". Was it intended for "tabndx" to specify the second of the pair of values to be used. I expect "tabndx" to specify the first (e.g. table[0]). I only get the behavior I expect if I delete the "AIX #-2 // Point to entry_1 data" instruction.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The value of tabndx was intended to reference the first table value.&amp;nbsp; The present code for this funtion contains a previously undiscovered bug.&amp;nbsp; I agree with your solution to the problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Mar 2013 06:56:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/division-32-bit-by-16-bit/m-p/242745#M19626</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2013-03-20T06:56:49Z</dc:date>
    </item>
  </channel>
</rss>

