Re: In-line Assembler Code

Erik Christiansen <[email protected]>
Newsgroups gmane.comp.hardware.avr.gcc
Message-ID <20150318070741.GA2155@ratatosk>
On 07.03.15 17:07, Alistair Gadd wrote:
> I'm used to using separate assembler files and I'm not a great boffin
> on using in-line assembly, but I really think I need to get a collar
> on it.

Alistair, is there any reason for not using separate assembler files, as
is your custom? As clock_prescale_set() is entirely assembler, it is
ripe for linking in from a separate assembler source file, either put
through the assembler only, or avr-gcc, as preferred.

In the latest makefile I wrote for use on avr, I find I'm using:

%.o: %.c 
   $(CC) -c $(CFLAGS) -o $(OBJDIR)/$@ $<

%.o: %.s 
   $(AS) -I$(INC_DIR) $(ASFLAGS) -o $(OBJDIR)/$@ $<

%.o: %.S 
   $(CC) -c -I$(INC_DIR) -x assembler-with-cpp $(CFLAGS) -Wa,-alms=$(OBJDIR)/[email protected] -o $(OBJDIR)/$@ $<

I.e. xxx.S goes through cpp, so lots of good macros are expanded.

If register allocation is a doubt, the ABI is documented here:
http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage

I would certainly never use in-line assembler for anything other than a
desperate hack of a large C function which couldn't be fixed any other
way. In-line assembler is unreadable gibberish, and not an efficient or
adequately maintainable method for writing a complete function, IME.

Given that linking file(s) of assembler function(s) into a C program is
quite elementary, it should hardly ever be necessary to use in-line
assembler. (And in a 30 year embedded systems design/programming career,
I never found it useful to do so.)

Erik

-- 
The world is spending half a trillion dollars on fossil fuel subsidies every
year, according to a new report. The Overseas Development Institute (ODI) says
rich countries are spending seven times more supporting coal, oil and gas than
they are on helping poorer nations fight climate change.
- http://www.bbc.co.uk/news/science-environment-24833153
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.