Read the knowledge of LR registers in one article

I don't know if you are careful in the DEBUG program, or when reading the operating system source code. The assembly instructions returned from an ordinary sub-function and returned with an exception (interrupt) are BX LR, but the meaning of the two is the same? This article will uncover the mystery of the LR register.

When the MCU involves function jumps or task scheduling, it is especially important to understand how the MCU handles the stack and change the state of each general-purpose register. Let's compare the meaning of the LR register under different conditions.

The above figure is a simple example. When the simulation clicks “Next”, the MCU will execute the BX LR instruction and return from the aa subfunction to the main function. You can see the meaning of executing BX LR in the ordinary subfunction. It is "jump to the address execution in the LR register".

Compare the source code of a ucos. If the partner who knows about the M3 kernel must know why the stack is defined like this, then we find that OS_TaskReturn is an error return function and is assigned to the R14 (LR) in the stack. position.

The task scheduling function will point the PSP stack pointer to the custom-defined stack. Then the last assembly is BX LR. Then, according to the ordinary sub-function, the meaning of BX LR is executed. At this time, will the MCU run the OS_TaskReturn function? Doesn't this go into the wrong function?

Actually, because the task scheduling of ucos using the M series is implemented in the PendSV exception (interrupt), the LR register in the exception (interrupt) is no longer the meaning of the "return address" in the ordinary function, but a definition. For a value called exc-return, when executing BX LR, this value determines whether the running level after the MCU exits the exception (interrupt) is at the privilege level or the user level and whether the stack used is MSP or PSP, and the MCU is about to Executing is the p_task function from the POP in the PSP stack, not the OS_TaskReturn function.

ZGAR Aurora 2500 Puffs

ZGAR Aurora 2500 Puffs


ZGAR electronic cigarette uses high-tech R&D, food grade disposable pod device and high-quality raw material. All package designs are Original IP. Our designer team is from Hong Kong. We have very high requirements for product quality, flavors taste and packaging design. The E-liquid is imported, materials are food grade, and assembly plant is medical-grade dust-free workshops.


Our products include disposable e-cigarettes, rechargeable e-cigarettes, rechargreable disposable vape pen, and various of flavors of cigarette cartridges. From 600puffs to 5000puffs, ZGAR bar Disposable offer high-tech R&D, E-cigarette improves battery capacity, We offer various of flavors and support customization. And printing designs can be customized. We have our own professional team and competitive quotations for any OEM or ODM works.


We supply OEM rechargeable disposable vape pen,OEM disposable electronic cigarette,ODM disposable vape pen,ODM disposable electronic cigarette,OEM/ODM vape pen e-cigarette,OEM/ODM atomizer device.

Aurora 2500 Puffs,Pod System Vape,Pos Systems Touch Screen,Empty Disposable Vape Pod System,2500Puffs Pod Vape System

ZGAR INTERNATIONAL(HK)CO., LIMITED , https://www.zgarette.com