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.
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
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- The Maximum Stack Usage in the compiled .htm file is 0 bytes
- [National Technology N32 Bluetooth chip development package] --N32WB452 series
- Regarding the use of UCC27531, I just want to ask a small question
- XC3S200A chip loading problem
- Design and implementation of embedded Bluetooth PSTN voice access point
- AD strange line. Can't delete it no matter what, please help
- Discounted boards are reduced by 100 yuan! Components are available as low as 0 yuan!
- MSP430G2553 clock external crystal configuration instructions
- Difficulty in UHF reader design - anti-collision
- How to use MSP430 hardware I2C - Taking BH1710 and AT24C02 as examples