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
The future of cloud is bright. By 2024, more than...
Have you ever encountered occasional slow Interne...
As we know, Standard Interconnect is a business t...
Edge computing market prospects and valuations Ed...
Gartner's report shows that by 2022, 75% of d...
The 5G era is approaching. While people are full ...
Preface The gateway is the entrance for traffic r...
The last time the tribe shared information about ...
The introduction of the new infrastructure strate...
The tribe has shared information about Maxthon ma...
"Times make heroes" is an eternal truth...
Regarding the technical solutions for future comm...
Labs Guide With the increase of network applicati...
[[398109]] On May 7, the three major operators is...
edgeNAT has sent out promotions for the Mid-Autum...