Reply by Ulf Samuelsson November 23, 20062006-11-23
mali.bobi@gmail.com wrote:
> 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) >
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.
> 2nd type: > > use MUL > > **************************************** > This is what I wrote but it is not working good :( Please help!! > > > start: > ror r8 > ror r9 > brcs add > rol r10 > rol r11 > jmp start > > add: > add r13,r11 > add r12,r10 > jmp start
-- Best Regards, Ulf Samuelsson ulf@a-t-m-e-l.com This message is intended to be my own personal view and it may or may not be shared by my employer Atmel Nordic AB
Reply by John B November 23, 20062006-11-23
On 23/11/2006 the venerable mali.bobi@gmail.com etched in runes:

> On Nov 23, 12:51 pm, "John B" <spamj_baraclo...@blockerzetnet.co.uk> > wrote: > > 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 > > 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!! >
This is Usenet, please do not top post. If you do not know what that means then look here: http://www.caliburn.nl/topposting.html As for your code, you should read these links and then look at your code again: http://en.wikipedia.org/wiki/Multiplication_ALU http://en.wikipedia.org/wiki/Carry_flag Ask yourself the following questions: 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? When you have answered these questions correctly your code will work. -- John B
Reply by November 23, 20062006-11-23
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!!

On Nov 23, 12:51 pm, "John B" <spamj_baraclo...@blockerzetnet.co.uk>
wrote:
>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
Reply by John B November 23, 20062006-11-23
On 23/11/2006 the venerable mali.bobi@gmail.com etched in runes:

> 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 > > **************************************** > This is what I wrote but it is not working good :( Please help!! > > > start: > ror r8 > ror r9 > brcs add > rol r10 > rol r11 > jmp start > > add: > add r13,r11 > add r12,r10 > jmp start
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
Reply by November 23, 20062006-11-23
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

****************************************
This is what I wrote but it is not working good :( Please help!!


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

add:	
add r13,r11
add r12,r10
jmp start