当前位置:网站首页>Optimization of fixed number of cycles in embedded C language
Optimization of fixed number of cycles in embedded C language
2022-07-27 19:37:00 【WangLanguager】
1、 Cycle optimization
(1) Loops are the focus of code optimization
(2)C The code mainly uses for Loop or while loop
2、 A fixed number of cycles
problem : What is? ARM Write on for The most efficient way to cycle ?
example : A fixed number of cycles
int checksum_v5(int *data)
{
unsigned int i;
int sum = 0;
for(i = 0; i < 64; i++)
{
sum += *(data++);
}
return sum;
}
The assembly code of the above program is :
checksum_v5_s
MOV r2,#0 ;r2 = data
MOV r0,#0 ; sum = 0
MOV r1,#0 ; i = 0, Counter
checksum_v5_loop
LDR r3,[r2] #4 ;r3 = *(data++)
ADD r1,r1,#1 ;r1++
CMP r1,#0x40 ;compare i,64
ADD r0,r3,r0 ;sum += r3
BCC checksum_v5_loop ;if(i < 64) loop
MOV pc,r14 ;return sum
3、 Discuss :
ARM In compilation , Only two instructions are needed to implement the loop
(1) The subtraction instruction is used as a loop count , At the same time, set the result condition flag
(2) A conditional branch instruction
4、 The cycle code is modified to :
int checksum_v6(int *data)
{
unsigned int i;
int sum = 0;
for(i = 64; i != 0; i--)
{
sum += *(data++);
}
return sum;
}
checksum_v6_s
MOV r2,#0 ;r2 = data
MOV r0,#0 ; sum = 0
MOV r1,#0X40 ; i = 64, Counter
checksum_v6_loop
LDR r3,[r2] #4 ;r3 = *(data++)
SUBS r1,r1,#1 ;i-- and set flags
ADD r0,r3,r0 ;sum += r3
BNE checksum_v6_loop ;if(i != 0) loop
MOV pc,r14 ;return sum
5、 summary
The cyclic variable in the cyclic body adopts the way of self decreasing rather than the way of self increasing , Assembly code should be optimized .
边栏推荐
- c语言:12、gdb工具调试c程序
- golang设置国内镜像,vscode配置golang开发环境,vscode调试golang代码
- opds sql 里面可以用set 定义局部变量吗
- sql 字段类型转换
- [cloud picture theory] the first time to know Huawei cloud micro service engine CSE in issue 250
- Yanghui triangle
- Fzu1669 right angled triangle
- 4 轮拿下字节 Offer,面试题复盘
- sql 时间处理(SQL SERVER\ORACLE)
- 汉字查拼音微信小程序项目源码
猜你喜欢

C language: clion debugging method

MFC高级控件之Tab控件( CTabCtrl )

C language: 5. Multidimensional array

Kettle references external scripts to complete phone number cleaning, de duplication and indentation

来一遍《剑指Offer》03. 数组中重复的数字

ipfs通过接口获得公钥、私钥,并加密存储。第一弹

c语言:12、gdb工具调试c程序

Memory management A4

应用程序池已被禁用

ES6 learning notes (1) - quick start
随机推荐
C # one method returns multiple values. Suggestions collection
记一次无准备的实习面试
Debian夺回“debian.community“ 域名,喷子仍不善罢甘休
SQL Server top keyword usage
c语言:9、main函数中的return
IPFs obtains the public key and private key through the interface, and encrypts the storage. First bullet
4 轮拿下字节 Offer,面试题复盘
kettle switch / case 控件实现分类处理
Hardware acceleration of zero knowledge proof
Anaconda下安装Talib库
Debian recaptured the "debian.community" domain name, but it's still not good to stop and rest
IIS 发生未知FastCGI错误:0x80070005
HDU1323_Perfection【水题】
Webmagic+selenium+chromedriver+jdbc垂直抓取数据。
C language: 10. Input stream, output stream, error stream
X-shell remote connection virtual machine
c语言:6、指针的简单使用与注意事项
[Luogu p3175] bitwise OR (min max inclusive) (high-dimensional prefix and / FWT)
JS common utils encapsulation
C language: 15. Structure