Efficiently Multiply 24-Bit Numbers Using Assembly Language: Tips & Tricks

  • Thread starter bobdole11122
  • Start date
  • Tags
    Numbers
In summary, using an assembly language, you can multiply two 16 bit numbers at a time. If you have a problem with multiplying two 8 bit numbers, you'll need to separate them into three parts and use a long hand method. There are more complicated algorithms that work with larger numbers.
  • #1
bobdole11122
4
0
I am using an assembly language for a chip that is only able to multiply two 16 bit numbers at a time. I have to multiply two 24 bit numbers and I am trying to find the best way to do this. If anyone has an algorithm to do this using basic functions such as storing and adding with carry I would appreciate it.
 
Technology news on Phys.org
  • #2
If there is a unsigned multiply that outputs a 32 bit number you can use a longhand approach:

Code:
         a       b
         c       d
         ---------
         ad     bd
  ac     bc
  ----------------
  ac  ad+bc     bd

If not, you'll need to separate each number into three 8 bit parts to use cpu's multiply and use a similar long hand method. There are more complicated algorigthms that work with large numbers.

http://www.apfloat.org
 
  • #3
long hand would work fine, but I'm not sure on the proper sequence. I have two 24-bit numbers that need to be multiplied. Do you have a chart like the one above you posted for this? So the final product would be 6-bytes.
 
  • #4
Since your chip can multiply two 16-bit numbers, I'm assuming there is a register than can store a 32-bit result. You can use the longhand approach that Jeff Reid showed. His variables a, b, c, and d are as follows in your problem:
a holds bits 23 - 16
b holds bits 15 - 0 of one multiplicand.

c holds bits 23 - 16
d holds bits 15 - 0 of the other multiplicand.

The value you get for bd will go into the low 16 bits of your result. The value you get for ac will go into the high 16 bits of your result.

The values for ad and bc need to be split up, with the high 16 bits of each being added to ac, and the low 16 bits being added to bd.

Hope that helps.
 
  • #5
I am sorry but my micro controller only has 16bit registers, two 8bit registers A and B that are concatenated to make 16bit register D, and two 16bit registers x and y.

When the multiplication operation is performed between say y and d. it stores the answer in y:d, where d is concatenated with y.

This is what is making my problem difficult for me. It seems that the previous chart would work I am just not entirely sure how it is suppose to play out. I am content in doing long multiplication but I am just confused on how exactly to handle the carry and how to end up with the final 6byte product.
 
  • #6
So how then can your MC do a multiplication of two 16-bit numbers?
bobdole11122 said:
I am using an assembly language for a chip that is only able to multiply two 16 bit numbers at a time.
Did you mean that the chip could multiply two 8-bit numbers? If that's the case, your long multiplication is going to look like
a...b...c
d...e...f
--------
and you will have 9 partial products.
 
  • #7
Going back to my example, if ac is non-zero you have an overflow condition. You end up with three 32 bit products. bd, ad, and bc. If the upper 16 bits of ad or bc are non-zero you have an overflow condition. Add the 16 bit values ad+bc, if you have a carry you have an overflow condition. Move this sum into the a register used to hold the upper 16 bits of the product. Add the upper 16 bits of the product bd to that register. Move the lower 16 bits of bd into the lower 16 bits of the product. Here is a diagram of the 3 products you sum:

Code:
|---------------|-----------------|
|  bd (upr 16)  |   bd (lwr 16)   |
|---------------|-----------------|

|---------------|-----------------|
|  ad (lwr 16)  |        0        |
|---------------|-----------------|

|---------------|-----------------|
|  bc (lwr 16)  |        0        |
|---------------|-----------------|
 
  • #8
Ok I've finally gotten it to work thanks to your help. When I start testing it the program works up to a certain value. When I try 1 mill x 1 mill it gives me the right answer but with extra digits to the left as in
Code:
[0F4240 * 0F4240]
I get
Code:
[00[COLOR="Red"]E8[/COLOR]D4A51000]
where red is the erroneous digits. I should just get
Code:
[D4A51000]
. I'm not sure if this is something you can help me with without a copy of my code, but is there some limit to the value we multiply using your methods?
 
  • #9
bobdole11122 said:
When I try 1 mill x 1 mill it gives me
E8D4A51000
Which is the correct answer, but one that doesn't fit in a 24 bit number. This an overflow case.
 

Related to Efficiently Multiply 24-Bit Numbers Using Assembly Language: Tips & Tricks

What is the process for multiplying numbers?

The process for multiplying numbers involves multiplying the digits in each place value and then adding the products together. For example, if you are multiplying 432 and 57, you would multiply 7 by 432, then 5 by 432 shifted one place to the left, and finally 4 by 432 shifted two places to the left. Then, you would add these products together to get the final answer of 24,624.

What is the difference between multiplying whole numbers and multiplying decimals?

Multiplying whole numbers involves multiplying digits in each place value, while multiplying decimals involves multiplying digits in the same place value and then shifting the decimal point in the product. For example, if you are multiplying 3.2 and 4.5, you would multiply 5 by 2, then 4 by 2 shifted one place to the left, and finally 3 by 2 shifted two places to the left. The final answer would be 14.4.

How do you handle multiplying negative numbers?

To multiply negative numbers, you can treat the negative sign as a negative one and follow the same rules for multiplying whole numbers or decimals. However, if you are multiplying two negative numbers, the product will be positive. For example, -3 x -4 = 12.

What are some tips for memorizing multiplication tables?

One tip for memorizing multiplication tables is to practice regularly. You can also use mnemonic devices or visual aids to help remember the products. Another tip is to group similar numbers together, such as multiplying by 5 or 10, to make the process easier.

How can we use multiplication in everyday life?

Multiplication is used in everyday life for various tasks, such as calculating the total cost of multiple items, determining the amount of ingredients needed for a recipe, or calculating the distance traveled based on speed and time. It is also used in more advanced concepts like budgeting, investing, and problem-solving in various fields such as science and economics.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
12
Replies
397
Views
14K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top