Programming embedded systems: Functions and the Stack

Have you witnessed your stack these days? I suggest, have you really seemed at the stack memory and observed what accurately transpires when a perform is called and afterwards returns?

Have you seen your stack recently? I mean, have you actually looked at the stack memory and observed what precisely occurs when a perform is known as and later returns? If not, you need to enjoy lesson #8 about “Functions and the Stack”:

Lesson 8 – Functions in C and the get in touch with stack
https://www.youtube.com/look at?v=Ju4KivZIL1g

The Greatest Invention Considering the fact that Sliced Bread

The operate in C (and method, subroutine, or subprogram in other programming languages) is the solitary greatest invention in laptop or computer science. It makes plans less difficult to recognize than any other characteristic of any programming language [1].

Capabilities Hold Your Code DRY

An noticeable motive for producing a function is to stay clear of copy code, also identified as the DRY (Never Repeat By yourself) theory. You do this by separating a piece of code and offering an interface that enables you to enter (simply call) it from different destinations in the system. Of program, you’ll have to have the programming language aid to build these types of an interface and the mechanisms of contacting and returning from a operate.

The Critical Purpose of the Stack

A simple get in touch with to a functionality and return would seem very simple sufficient. The return tackle can be stored in a sign up, this kind of as the LR (link sign-up) in ARM Cortex-M. But things get complicated when the called function phone calls another operate. A single LR sign-up can not “remember” two return addresses. The option is to retail store all this kind of nested return addresses in memory — in a facts structure known as the stack. The stack can keep the two return addresses and neighborhood variables utilised inside of the capabilities.

A very good metaphor for the functionality contact stack is a stack of dishes, where you can include or take out dishes only at the current leading of the stack. To guidance these kinds of a facts composition, a CPU only demands to keep in mind the present major of the stack, which in ARM Cortex-M is the position of the SP (stack pointer) sign-up.

The Purpose Overhead

The perform overhead is exceptionally very low on the ARM Cortex-M processor. The connect with alone is a single BL instruction, although the return is a solitary BX LR instruction. On leading of this comes the preparing of function parameters, which are passed in the registers R0-R3.

Close Notes

Features are critically essential, not just to stay clear of repetitions, but as the main mechanism for lessening complexity simply because you can concentrate on what is remaining carried out somewhat than how it is remaining accomplished.

Also, understanding the operate call/return mechanisms and the stack at the small stage is your gateway to other essential concepts, this sort of as interrupts, context-change in the RTOS (Serious-Time Working Program), and independent compilation.

Speaking of which, in the upcoming lesson, you will see how functions enable one more elementary function of the C language, which is the capacity to partition your code into individual documents. You will also study about the ARM Method Contact Regular. Keep tuned!

[1] Steve McConnell, “Code Complete, 2nd Ed.” Microsoft Press (July 7, 2004)


Dr. Miro M. Samek is the creator of the open resource QP actual-time
embedded frameworks and the freeware QM graphical design-based style
tool. He is also the founder and CEO of Quantum Leaps — the company of modern day embedded software package primarily based on lively objects and hierarchical state devices as nicely as resources for visible modeling, computerized code generation, and device testing of deeply embedded program. Miro teaches the popular YouTube “Modern Embedded Methods Programming” online video system on which this write-up series is dependent.

Relevant Contents:

For extra Embedded, subscribe to Embedded’s weekly email publication.



Related posts