EmbeddedRelated.com
Forums

REALLY NEED HELP TO DO DIVISION in assembly

Started by Alism 2000 April 15, 2003
Hi,
I have a 16-bit variable that is needed to be multiplied by 5
(decimal) and then divided by 1000000 (decimal, too)...so it is like
multiplying by 0.000005 (decimal),,,,,,ANY IDEA HOW TO DO THAT????
I really need help...ASAP

thanx all
AA



Divide by 200,000 decimal.

> -----Original Message-----
> From: Alism 2000 [mailto:]
> Sent: Tuesday, April 15, 2003 8:55 AM
> To:
> Subject: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly > Hi,
> I have a 16-bit variable that is needed to be multiplied by 5
> (decimal) and then divided by 1000000 (decimal, too)...so it is like
> multiplying by 0.000005 (decimal),,,,,,ANY IDEA HOW TO DO THAT????
> I really need help...ASAP
>
> thanx all
> AA >
> To unsubscribe from this group, send an email to: >
>
> ">http://docs.yahoo.com/info/terms/ >


5/1000000 = 1/(2^6 x 5^5)

So, shift right six times, then divide the result by 5^5 or 3125.

If you need help with the actual division process, search the
Internet, the HC11 WebRing, or Mot's web site for sample code to do
division.

----- Original Message -----
From: Alism 2000 <>
To: <>
Sent: Tuesday, April 15, 2003 3:55 PM
Subject: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly > Hi,
> I have a 16-bit variable that is needed to be multiplied by 5
> (decimal) and then divided by 1000000 (decimal, too)...so it is like
> multiplying by 0.000005 (decimal),,,,,,ANY IDEA HOW TO DO THAT????
> I really need help...ASAP
>
> thanx all
> AA


By the way, an example of FDIV use is found in this program (in
French):

http://martial.benoit.free.fr/files/weller.txt

Look at the routine ADC_2_DEGREE to get an idea.
----- Original Message -----
From: Tony Papadimitriou <>
To: <>
Sent: Tuesday, April 15, 2003 4:23 PM
Subject: Re: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly > 5/1000000 = 1/(2^6 x 5^5)
>
> So, shift right six times, then divide the result by 5^5 or 3125.
>
> If you need help with the actual division process, search the
> Internet, the HC11 WebRing, or Mot's web site for sample code to do
> division.


In a message dated 4/15/03 8:59:33 AM Eastern Daylight Time,
writes:

> I have a 16-bit variable that is needed to be multiplied by 5
> (decimal) and then divided by 1000000 (decimal, too)...so it is like
> multiplying by 0.000005 (decimal),,,,,,ANY IDEA HOW TO DO THAT????
> I really need help...ASAP
>

Oh good. SInce you need it ASAP, you should use c... then the answer is

answer=variable*5/1000000l;


I'm a bit troubled by dividing a 16-bit variable (max contents of 65,535) by
200,000. Seems like you'll always get 0. Or am I missing something?

Karl > -----Original Message-----
> From: Thomas Sefranek [mailto:]
> Sent: Tuesday, April 15, 2003 6:13 AM
> To:
> Subject: RE: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly > Divide by 200,000 decimal.
>
> > -----Original Message-----
> > From: Alism 2000 [mailto:]
> > Sent: Tuesday, April 15, 2003 8:55 AM
> > To:
> > Subject: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly
> >
> >
> > Hi,
> > I have a 16-bit variable that is needed to be multiplied by 5
> > (decimal) and then divided by 1000000 (decimal, too)...so it is like
> > multiplying by 0.000005 (decimal),,,,,,ANY IDEA HOW TO DO THAT????
> > I really need help...ASAP
> >
> > thanx all
> > AA
> >
> >
> >
> > To unsubscribe from this group, send an email to:
> >
> >
> >
> >
> > ">http://docs.yahoo.com/info/terms/

To unsubscribe from this group, send an email to:

">http://docs.yahoo.com/info/terms/


1. Multiply by 5 is easy:
a. Check size to make sure you do not overflow
b. Multiply by 4m ie, laft shift twice
c. add to orginal number.
2. If this is an integer variable, you are goint ot have to convet to some format which tracks decimal places since if you divide a 16 bit number by 1000000 (base 10) you would have nothing left.

Regards
Dave Perreault

>
> From: "Alism 2000" <>
> Date: 2003/04/15 Tue AM 08:55:19 EDT
> To:
> Subject: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly
>
> Hi,
> I have a 16-bit variable that is needed to be multiplied by 5
> (decimal) and then divided by 1000000 (decimal, too)...so it is like
> multiplying by 0.000005 (decimal),,,,,,ANY IDEA HOW TO DO THAT????
> I really need help...ASAP
>
> thanx all
> AA >
> To unsubscribe from this group, send an email to: >
>
> ">http://docs.yahoo.com/info/terms/ >




Tony,

As you have probably gathered from all the traffic on your question about division, the bottom line is that you just can't effectively use the divide instructions when your denominator is very large and your numerator very small.

Your best bet (I think) is to shift the numerator to the left several times and then do the division.

