当前位置:网站首页>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 .
边栏推荐
- 2021-11-26 pyautogui cooperates with lightning simulator to realize the automation of mobile app check-in and answer questions
- QTSS常数
- Fs5383a lithium battery 3.7V input power supply solar lawn lamp drive IC
- Qtss data type
- Thermal resistance PT100 cu50 isolation converter to 4-20mA analog output temperature transmitter 0-10V
- 计算几何(4.17)
- 带透明png转换成c数组
- 嵌入式C语言volatile作用
- 2022 robocom world robot developer competition - undergraduate group (provincial competition)
- 数学基础课2_欧拉函数,线性筛,扩欧
猜你喜欢
[detailed tutorial installation] [configuration] auxiliary plug-ins about eslint in vscode
Introduction to Darwin streaming server
面试复习第N次
设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
自动补全 & (自定义)拼音分词器 & 搜索时注意事项
2022/07/09 第五小组 丁帅 学习笔记 day02
Proportional valve amplifier 1a, 2a, 3a, 5A proportional valve drive module 0-10V to 0-24v
处理中文分词 ik分词器以及拓展和停止字典
Material and application circuit diagram of 0-10V, 4-20mA current voltage to PWM isolation converter
4-20mA to 4-20mA 0-5V to 0-5V analog signal isolation transmitter
随机推荐
QTSS常数
Chrome browser settings [display the translation language icon in the upper right corner]
[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....
MySQL Workbench基本使用【创建一个数据库】
Qtss data type
5V boost charging 8.4v chip
[BJOI2019] 排兵布阵(分组背包)
【力扣】对称二叉树
解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...
Baby Ehab Partitions Again(dp,构造,位运算)
BusyBox 1.21.1 有udpsvd功能 可以编译成功 不干涉本机busybox方法
Decorate Apple Tree
MEX and Increments
Fs5383a lithium battery 3.7V input power supply solar lawn lamp drive IC
MySQL workbench basically uses [create a data table]
2021-11-26 pyautogui cooperates with lightning simulator to realize the automation of mobile app check-in and answer questions
[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning
Acwing game 57 (AK)
开源在线的MarkDown编辑器 --【Editor.md】
Acwing game 59 (AK)