当前位置:网站首页>Volatile function of embedded C language
Volatile function of embedded C language
2022-07-19 06:08:00 【Shining uncle】
Important examples :
tell compiler Can't do any optimization
For example, send two instructions to a certain address :
int *ip =...; // Device address
*ip = 1; // The first instruction
*ip = 2; // Second instruction
In general c In the program , This program is just assignment , The first instruction of the above program is completely unnecessary ,compiler It may be optimized :
int *ip = ...;
*ip = 2;
As a result, the first instruction was lost . But in embedded or single chip computer , The first is necessary , If you use volatile, compiler No optimization is allowed , So as to ensure the original intention of the program , This is very important for embedded .
One 、 Changeable
volatile The main definition in the dictionary is “ Changeable ”.
Variables are stored in memory , But in order to improve the running speed , Sometimes you don't get values from memory , And from cpu cache / Value in register . In embedded / Or MCU development , This can sometimes be fatal .
volatile After defining variables : Suppose you have read 、 Write two sentences , One by one volatile Variable to operate , Then the read operation of the latter item will not directly use the corresponding write operation of the previous item volatile Variable register Content , But from Memory Read the volatile The value of the variable .
As described above ( part ) The sample code is as follows :
volatile int nNum = 0; // take nNum Declare as volatile
int nSum = 0;
nNum = FunA(); // nNum New content written , Its value is cached in registers
nSum = nNum + 1; // From memory ( Not registers ) Read from nNum Value
Two 、 Non optimizable
stay C/C++ In programming language ,volatile The second feature of is “ Can't be optimized ”.
volatile Will tell the compiler , Don't take volatile Declare variables for various radical optimizations ( Even eliminate variables directly ), So as to ensure that the instructions written by the programmer in the code will be executed .
As described above ( part ) The sample code is as follows :
volatile int nNum; // take nNum Declare as volatile
nNum = 1;
printf("nNum is: %d", nNum);
In the above code , If the variable nNum There is no claim to be volatile type , Then the compiler will optimize it in the compilation process , Use constants directly “1” Replace ( After this optimization , The generated assembly code is very brief , Efficient execution ).
And when you use volatile After making the statement , The compiler will not optimize it ,nNum Variables still exist , The compiler will take the variable out of memory , Put it into the register , Then call printf() Function to print .
3、 ... and 、 In sequence
stay C/C++ In programming language ,volatile The third feature of is “ Sequential execution feature ”, That is, it can guarantee volatile The order between variables will not be optimized by the compiler .
**C/C++ The basic optimization principle of compiler :** Ensure the output of a program , There is no change before and after optimization . In order to have an in-depth understanding of this feature , Here are two variables (nNum1 and nNum2) For example ( Since it exists “ Sequential execution ”, The description object must be larger than one ), Introduce volatile Sequential execution feature of , The sample code is as follows :
int nNum1;
int nNum2;
nNum2 = nNum1 + 1; // sentence 1
nNum1 = 10; // sentence 2
In the above code : When nNum1 and nNum2 No use volatile When modifying keywords , The compiler will “ sentence 1” and “ sentence 2” Optimize the execution sequence of : I.e. execute first “ sentence 2”、 Re execution “ sentence 1”; When nNum2 Use volatile When modifying keywords , The compiler may also be right “ sentence 1” and “ sentence 2” Optimize the execution sequence of : I.e. execute first “ sentence 2”、 Re execution “ sentence 1”; When nNum1 and nNum2 All use volatile When modifying keywords , The compiler will not “ sentence 1” and “ sentence 2” Optimize the execution sequence of : I.e. execute first “ sentence 1”、 Re execution “ sentence 2”; explain : The above discussion can be verified by observing the assembly code generated by the code .
边栏推荐
- RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)
- ES聚合分析报错:“reason“ : “Text fields are not optimised for operations
- 2021-09-15
- Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;
- 谷歌浏览器不能手动修改cookies,cookie报红标红
- Golang multi project workspace construction
- Longest bracket match (linear DP)
- MEX and Increments
- Decorate Apple Tree
- Darwin reflex summary
猜你喜欢

2022/07/10 第五小组 丁帅 学习笔记 day03

Antd is not defined

QT creator flashback solution

Chrome浏览器设置 【显示右上角 翻译语言图标】

Complete scheme diagram of lth7 five pin chip fs4054 charging circuit principle

Thermal resistance PT100 cu50 isolation converter to 4-20mA analog output temperature transmitter 0-10V

Qt Creator闪退解决办法

2022/07/11 第五小组 丁帅 学习笔记 day04

面试复习第N次

【力扣】另一棵树的子树
随机推荐
3.7V lithium battery boost to 5v1a, fs2114 boost conversion chip design layout
Loadng class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb
Dac7512n analog mixed signal IC converter
Basic mathematics course 2_ Euler function, linear sieve, extended Euler
【力扣】相同的树
串口循环缓存区 简单 免初始化 不用堆、指针、分段memcpy
數學基礎課2_歐拉函數,線性篩,擴歐
解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...
js变量提升
Material and application circuit diagram of 0-10V, 4-20mA current voltage to PWM isolation converter
Qtss callback routine
MCU single chip OTP
2022/07/11 第五小组 丁帅 学习笔记 day04
MCU single chip OTP
MySQL workbench basically uses [create a data table]
2021-11-17 esp32 pin reference
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)
RestAPI实现聚合(黑马教程)
Longest bracket match (linear DP)
【力扣】二叉树的前序遍历