# AVRStudio School Task - Need help with ASM

• posted

Write a program that multiply two _unsigned_ 16bit numbers and the result is 32bit number. First number is in R9:R8, second is in R11:R10 and the result is in R15:R14:R13:R12.

1st type:

First number (R9:R8) shift to right and if Carry is set add second number (R11:R10) to the result. Second number multiply with 2 in every step (shift to left)

2nd type:

use MUL

start: ror r8 ror r9 brcs add rol r10 rol r11 jmp start

• posted

On 23/11/2006 the venerable snipped-for-privacy@gmail.com etched in runes:

There are several fundamental mistakes, but you will not learn anything by having someone else write your code for you.

How do you know your code is not working? What are the symptoms? Figure out the answers to those two questions and then ask yourself why. It's called 'debugging'.

```--
John B```
• posted

I'm not asking for the complete solution. I'm asking for the help. What am I doing wrong? Please tell me what that several fundamental mistakes I made. Or point me to some docs.

Thank you!!

• posted

On 23/11/2006 the venerable snipped-for-privacy@gmail.com etched in runes:

This is Usenet, please do not top post. If you do not know what that means then look here:

1. What size is my result?
2. What size of addition do I need to do to get that result?
3. How do I do that size of addition?
4. How many additions should I do?
5. How do I stop the loop after the correct number of additions?

```--
John B```
• posted

Break down what you do on paper multiply into small parts!

345 x 123 1,1:1,0:0,0 ----------

This is the same as:

3 * 5 + 3 * (4*10) + 3 * (3*10*10) + (2*10) * 5 + (2*10) * (4*10) + (2*10) * (3*10*10) + (1*10*10) * 5 + (1*10*10) * (4*10) + (1*10*10) * (3*10*10)

You are working on 3 digit numbers, but to get the end result you have to add (among other things) the result of 100 * 300 which is 30000

30000 is a *five* digit number.

Try to figure out WHY do you shift the first number right?

Why do you shift the second number left.

You see multiply by 10 above because that is using a decimal system. In your algoritm you use a binary system. What is doing the equivalent of the multiplies by 10 in a binary system.

Your end result is a 32 bit number. In what way does your algorithm affect r15:r14?

------------------------------------------------------------

If you break down another example into a binary system you get

0x8159 1000000101011001 (second number) x 0x007B x 0000000001111011 (first number) ------------- ---------------------------- 1000000101011001 bit 0 = 1 1000000101011001 bit 1 = 1 0000000000000000 bit 2 = 0 1000000101011001 bit 3 = 1 1000000101011001 bit 4 = 1 1000000101011001 bit 5 = 1 1000000101011001 bit 6 = 1 0000000000000000 bit 7 = 0 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 + 0000000000000000 ----------------------------------------------------

The algorithm is simple, you test a bit in the first number and add the value of the second number if the bit is set, and you do not add it if the bit is clear. You do so for each bit in the first number. Testing bit n is asking yourself : Should I add the second number multiplied by 2^n.

```--
Best Regards,
Ulf Samuelsson```

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.