EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: sasa on September 13, 2017, 06:59:43 am

Title: Arduino, C++ and ASM .S file usage
Post by: sasa on September 13, 2017, 06:59:43 am
I do not really using inline ASM with Arduino software, as  GNU C/C++ basically provide quite complex and not easy to use interface. At ATMEL site is quite limited example using independent .S file:

http://www.atmel.com/webdoc/avrlibcreferencemanual/assembler_1ass_example.html (http://www.atmel.com/webdoc/avrlibcreferencemanual/assembler_1ass_example.html)

Using separate ASM code in .S file seems to me much better solution to make some calculations require low-level approach and would be much faster using target AVR (i.e. 328p or 2560) registers and flags directly. For instance:

Code: [Select]
void ASM_foo(byte *in_data, byte *out_data);
...
byte *in_data, *out_data;
...
ASM_foo(in_data, out_data);

Code: [Select]
bool ASM_foo(byte *in_data, byte *out_data);
...
byte *in_data, *out_data;
res bool;
...
res = ASM_foo(in_data, out_data);

I wonder anyone is aware or similar examples, as I have found nothing related, yet.
Title: Re: Arduino, C++ and ASM .S file usage
Post by: rstofer on September 13, 2017, 06:25:54 pm
In the early years of ARM7TDMI, the startup code was in crt.S.  The code handled the many vectors, loading RAM (.data) and clearing .bss.  It also set up the stack and enabled interrupts (if desired) before branching to main().

It is not a new idea to incorporate a number of .S files but one has to be a very good assembly language programmer to generate better code than the compiler writers.

When the Cortex variants came along, startup code could be written in C and that was a vast improvement.  Assembly language was a thing of the past.

Still, there are opportunities to tweak the code and get a little better performance.

Title: Re: Arduino, C++ and ASM .S file usage
Post by: sasa on September 13, 2017, 08:00:43 pm
When the Cortex variants came along, startup code could be written in C and that was a vast improvement.  Assembly language was a thing of the past.

Still, there are opportunities to tweak the code and get a little better performance.

This is related to small AVRs (8-bit). In time critical functions, carefully hand written ASM is sometimes the only solution.

What I missing is complete and accurate guide how exactly GNU AVR C/C++ organize parameters and ability to mix C/C++ and ASM files. I'm aware that is possible and I can find some third-party info about, but they are rather incomplete...

In the mean time, I have also found this app note and if they talk about the same C/C++ compiler, this may be enough to start:
http://www.atmel.com/images/doc42055.pdf (http://www.atmel.com/images/doc42055.pdf)

Learning about 100 instructions and a bit more about AVR architecture is not an issue.
Title: Re: Arduino, C++ and ASM .S file usage
Post by: andersm on September 13, 2017, 08:34:35 pm
The ABI is described in the GCC wiki (https://gcc.gnu.org/wiki/avr-gcc).
Title: Re: Arduino, C++ and ASM .S file usage
Post by: sasa on September 13, 2017, 09:30:02 pm
The ABI is described in the GCC wiki (https://gcc.gnu.org/wiki/avr-gcc).

That is exactly what I was looking for.

Thank you very much, Anders!
Title: Re: Arduino, C++ and ASM .S file usage
Post by: NivagSwerdna on September 13, 2017, 09:48:59 pm
There was a nice series of videos on YouTube relating to this...

https://youtu.be/8yAOTUY9t10

Title: Re: Arduino, C++ and ASM .S file usage
Post by: westfw on September 14, 2017, 07:53:12 am
There's a bunch of info with the avr-libc documentation:
http://www.nongnu.org/avr-libc/user-manual/pages.html (http://www.nongnu.org/avr-libc/user-manual/pages.html)
Especially the FAQ and the page on using assembly language programs.  (The gcc wiki is a bit better organized, though.)
avr-gcc is pretty good; the only obvious times I've wanted to use assembly language are with mixed-size math (add a byte to a long) and explicit use of the CPU flags (especially carry, when using shift or rotate algorithms.  C really sucks at doing rotates.)  Also, the prologue used for ISRs has a painfully large minimum size (it's not awful, but there are ISRs you COULD write without affecting flags or needing the "known zero" register that nevertheless save the flags and set up the zero...) (Though I think I recently saw a patch go by that changes this?)