Efficient C Programming: C Loop Structure (Part 1)

Publisher:紫菜包饭Latest update time:2021-04-07 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: Loop bodies are the focus of program design and optimization. This section will focus on the most effective way to handle for and while loops on ARM. (EEFOCUS)

14.5C Loop Structure


The loop body is the focus of program design and optimization. This section will focus on the most effective way to handle for and while loops on ARM.


14.5.1 Loop Termination


First, let's look at the following example, which shows two different loop exit conditions and the different assembly codes they generate.


The C source program is shown below.


intfact1(intn)


{


inti,fact=1;


for(i=1;i<=n;i++)


fact*=i;


return(fact);


}


intfact2(intn)


{


inti,fact=1;


for(i=n;i!=0;i--)


fact*=i;


return(fact);


}


The generated assembly code is shown below.


fact1


MOVa3,#1


MOVa2,#1


CMPa1,#1


BLT|L000020.J5.fact1|


|L000010.J4.fact1|


MULa3,a2,a3


ADDa2,a2,#1


CMPa2,a1


BLE|L000010.J4.fact1|


|L000020.J5.fact1|


MOVa1,a3


MOVpc,lr


fact2


MOVSa2,a1


MOVa1,#1


MOVEQpc,lr


|L000034.J4.fact2|


MULa1,a2,a1


SUBSa2,a2,#1


BNE|L000034.J4.fact2|


MOVpc,lr


From the generated assembly code, we can see that although the two functions implement the same function, the generated code efficiency is not exactly the same. The key here is that the loop termination condition should be countdown to zero, rather than countup to a certain value. Since the countdown result is stored in the conditional flag, the instruction to compare with zero can be omitted. At the same time, one less register can be used to store the loop termination value.


Notice


The above example uses the -O2–Otime compilation options. If the -Ospace option is used, the compilation results will be different.


For loop count value i, if i is unsigned, the loop continuation condition can be either i!=0 or i>0. Since i cannot be a negative number, these two conditions are equivalent. For a signed loop count value, it is best not to use the condition i>0 as the loop continuation condition. If i>0 is used as the loop continuation condition, the compiler will generate the following code.


SUBa2,a2,#1


CMPr1,#0


BGT|L000034.J4.fact2|


At this time, the compiler adds an extra CMP instruction, mainly to prevent the signed number i=−0x8000000. In short, no matter for signed or unsigned loop count values, i!=0 should be used as the loop end condition. For the signed number i, this is one less instruction than using i>0.


Reference address:Efficient C Programming: C Loop Structure (Part 1)

Previous article:Solution to the problem that the ARM development board cannot mount the USB disk
Next article:Development of seismic intensity meter based on IPv6 and ARM9

Recommended ReadingLatest update time:2024-11-23 11:07

Design and implementation of wireless meteorological data communication system based on ARM-Linux
Automatic weather station data collectors are generally designed based on single-chip microcomputers or PC/104 bus controllers, and have the characteristics of good compatibility with PCs, low power consumption, and compact size. However, how to design an automatic weather station data collector with powerful function
[Microcontroller]
Design and implementation of wireless meteorological data communication system based on ARM-Linux
Several key addresses when the ARM Linux kernel starts
1. Kernel boot address 1.1. Glossary ZTEXTADDR The start address of the decompressed code. There is no distinction between physical address and virtual address because the MMU is turned off at this time. This address is not necessarily the address of RAM, but can be a storage medium such as flash that supports read an
[Microcontroller]
ARM44B0x realizes signal transmitter controller
  1. Introduction   Using the embedded 32-bit microprocessor ARMS3C44B0x as the core of the controller, the designed controller will have more comprehensive functions and better performance than the processor with a single-chip microcomputer as the core.   The main function of the transmitter controller is to coll
[Microcontroller]
ARM44B0x realizes signal transmitter controller
Arm China congratulates the release of the "Kunpeng Computing Industry Development White Paper"
Mr. Wu Xiong'ang, Executive Chairman and CEO of Arm China, attended the Huawei Connect Conference and released the "Kunpeng Computing Industry Development White Paper" jointly signed by Huawei and Arm China together with Mr. Hou Jinlong, President of Huawei Cloud & AI Products and Services, and Ms. Guo Jing, CTO of th
[Embedded]
Arm China congratulates the release of the
Chirp: Arm-based technology will help development
——Translated from embedded-computing Chirp announces support for Arm® Cortex-A and Cortex-M architectures, improving device-to-device connectivity for any Arm®-based product with audio capabilities.   Chirp’s Arm SDK benefits from the DSP capabilities of the Arm Cortex-M4 and Cortex-M7 processors to design a reliabl
[Internet of Things]
Design of media player based on ARM9
1 Introduction The rapid development of computer multimedia technology and network technology has made multimedia applications based on streaming media also used in many fields, especially in network videophone, remote monitoring, and video on demand. With the maturity of computer multimedia compression technology and
[Microcontroller]
Design of media player based on ARM9
Transplantation of μCOS-Ⅱ on ARM Series MCU S3C44B0x
Introduction At present, embedded systems have been more and more widely used in various fields such as industrial control, household appliances, mobile communications, PDAs, etc. As users have higher and higher requirements for the performance of embedded products, program design has become more and more co
[Microcontroller]
Transplantation of μCOS-Ⅱ on ARM Series MCU S3C44B0x
Interrupt shielding method of ARM7 core
Introduction When the CPU is executing a program, due to some external reasons, it is necessary to terminate the execution of the current program as soon as possible and execute the corresponding processing program. After the processing is completed, it will return to continue executing the terminated original p
[Industrial Control]
Interrupt shielding method of ARM7 core
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号