4.3 C
New York
Friday, December 9, 2022

Programming embedded systems: Modules, recursion, and the Procedure Call Standard – Embedded

Advertisement

Advertisement
Embedded.com
Advertisement
In the last lesson, you learned how to define a function in C, and you also saw a declaration of a function. But the purpose of the declaration wasn’t entirely clear. In this lesson, you will see how the distinction between the declaration and the definition allows you to split the program into multiple modules — one of the most fundamental features of C. You’ll also learn about the ARM Application Procedure Call Standard (AAPCS).
Lesson 9 – Modules, recursion, and the Procedure Call Standard

Source Files Are for Definitions
Source files, with the extension.c, contain the function definitions (the function code). A source file can also contain definitions of variables. There should be only one definition of any entity (function or variable) in the program.
Header Files Are for Declarations
Header files, with the extension.h, provide declarations that introduce names and type information to the compiler. For example, a function declaration (a.k.a. function prototype) tells the compiler about the function name, the exact type of parameters it takes, and what type the function returns.
Protection Against Multiple Inclusion
Header files should generally protect themselves against multiple inclusion, which can happen when header files include other header files. The most common protection shown in the video is based on the C preprocessor and the #ifndef guards. However, some programmers prefer using #pragma once instead. What do you use? Please post a comment.
Logical vs. Physical Design
Most writings on C only teach logical design, such as partitioning a program into functions. But that does not address questions such as where to place the function declarations and definitions. These issues are the subject of physical design, which is partitioning the code into directories, header files, and source files, as well as organizational issues, such as compile-time and link-time dependencies among the files [1].
Calling Convention
A calling convention is a set of rules that regulates and facilitates calls between separately compiled program files. In ARM processors, this convention is defined in the ARM Application Procedure Call Standard (AAPCS). The video for this lesson explains the most important aspects of AAPCS, such as which CPU registers must be preserved and which can be clobbered in a function call.
End Notes
In most processors, a calling convention is just a convention of allocating CPU registers and the stack to pass parameters into the function and receive return values out of the function. For example, the x86 CPU can work with several calling conventions, sometimes mixed in the same program. But ARM Cortex-M is different because interrupt handlers in Cortex-M can be regular C functions, which ties the AAPCS to the interrupt processing hardware. You’ll learn more about interrupts in the future lessons of this series.
Finally, I encourage you to learn more about physical design as a discipline because it seems vastly underappreciated [1]. Yet, physical design is essential in professional projects, perhaps even more so than logical design. For example, handling software product lines is a physical design issue. A good physical design would allow you to build a specific software product by selecting a set of modules at link time instead of struggling with convoluted if-else logic at runtime or #if-#else conditional logic at compile time.
[1] John Lakos, “Large Scale C++ Software Design”, Addison-Wesley; (1996)
Related Contents:
For more Embedded, subscribe to Embedded’s weekly email newsletter.

Advertisement
You must or to post a comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Advertisement
Advertisement
Advertisement

source

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles