Embedded Systems Programming
Added to favorite list
Updated On 02 Feb, 19
Getting Started - Counting - Flow of Control - Variables and Pointers - Blinking the LED - Preprocessor and volatile - Bit-wise operators in C - Arrays and Pointer Arithmetic - Functions and the Stack - Modules, Recursion, AAPCS - Stack Overflow and Other Pitfalls of Functions - stdint.h and mixing types - structures and CMSIS - startup code part - interrupts
4.1 ( 11 )
Embedded Systems Programming Lesson-8 introduces you to functions and the call stack. You will learn what a function is, how to declare it with a function prototype, how to define it, and how to call it.
The lesson starts with an observation that the blinky code written so far repeats the delay loop in two places. Such code violates the DRY (Dont Repeat Yourself) programming principle and should be avoided.
Next, you see how to turn a piece of code into a function and then call this function as many time as needed instead of repeating the code verbatim.
Next, you inspect the machine code and see that the function is called by means of the BL (Branch with Link) instruction, which stores the return address in the Link Register (LR).
In the function body, you see that the local variable counter is allocated in a special memory region called the stack. You learn about the Stack Pointer (SP) register and you learn how the stack can grow and shrink.
Next, you see how a function returns by means of the BX (Branch and Exchange) instruction. You learn about the THUMB-mode bit in the return code and you see what happens when the THUMB-bit is absent.
Next, you learn how to add an argument to the delay function, to allow you changing the delay at every invocation. You see how the function prototype ensures that every call provides the argument.
Finally, you step down to disassembly and you see that the argument is passed in the R0 register.
The next lesson will explain more about the stack and functions calling other functions, including functions calling themselves recursively. You will also learn more about function arguments as well as the non-void return types. Finally, at the low level, you will learn about the ARM Procedure Call Standard.
YouTube playlist of the course
Sep 12, 2018
Excellent course helped me understand topic that i couldn't while attendinfg my college.
March 29, 2019
Great course. Thank you very much.