AVR Inline Assembly - Two Operands Using the Same Registers?

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I have written some inline assembly, but the output operands (c2-0) and the input operands (b2-0) are sharing the same three registers. This particular function is a trial to do 24-bit multiplication.

This gives the wrong answer for my situation, which is unfortunate. :(

Can anyone help explain my error? I think it must be a small one. I have attached screenshots of the inline assembly, dissasembly, and a zip of the proj.

Thanks,
Justin Reina

*"=d" and "=r" gave the same result (as probably expected)

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The only strangeness I see is that you ignore half the result from the multiplication a number of times.
You should always use both r0 and r1 after a multiplication.

and if i multiply FFFFFF (24bit) with FFFFFF ( 24bit) in the windows calculator I end up with 6 bytes and not 3, so the result should be 6 byte based and not 3

even F * F gives E1.

so I think you need to rethink this multiplication from scratch and write it out before you start programming.
It is pretty straight forward and should not give to much trouble doing

regards

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
Yes multiplication always results in a value twice the digit length of your input values. That's why I picked 24-bit, the result of c=a*b should never exceed 2^24. This is ultimately an attempt at a fixed-point filter.

Does that make sense? Maybe try some multi-byte multiplication on paper... Back to the point though- why does the inline assembler use the same registers for B & C?

Thanks,
Justin

*I have put some thought into this and I am thinking the source of my problem is that I don't understand well enough how WinAVR uses the registers. We'll see though...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> I don't understand well enough how WinAVR uses the registers.

Read the inline assembler cookbook,
in particular the use of the & constraint modifier.

Btw., please don't attach plain text as an image. It requires an extra action
(right click -> view image) to be able to read it. In particular for the source
code snippet, just use a code tag inside your article.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

In particular for the source
code snippet, just use a code tag inside your article.

Jörg,

I'm guessing he was bitten by the current forum bug that won't allow a percent sign to appear in text/code

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> I'm guessing he was bitten by the current forum bug that won't
> allow a percent sign to appear in text/code

Ah, well, I forgot about that one...

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Someone should notify the admins about that so it can fixed. It's totally stupid that percent signs can't be used.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Eric,

I've PM'd them previously and there's a LOT of traffic about all the various faults in AVR Freaks forum that you'd kind of hope they wouldn't have missed. Eivind did reply to one of the threads and said that the problem was being caused by increased security levels on a new server (perhaps it simply strips out percent in all inbound traffic on the basis that's it's often used in obfuscated URLs?). There was a mysterious post in AVR Freaks forum in the last 24 hours from a "1 post" newbie who I took to be someone in Trondheim:

https://www.avrfreaks.net/index.p...

to say that they were shortly going to make some adjustments. Hope it works!

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

justinmreina wrote:
Hello,
Yes multiplication always results in a value twice the digit length of your input values. That's why I picked 24-bit, the result of c=a*b should never exceed 2^24. This is ultimately an attempt at a fixed-point filter.

Does that make sense? Maybe try some multi-byte multiplication on paper... Back to the point though- why does the inline assembler use the same registers for B & C?

So you have made 100% sure that the a and b value are never bigger then 12bit ? This means that you can skip A2 and B2 as a0/B0 + A1/B1 total up to 16 bit.....

If your A numbers are already 24bit though you need to set it up for having a 48bit result.

write out the equations to find out why in the end the b registers and c registers are the same. It might be that you think it is not logical, but when doing the math yourself find out that it is very clever, or... a bug in the compiler.

regards

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Regardless of the algorithm itself, if you want to have allocated "non-overlapping" registers, you should mark the output operands as "earlyclobbers", by adding the "&" constraint modifier (i.e. "[C2] "=&d" (c2)," etc.).

You are also supposed to restore zero in r1 by the end of your routine (add "clr r1" at the end).

On stylistics, I would also recommend you not to use tabulators.

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> [...] you should mark the output operands as "earlyclobbers",
> by adding the "&" constraint modifier

https://www.avrfreaks.net/index.p...

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#$^* I somehow overlooked that one... :-|

However, after reading the cookbook, I recommend also reading of the relevant chapters in the gcc documentation, too. Particularly the "&" constraint modifier gets a more thorough treatment there than in the cookbook.

JW