Arduino, C++ and ASM .S file usage - Page 1
Forum section: electronics / general

author: sasa
author_rank: Regular Contributor
timestamp: September 13, 2017, 06:59:43 am
content: 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.htmlUsing 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:void ASM_foo(byte *in_data, byte *out_data);...byte *in_data, *out_data;...ASM_foo(in_data, out_data);Code: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.

author: rstofer
author_rank: Super Contributor
timestamp: September 13, 2017, 06:25:54 pm
content: 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.

author: sasa
author_rank: Regular Contributor
timestamp: September 13, 2017, 08:00:43 pm
content: Quote from: rstofer on September 13, 2017, 06:25:54 pmThis 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.pdfLearning about 100 instructions and a bit more about AVR architecture is not an issue.

author: andersm
author_rank: Super Contributor
timestamp: September 13, 2017, 08:34:35 pm
content: The ABI is described in theGCC wiki.

author: sasa
author_rank: Regular Contributor
timestamp: September 13, 2017, 09:30:02 pm
content: Quote from: andersm on September 13, 2017, 08:34:35 pmThat is exactly what I was looking for.Thank you very much, Anders!

author: NivagSwerdna
author_rank: Super Contributor
timestamp: September 13, 2017, 09:48:59 pm
content: There was a nice series of videos on YouTube relating to this...https://youtu.be/8yAOTUY9t10

author: westfw
author_rank: Super Contributor
timestamp: September 14, 2017, 07:53:12 am
content: There's a bunch of info with the avr-libc documentation:http://www.nongnu.org/avr-libc/user-manual/pages.htmlEspecially 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?)