Page 1 of 1

Any inline assembler tutorial?

Posted: Mon Oct 16, 2023 7:53 am
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.

Re: Any inline assembler tutorial?

Posted: Mon Oct 16, 2023 9:03 am
by fpiSTM

Re: Any inline assembler tutorial?

Posted: Thu Nov 23, 2023 9:06 am
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.

Re: Any inline assembler tutorial?

Posted: Thu Nov 23, 2023 3:03 pm
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.

Re: Any inline assembler tutorial?

Posted: Thu Nov 23, 2023 3:31 pm
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.

Re: Any inline assembler tutorial?

Posted: Sun Jan 28, 2024 10:50 am
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.