The loop structures of C language include for loop, while loop, do loop and goto loop. This article introduces the first three loop methods. 1. for loop structureThe for loop can also be called a step loop. Its characteristic is that it is often used to define the scope of the loop. Let's look at a simple C language code, as follows:
This is a typical program for finding the cumulative sum of 1 to 100. Through this program, we can understand the disassembly code of the for loop structure.
This time, the disassembled code has modified the variables and labels, which looks more intuitive. From the modified labels, the for structure can be divided into three parts: the part above LOC_STEP is the initialization part, the part below LOC_STEP is the part that modifies the loop variable, and the part below LOC_CMP and above LOC_ENDFOR is the part that compares the loop condition and the loop body. The disassembled structure of the for loop is as follows:
Let’s use IDA to look at the generated process structure diagram, as shown in Figure 1. Figure 1 Flowchart of the for structure 2. do…while loop structureThe body of the do loop is always executed once, which is the difference between the do loop and the while loop. Here is the code for adding 1 to 100. Let's take a look at its disassembly structure. First, let's look at the C language code, as follows:
The structure of the do loop is much simpler than that of the for loop, and the disassembled code is much less. Let's first look at the flowchart generated by IDA, as shown in Figure 2. Figure 2 do loop flow chart The disassembled code is as follows:
The body of the do loop is between LOC_DO and the jle of 0040104C. Its structure is as follows:
3. While loop structureThe difference between a while loop and a do loop is that a conditional check must be performed before entering the loop body. The loop body may not be executed once because the loop condition is not met.
Let's take a look at its disassembled code. The while loop has one more condition than the do loop, so there is one more branch. The disassembled code is as follows:
The main part of the while loop is between LOC_WHILE and LOC_WHILEEND. The two sentences below LOC_WHILE are cmp and jxx instructions, and the jmp instruction is above LOC_WHILEEND. These two parts are in a fixed format, and their structure is organized as follows:
Let’s take a look at the flowchart generated by IDA, as shown in Figure 3. Figure 3 while loop flow chart Among the three types of loops, for loop, do loop and while loop, the do loop is obviously more efficient, and the while loop is relatively more efficient than the for loop. |
>>: How wireless technology is changing the world
DesiVPS is a foreign VPS hosting company headquar...
Chrome is the most popular web browser in the wor...
Xi'an's health code crashed twice in a ro...
Yesterday, the highly anticipated 2024 General Ar...
Affected by the epidemic, I have participated in ...
5G is developing in full swing. Currently, the nu...
There’s a lot of hype surrounding the next Wi-Fi ...
I wonder if you are confused: Why do we need a MA...
In the era of cloud computing, IT system construc...
The Federal Communications Commission (FCC) voted...
Kvmla, a long-established Chinese VPS hosting com...
We often share promotional information released b...
[[414422]] This article is reprinted from the WeC...
According to a study by researchers from the U.S....
In recent years, intent-based networking (IBN) ha...