Any inline assembler tutorial?

Post here first, or if you can't find a relevant section!
Post Reply
Bulek44
Posts: 16
Joined: Mon Feb 22, 2021 3:42 pm

Any inline assembler tutorial?

Post by Bulek44 »

Hello,

we would love to read more about how inline assembler can be properly used under Arduino for STM32.
For example, how to write/read from existing variables and other ways of cooperation with generic code in IDE.

Also any examples would help a lot. Are there any general tutorials that can be applied to this environment?

Thanks in advance,
regards,
Bulek.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Any inline assembler tutorial?

Post by fpiSTM »

koen
Posts: 4
Joined: Mon May 18, 2020 8:42 am

Re: Any inline assembler tutorial?

Post by koen »

I've written assembler, but try to avoid it if I can.
If what you want is optimizing c source, what I do is compile with the '-S' option.
After compilation you then have the gcc assembler output.

Code: Select all

$ arm-none-eabi-gcc -S ftoa.c
$ ls -l ftoa.*
-rw-rw-r-- 1 koen koen  9670 Nov 23 07:11 ftoa.c
-rw-rw-r-- 1 koen koen   705 Nov 23 07:11 ftoa.h
-rw-rw-r-- 1 koen koen 21531 Nov 23 09:58 ftoa.s
The file ending in .s then contains the assembler code gcc wrote.
You just tweak your c code until the assembler code gcc produces is minimal.
Tools like puncover may help.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Any inline assembler tutorial?

Post by ag123 »

it is possible to write assembly, but that c/c++ codes tend to be more readable and maintainable.
in terms of savings, you can possibly write / make smaller binaries with assembly. But for my purpose, as I'd rather simply get a chip with a more comfortable margin of sram and flash, I'd make do with c/c++ codes.

In addition, assembly tend to be far more verbose (at least in terms of lines of code) as compared to c/c++ codes.
e.g. a function like

Code: Select all

int plus(int a, int b) {  return a + b; }
can compile into a 'whole page' of assembly e.g. with gcc -S for a practical one liner.

for inline assembly, normally on mcus e.g. stm32, it is used for 'special' situations, e.g. like

Code: Select all

asm("wfi"); // wait for interrupt
asm("wfe"); // wait for event
asm("dmb"); // data memory barrier 
asm("dsb"); //data synchronization barrier
asm("isb"); //Instruction Synchronization Barrier 
otherwise, much of other codes is much easier to read and maintain in simple c/c++ codes.
For the above, there are probably intrinsics for the same rather than specifying like asm("wfi"), but that I tend to use that for 'convenience'.
https://developer.arm.com/documentation ... -functions

In terms of speed optimization, there is very little to gain on a microcontroller. I've tried some arm assembly coding, it turns out gcc's -O2 along with some other optimization features can make codes that run faster than what you naively try to optimize.

so the only reason to write assembly on an mcu is when you are trying to save the use of every byte, such as is with a processor say with 1k sram and 4k flash, most stm32 are far more generous than that. And that I'd rather go for a chip that has more generous sram and flash, then try to squeeze in those 1k sram and 4k flash constraints.
It often don't cost very much more.

And that you would need to pick up ARM assembly if you really want to learn that. simply google ARM assembly and there'd be lots of hits.
you can nevertheless learn some of that and learn to read some codes during debug, after a while many of the generated codes are literal repeats.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Any inline assembler tutorial?

Post by ag123 »

these days, for *convenience*, and with microcontrollers with generous sram and flash, some don't even bother with c/c++ and simply script away.
https://micropython.org/
https://circuitpython.org/

e.g. if you have a good library, writing to a sd card can be simply

Code: Select all

f = open("file.txt","w")
f.write(my_data)
f.flush()
f.close()
the whole stack of things beneath - SDIO, SPI interfacing, FAT filesystem, directory structures, file structures, usb interface, serial interface etc simply 'disappears' and is done with a few lines of code. of course the library can be 10s to 100s of k bytes for micropython, circuitpython, all the 'hard work' is done in the library.
MGeo
Posts: 38
Joined: Thu Dec 19, 2019 10:29 am
Answers: 2

Re: Any inline assembler tutorial?

Post by MGeo »

It' might take some work. You end up having to learn ARM assembler and gcc's arcane inline syntax as well.

https://www.ibiblio.org/gferg/ldp/GCC-I ... HOWTO.html

This tool can be handy: https://godbolt.org/. Make sure to set the correct compiler.
Post Reply

Return to “General discussion”