When you get the result, you will have to remember what you did, that is, how many times did you shift to the left. Well, once you get an answer, you will have to shift to the right by the same number of times. This will take all the significant digits off the right hand end of your result and you will be right back where you started.

What I think you are going to have to do is make up your own format to save the results in a 32 bit or a 64 bit format. This will have to be some type of "do your own thing".

You may have to do two divisions. Do the original divide and then take the remainder and divide it by the original denominator. Then you will have to deal with the "upper half" of the result and the "lower half" of the result.

I believe that you are just going to have to make up your own way to store 64 bit results.

Regards,

Charlie

-----Original Message-----
From: Tony Papadimitriou [mailto:]
Sent: Tuesday, April 15, 2003 8:43 AM
To:
Subject: Re: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly By the way, an example of FDIV use is found in this program (in
French):

http://martial.benoit.free.fr/files/weller.txt

Look at the routine ADC_2_DEGREE to get an idea.
----- Original Message -----
From: Tony Papadimitriou <>
To: <>
Sent: Tuesday, April 15, 2003 4:23 PM
Subject: Re: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly > 5/1000000 = 1/(2^6 x 5^5)
>
> So, shift right six times, then divide the result by 5^5 or 3125.
>
> If you need help with the actual division process, search the
> Internet, the HC11 WebRing, or Mot's web site for sample code to do
> division.

To unsubscribe from this group, send an email to:

">http://docs.yahoo.com/info/terms/


Charles,

Actually, I was replying to a question, not making it.

However, in response to what you say, with which I agree in general
situations, for this particular example of multiplying by a known
fixed constant (5/1000000), I think by using the FDIV instruction,
it's possible to divide two 16-bit numbers where the numerator is less
than the denominator. So, if one manages to simplify the problem and
make both the numerator and the denominator small enough to fit in
16-bits (e.g., by factoring which turns 5/1000000 to 1/(2^6 x 5^5)
thus reducing the whole problem to a a couple of divisions, then
depending on the relationship between numerator and denominator, use
either the IDIV or FDIV (in this case FDIV). I quote from the FDIV
description in the HC11 reference manual:

"FDIV is equivalent to multiplying the numerator by 2^16 and then
performing a 32-bit by 16-bit integer divide. The result is
interpreted as
a binary-weighted fraction, which resulted from the division of a
16-bit
integer by a larger 16-bit integer. A result of $0001 corresponds to
0.000015, and $FFFF corresponds to 0.99998. The remainder of an
IDIV instruction can be resolved into a binary-weighted fraction by an
FDIV instruction. The remainder of an FDIV instruction can be resolved
into the next 16 bits of binary-weighted fraction by another FDIV
instruction."

Since after the 2^6 division (a shift right six times) the result can
never be greater than 2^10 (1024), and the second divisor is the
constant 5^5 (3125), we have the dividend always being larger than the
divisor and, therefore, an FDIV could be used to get a good result.
Some accuracy may be lost because of the initial right shifting which
discarted some bits, but I think it's not too significant.

If such shortcuts aren't possible, one must use a floating point
package to carry out this operation, as the hardware is incapable of
directly providing a result.
----- Original Message -----
From: Melear Charles-rdph40 <>
To: <>
Sent: Wednesday, April 16, 2003 12:31 AM
Subject: RE: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly > Tony,
>
> As you have probably gathered from all the traffic on your question
about division, the bottom line is that you just can't effectively use
the divide instructions when your denominator is very large and your
numerator very small.
>
> Your best bet (I think) is to shift the numerator to the left
several times and then do the division.
>
> When you get the result, you will have to remember what you did,
that is, how many times did you shift to the left. Well, once you get
an answer, you will have to shift to the right by the same number of
times. This will take all the significant digits off the right hand
end of your result and you will be right back where you started.
>
> What I think you are going to have to do is make up your own format
to save the results in a 32 bit or a 64 bit format. This will have to
be some type of "do your own thing".
>
> You may have to do two divisions. Do the original divide and then
take the remainder and divide it by the original denominator. Then
you will have to deal with the "upper half" of the result and the
"lower half" of the result.
>
> I believe that you are just going to have to make up your own way to
store 64 bit results.
>
> Regards,
>
> Charlie
>
> -----Original Message-----
> From: Tony Papadimitriou [mailto:]
> Sent: Tuesday, April 15, 2003 8:43 AM
> To:
> Subject: Re: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly > By the way, an example of FDIV use is found in this program (in
> French):
>
> http://martial.benoit.free.fr/files/weller.txt
>
> Look at the routine ADC_2_DEGREE to get an idea. >
> ----- Original Message -----
> From: Tony Papadimitriou <>
> To: <>
> Sent: Tuesday, April 15, 2003 4:23 PM
> Subject: Re: [m68HC11] REALLY NEED HELP TO DO DIVISION in assembly > > 5/1000000 = 1/(2^6 x 5^5)
> >
> > So, shift right six times, then divide the result by 5^5 or 3125.
> >
> > If you need help with the actual division process, search the
> > Internet, the HC11 WebRing, or Mot's web site for sample code to
do
> > division.
> >
> >