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], 0Ah

22: do {

23: printf ("% d \ n", m );

0040163F mov eax, dword ptr [ebp-4]

00401642 push eax

00401643 push offset string "% d \ n" (0046f01c)

00401648 call printf (00420fb0)

0040164D add esp, 8

24: m ++;

00401650 mov ecx, dword ptr [ebp-4]

00401653 add ecx, 1

00401656 mov dword ptr [ebp-4], ecx

25:} while (m <10 );

00401659 cmp dword ptr [ebp-4], 0Ah

0040165D jl process + 1Fh (0040163f)

What if I change to while?

 

21: int m = 10;

00401638 mov dword ptr [ebp-4], 0Ah

22: while (m <20)

0040163F cmp dword ptr [ebp-4], 14 h

00401643 jge process + 41 h (00401661)

23 :{

24: printf ("% d \ n", m );

00401645 mov eax, dword ptr [ebp-4]

00401648 push eax

00401649 push offset string "% d \ n" (0046f01c)

0040164E call printf (00420fb0)

00401653 add esp, 8

25: m ++;

00401656 mov ecx, dword ptr [ebp-4]

00401659 add ecx, 1

0040165C mov dword ptr [ebp-4], ecx

26 :}

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], 0Ah

0040163F jmp process + 2Ah (0040164a)

00401641 mov eax, dword ptr [ebp-4]

00401644 add eax, 1

00401647 mov dword ptr [ebp-4], eax

0040164A cmp dword ptr [ebp-4], 14 h

0040164E jge process + 4Ch (0040166c)

22 :{

23: printf ("% d \ n", m );

00401650 mov ecx, dword ptr [ebp-4]

00401653 push ecx

00401654 push offset string "% d \ n" (0046f01c)

00401659 call printf (00420fc0)

0040165E add esp, 8

24: m ++;

00401661 mov edx, dword ptr [ebp-4]

00401664 add edx, 1

00401667 mov dword ptr [ebp-4], edx

25 :}

0040166A jmp process + 21 h (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.

 

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.