View C ++ from the perspective of Assembly (the cyclic process)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Loop is another important technology we encounter in programming. Through iterative operations, we can obtain any expected results. Of course, this iteration has basic conditions, either time-based, space-based, or a foreign interaction. There are many loop methods, but they are commonly used: while, for, do-while, And Goto. Many companies do not like Goto for projects. This is not to say that GOTO is not good. It is mainly because goto is too random. If it is not used well, it will reduce the readability of the Code, it affects the efficiency of others.

(1) Why do-while is executed first and then determined?

Old Rules: Let's look at the sample code first:

21:       int m = 10;00401638   mov         dword ptr [ebp-4],0Ah22:       do {23:           printf("%d\n", m);0040163F   mov         eax,dword ptr [ebp-4]00401642   push        eax00401643   push        offset string "%d\n" (0046f01c)00401648   call        printf (00420fb0)0040164D   add         esp,824:           m ++;00401650   mov         ecx,dword ptr [ebp-4]00401653   add         ecx,100401656   mov         dword ptr [ebp-4],ecx25:       }while(m < 10);00401659   cmp         dword ptr [ebp-4],0Ah0040165D   jl          process+1Fh (0040163f)

What if I change to while?

21:       int m = 10;00401638   mov         dword ptr [ebp-4],0Ah22:       while(m < 20)0040163F   cmp         dword ptr [ebp-4],14h00401643   jge         process+41h (00401661)23:       {24:           printf("%d\n", m);00401645   mov         eax,dword ptr [ebp-4]00401648   push        eax00401649   push        offset string "%d\n" (0046f01c)0040164E   call        printf (00420fb0)00401653   add         esp,825:           m ++;00401656   mov         ecx,dword ptr [ebp-4]00401659   add         ecx,10040165C   mov         dword ptr [ebp-4],ecx26:       }0040165F   jmp         process+1Fh (0040163f)27:   }

In fact, the above Code is already very obvious. When Do-while is used, the module first performs operations and then determines the size of the data range. When while is different, the module starts to judge and continues to run after the judgment is successful, otherwise, exit the loop module. What if it is?

21:       for(int m = 10; m < 20; m++)00401638   mov         dword ptr [ebp-4],0Ah0040163F   jmp         process+2Ah (0040164a)00401641   mov         eax,dword ptr [ebp-4]00401644   add         eax,100401647   mov         dword ptr [ebp-4],eax0040164A   cmp         dword ptr [ebp-4],14h0040164E   jge         process+4Ch (0040166c)22:       {23:           printf("%d\n", m);00401650   mov         ecx,dword ptr [ebp-4]00401653   push        ecx00401654   push        offset string "%d\n" (0046f01c)00401659   call        printf (00420fc0)0040165E   add         esp,824:           m ++;00401661   mov         edx,dword ptr [ebp-4]00401664   add         edx,100401667   mov         dword ptr [ebp-4],edx25:       }0040166A   jmp         process+21h (00401641)

We found that, in fact, for is slightly different from the preceding while and do-while. When M is assigned a value for the first time, it does not add 1, but directly jumps to the address 0x40164a to run and judges m and 20. If the judgment succeeds, the system jumps into the loop module. Otherwise, the loop module is crossed. So after the loop processing is complete? That is, how does the loop module handle M ++? We found that the code was processed again at 0x00401641. But here is not the code starting from the entire loop module, but the auto-increment processing of M. After the auto-increment is completed, continue to judge. The following process is the same as that for the first time and will not be repeated.

(2) How does one jump out of multiple loops?

Many friends sometimes want to find a conditional variable in a multi-tier loop. However, after finding a specific variable, they want to exit the loop quickly. What should we do? The following is my personal practice, which is for your reference only.

int flag = 0;         for(int m = 1; m < 20 && !flag; m++){for(int n = 1; n < 20 && !flag; n++){for(int t = 1; t < 20 && !flag; t++){if(/* special conditions are satisfied */)flag = 1;}}}

(3) While (1) Is there any other representation?

int flag = 0;for(;;) {/* code segment */}do{/* code segment */}while(1);  loop:{/* code segment */}if(!flag)goto loop;

Summary:

In fact, there are still many details to deal with in the loop, such:

(1) When looping, please be sure to fill in the conditions for program termination

(2) Pay attention to the difference between 8-bit char and 32-bit int during the loop, and be sure not to use an endless loop.

(3) Pay attention to '\ 0' for character Loops'

(4) do not combine loops and judgments to 1, and leave a living path for your colleagues. Do not think while (* DST ++ = * SRC ++ ); writing code in this way is handsome.

(5) Make sure that the returned value is the address you need, the previous address, or the next address.

(6) do not add additional statements in for (;). The more you add, the more risks you have.

[Forecast: The following describes the flexible use of break, continue, and Goto]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.