当前位置:网站首页>嵌入式C语言对次数固定的循环的优化
嵌入式C语言对次数固定的循环的优化
2022-07-27 16:51:00 【WangLanguager】
1、循环优化
(1)循环是代码优化的重点
(2)C代码主要使用for循环或者while循环
2、固定次数的循环
问题:什么是ARM上编写for循环最高效的办法?
例:固定次数的循环
int checksum_v5(int *data)
{
unsigned int i;
int sum = 0;
for(i = 0; i < 64; i++)
{
sum += *(data++);
}
return sum;
}
上述程序的汇编代码为:
checksum_v5_s
MOV r2,#0 ;r2 = data
MOV r0,#0 ; sum = 0
MOV r1,#0 ; i = 0,计数器
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、讨论:
ARM汇编中,实现循环只要两条指令就可以了
(1)减法指令用作循环计数,同时设置结果条件标志
(2)一条条件分支指令
4、循环代码修改为:
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,计数器
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、总结
循环体中的循环变量采用自减的方式要比采用自增的方式,汇编代码要优化。
边栏推荐
- kettle学习——8.2版本的资源库配置变为灰色,且没有了Connect按钮
- There is another example of repeater
- Use fastjson JSON (simple and rough version)
- Kettle consolidated record data reduction
- Browser rendering principle analysis suggestions collection
- Daily question (02): inverted string
- c语言:10、输入流,输出流,错误流
- C # one method returns multiple values. Suggestions collection
- idea优化小攻略
- Summary of APP launch in vivo application market
猜你喜欢

C language: 7. How to use C language multi source files

MySQL learning notes (1) -- variables

c语言:10、输入流,输出流,错误流

C语言案例:密码设置及登录> 明解getchar与scanf

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

c语言:15、结构体

200 lines of code quick start document database monogodb

c语言:8、makeFile编写

VIVO应用市场APP上架总结

Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
随机推荐
Webmagic+selenium+chromedriver+jdbc grabs data vertically.
内存管理A4
Debian夺回“debian.community“ 域名,喷子仍不善罢甘休
200 lines of code quick start document database monogodb
There is a problem with the time zone when the idea connects to the database. The server returns invalid timezone is red Need to set ‘serverTimezone‘ property.
kettle JVM内存设置---效果不明显
go-zero单体服务使用泛型简化注册Handler路由
2022备战秋招10W字面试小抄pdf版,附操作系统、计算机网络面试题
C language preprocessing instruction
ReferenceError: __dirname is not defined in ES module scope
Programming jump
MongoDB学习笔记(1)——安装MongoDB及其相关配置
汉字查拼音微信小程序项目源码
There is another example of repeater
Under the heat wave of Web3.0, the ecological shock of Mensa struck
SQL Server top keyword usage
Kettle switch / case control to realize classification processing
c语言:7、c语言多源码文件使用方法
Kettle learning - the repository configuration in version 8.2 is grayed out, and there is no connect button
C language: 7. How to use C language multi source files