Low Cost PCB's Low Cost Components

Author Topic: Arduino, C++ and ASM .S file usage  (Read 368 times)

0 Members and 1 Guest are viewing this topic.

Online sasa

  • Regular Contributor
  • *
  • Posts: 127
  • Country: 00
  • Hobbyist in electronic
Arduino, C++ and ASM .S file usage
« on: September 13, 2017, 04:59:43 PM »
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

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.
« Last Edit: September 13, 2017, 05:15:10 PM by sasa »
The 30+ years professional desktop software designer and software engineer
 

Online rstofer

  • Super Contributor
  • ***
  • Posts: 3001
  • Country: us
Re: Arduino, C++ and ASM .S file usage
« Reply #1 on: September 14, 2017, 04:25:54 AM »
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.

 

Online sasa

  • Regular Contributor
  • *
  • Posts: 127
  • Country: 00
  • Hobbyist in electronic
Re: Arduino, C++ and ASM .S file usage
« Reply #2 on: September 14, 2017, 06:00:43 AM »
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

Learning about 100 instructions and a bit more about AVR architecture is not an issue.
The 30+ years professional desktop software designer and software engineer
 

Online andersm

  • Frequent Contributor
  • **
  • Posts: 801
  • Country: fi
Re: Arduino, C++ and ASM .S file usage
« Reply #3 on: September 14, 2017, 06:34:35 AM »
The ABI is described in the GCC wiki.

Online sasa

  • Regular Contributor
  • *
  • Posts: 127
  • Country: 00
  • Hobbyist in electronic
Re: Arduino, C++ and ASM .S file usage
« Reply #4 on: September 14, 2017, 07:30:02 AM »
The ABI is described in the GCC wiki.

That is exactly what I was looking for.

Thank you very much, Anders!
The 30+ years professional desktop software designer and software engineer
 

Offline NivagSwerdna

  • Frequent Contributor
  • **
  • Posts: 858
  • Country: gb
Re: Arduino, C++ and ASM .S file usage
« Reply #5 on: September 14, 2017, 07:48:59 AM »
There was a nice series of videos on YouTube relating to this...



 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 2301
  • Country: us
Re: Arduino, C++ and ASM .S file usage
« Reply #6 on: September 14, 2017, 05:53:12 PM »
There's a bunch of info with the avr-libc documentation:
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?)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf