x
Menu

Embedded Systems Programming

Other,

Updated On 02 Feb, 19

Overview

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

Includes

Lecture 9: Functions and the Stack

4.1 ( 11 )


Lecture Details

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.

Stay tuned...

--Miro Samek
state-machine.com



---

Resources



YouTube playlist of the course

httpwww.youtube.complaylist?list=PLPW8O6W-1chwyTzI3BHwBLbGQoPFxPAPM



Course web-page

httpwww.state-machine.comquickstart

Ratings

0


0 Ratings
55%
30%
10%
3%
2%
Comments
comment person image

Sam

Excellent course helped me understand topic that i couldn't while attendinfg my college.

Reply
comment person image

Dembe

Great course. Thank you very much.

Reply
Send