Mixing C and assembly

Share code examples or discuss embedded software, including device drivers, interrupt handlers, middleware and application code.

Moderators: Markus Girdland, Mattias Norlander

Polyene
Posts: 2
Joined: Sat Nov 30, 2019 2:39 pm

Mixing C and assembly

Postby Polyene » Sat Nov 30, 2019 2:52 pm

Hello,

I'm quite new to the Cortex-world, but would like to learn more. I usually find that writing some assembly is a good way to learn a new architecture, and would like to start there. My goal is to port a simple co-operational multitasking system that I've made years ago for Microchip's 16-bit series to a STM32. For this I would need some information regarding the calling convention of the compiler toolchain. How are parameters transferred to functions? How are return values handled? What registers needs to be saved/restored when a function is called? Etc...

I've tried to google this, but there seems to be very little information at this level. For Microchip, all I need to do is to google "XC16 calling convention" (XC16 is their compiler toolchain for 16-bitters), and the first result has an excellent chapter called "Mixing C and Assembly Code". Is there any similar information available for the arm-atollic-eabi-gcc toolchain?

Primarily, I don't want to use inline assembly. I want to understand what goes on when a function is called, and what implications it has.

Best regards,
Lennart Hedlund

frank_ee
Posts: 112
Joined: Wed Apr 05, 2017 5:08 pm
Location: USA

Re: Mixing C and assembly

Postby frank_ee » Tue Dec 03, 2019 9:39 pm

Hello Lennart ,

If you really need to understand the ARM Cortex M4, you should read the book "The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors".
This book is not vendor specific, but you will learn a lot about ARM Cortex M4. Just for reference, the ARM Cortex M4 was designed for firmware development in C by use of the ARM CMSIS. I agree that learning by assembly is great, but with 32bit MCUs, the C\C++ compiler is more efficient.
However, ARM Cortex M4 assembly instructions still need to be read when debugging and performing certain tasks like clear leading zeros.

Hope this helps.

Polyene
Posts: 2
Joined: Sat Nov 30, 2019 2:39 pm

Re: Mixing C and assembly

Postby Polyene » Sun Dec 08, 2019 1:24 pm

Thank you for the advice, I'll look into if I can get my hands on that book for a general description of the system. I will to do most code in C, but as I said in the previous post, I plan to port my multitasking system, which surely will involve some assembly for context switching and stack manipulation.

In response to my own original question, I found the "Procedure Call Standard for the Arm Architecture" - https://developer.arm.com/docs/ihi0042/latest - which describes how a function call is done, but it is a bit generic. Is there any place where the specifics of how the GCC compiler implements this? For example, the Procedure Call Standard for the Arm Architecture specifies R9 as "The meaning of this register is defined by the platform standard.". Does that mean that it needs to be saved through a function call or not?

frank_ee
Posts: 112
Joined: Wed Apr 05, 2017 5:08 pm
Location: USA

Re: Mixing C and assembly

Postby frank_ee » Sun Dec 08, 2019 9:33 pm

Hello,

If you plan to port your custom multi-tasking code, you will definitively need to know about the ARM Cortex M4 context switching and the use of its two stack pointers. In addition, privileged and unprivileged modes.

The Atollic folder has documentation on the compiler which is a good place to start.

See this link:
https://developer.arm.com/docs/dui0553/a/the-cortex-m4-processor/exception-model/exception-entry-and-return#BABDBGGA

R9 needs to be saved and restored.


Return to “Embedded target code development discussions”

Who is online

Users browsing this forum: No registered users and 1 guest