Avenue to simplicity-new ideas for Program Design

Source: Internet
Author: User

Most people who have studied single-chip microcomputer know the names of "Water lamp" or "horse lamp". The specific implementation effect is to display a group of changes cyclically according to a fixed rule. For example, if you use a byte length space to realize the "marquee", you can change the value of this Byte as follows:

"0000 0001b"-> "0000 0010b"-> "0000 0100b"-> "0000 1000b"-> "0001 10000b"-> "0010 10000b"-> "0100 10000b" -> "1000 10000b"-> "0000 0001b" (loop)

For example, to change the value of an int type variable from 0 to 9, the general method is as follows:

First version:

int val = 0;while(1){val++;if( val > 9 )val = 0;}//End;

Use the Atmel 89C51 configuration in the Keil UV3 environment. The compiled assembly code is:

C:0x0000    020010   LJMP     C:0010     2: void main()      3: {      4:         unsigned char val = 0;      5:          C:0x0003    E4       CLR      AC:0x0004    FF       MOV      R7,A     6:         while(1)      7:         {      8:                 val++; C:0x0005    0F       INC      R7     9:                 if( val > 9 ) C:0x0006    EF       MOV      A,R7C:0x0007    D3       SETB     CC:0x0008    9409     SUBB     A,#0x09C:0x000A    40F9     JC       C:0005    10:                         val = 0; C:0x000C    E4       CLR      AC:0x000D    FF       MOV      R7,A    11:         } C:0x000E    80F5     SJMP     C:0005C:0x0010    787F     MOV      R0,#0x7FC:0x0012    E4       CLR      AC:0x0013    F6       MOV      @R0,AC:0x0014    D8FD     DJNZ     R0,C:0013C:0x0016    758107   MOV      SP(0x81),#0x07C:0x0019    020003   LJMP     main(C:0003)

Simplified and modified version:

int val = 0;while(1){if( (++val) > 9 )val = 0;}

Use the Atmel 89C51 configuration in the Keil UV3 environment. The compiled assembly code is:

C:0x0000    020010   LJMP     C:0010     2: void main()      3: {      4:         unsigned char val = 0;      5:          C:0x0003    E4       CLR      AC:0x0004    FF       MOV      R7,A     6:         while(1)      7:         {      8:                 if( (++val) > 9 ) C:0x0005    0F       INC      R7C:0x0006    EF       MOV      A,R7C:0x0007    D3       SETB     CC:0x0008    9409     SUBB     A,#0x09C:0x000A    40F9     JC       C:0005     9:                         val = 0; C:0x000C    E4       CLR      AC:0x000D    FF       MOV      R7,A    10:         } C:0x000E    80F5     SJMP     C:0005C:0x0010    787F     MOV      R0,#0x7FC:0x0012    E4       CLR      AC:0x0013    F6       MOV      @R0,AC:0x0014    D8FD     DJNZ     R0,C:0013C:0x0016    758107   MOV      SP(0x81),#0x07C:0x0019    020003   LJMP     main(C:0003)

From the two examples above, after keilc51 compiles the source file, the generated assembly code is exactly the same. That is to say, although the previous two programming methods are somewhat different from the C language perspective, however, after being compiled as a machine code, the execution efficiency of the first and second codes is the same.

The above two programming methods in C language format are the methods I have mastered so far, to understand a new programming method or idea, the effect of running a horse lamp in this example is as follows:

unsigned char val = 0;while(1){val = (val+1)%10;}

Use the Atmel 89C51 configuration in the Keil UV3 environment. The compiled assembly code is:

C:0x0000    020010   LJMP     C:0010     2: void main()      3: {      4:         unsigned char val = 0;      5:          C:0x0003    E4       CLR      AC:0x0004    FF       MOV      R7,A     6:         while(1)      7:         {      8:                 val = (val+1)%10; C:0x0005    0F       INC      R7C:0x0006    EF       MOV      A,R7C:0x0007    D3       SETB     CC:0x0008    9409     SUBB     A,#0x09C:0x000A    40F9     JC       C:0005     9:         } C:0x000C    E4       CLR      AC:0x000D    FF       MOV      R7,A    10: }C:0x000E    80F5     SJMP     C:0005C:0x0010    787F     MOV      R0,#0x7FC:0x0012    E4       CLR      AC:0x0013    F6       MOV      @R0,AC:0x0014    D8FD     DJNZ     R0,C:0013C:0x0016    758107   MOV      SP(0x81),#0x07C:0x0019    020003   LJMP     main(C:0003)

As a result, I found that the compilation code is exactly the same for the three different C language programming methods (or processes) compiled as assembly code by the keilc51 compiler. Since the Assembly Code is the same, the corresponding machine code is the same, and the code execution efficiency and time are the same.

So far, I was surprised that Keil 'S Compiler was so "efficient" and "intelligent" that it was written for the same purpose, but it was written using programming methods of different styles, after the code is compiled, the final execution efficiency is almost the same.

It is concluded that Keil has a very efficient compilation method for the source code of C language, so that the Code implemented by the same purpose but different programming methods can achieve very efficient execution efficiency on the hardware.

So, since the compiler is so efficient, does it mean that users can program at will without considering the compilation efficiency? This question is to be further studied and researched!

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.