[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Optimisation mixes up registers in inline assembly

From: David Brown
Subject: Re: [avr-gcc-list] Optimisation mixes up registers in inline assembly
Date: Thu, 25 Jul 2019 09:31:49 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0

On 24/07/2019 23:53, Michael Kwasnicki wrote:
> Hello David. Thanks for the explanation. I intentionally reduced the
> code to not bother you with details that didn't help with pointing
> out my issue. So as a conclusion it was my misconception of how
> inline assembly works. In the full code I not only count 8 bits and
> count some bytes but also load each byte, shift each bit out and
> change the actual duty cycle according to the bit that lands in the
> carry bit. I am new to inline assembly so the first steps are not
> always useful but playful and educational. So I learn more from the
> inner workings of AVR MCUs.
> Cheers,
> Michael K.

I did not think it was the full code - after all, it's called "test" and
doesn't do anything useful.  (Though a depressingly large number of
functions called "test" end up in production code...).

Inline assembly is quite specialised.  It is very rarely needed - it
should not be needed for the code you describe here.  Unless you are
writing something like task switching code for an RTOS, any inline
assembly of more than two or three instructions is probably a poor
choice from the viewpoints of maintainability, flexibility, portability,
readability, and testability.  In many cases I have seen, it is also
bigger and slower than more careful use of C or C++ and a better
understanding of the compiler.

However, it is interesting to learn how it works, and how to use it.
And it is always a good idea to understand more about the workings of
the microcontroller, especially for a small cpu like the AVR.
Understanding the assembly and ISA of such a device is important for
getting the most out of it, even when programming in C or C++.

So my advice to you is to play around with this inline assembly and
learn.  Then for your real program, scrap it and write the code in C or C++.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]