This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

swithc-case question

When I write a simple switch case program:
----------source:

void main()
{
    char e = 0;

    int a=0;
    int b=2;
    int c=3;
    switch(e)
    {
        case 1:
            a = a + b;
            break;
        case 2:
            a = a + c;
            break;
        default:
            break;
    }
}


But,the c51 complier generates:

            ...
     4: void main()
     5: {
     6:     char e = 0;
     7:
C:0x000F    E4       CLR      A
     8:     int a=0;
     9:     int b=2;
    10:     int c=3;
    11:     switch(e)
C:0x0010    24FE     ADD      A,#0xFE
C:0x0012    6002     JZ       C:0016
C:0x0014    04       INC      A
    12:     {
    13:         case 1:
    14:             a = a + b;
    15:             break;
C:0x0015    22       RET
    16:         case 2:
C:0x0016    22       RET
C:0x0017    00       NOP
C:0x0018    00       NOP
C:0x0019    00       NOP
         ...


How could it be possible?

Parents Reply Children
  • Hi yu zhou,

    I don't know much about this. But as Andy Neil says, it depends on your requirement.

    For Study/Debug, it is usually better to choose the lowest optimization level. (Unless you want to know the behavior of compiler.)

    If smaller binary size/better performance is important, it is usually better to choose the highest optimization level.

    And if you find something strange, then choose the lowest optimization level.

  • http://www.keil.com/support/man/docs/armcc/armcc_cjaieafa.htm

    www.keil.com/.../armccref_cihgfgfb.htm

    The above URL is for ARM, not 8051. But it does provide some hints.


    3

    Maximum optimization. -O3 performs the same optimizations as -O2 however the balance between space and time optimizations in the generated code is more heavily weighted towards space or time compared with -O2. That is:

    * -O3 -Otime aims to produce faster code than -O2 -Otime, at the risk of increasing your image size

    * -O3 -Ospace aims to produce smaller code than -O2 -Ospace, but performance might be degraded.

    In addition, -O3 performs extra optimizations that are more aggressive, such as:

    * High-level scalar optimizations, including loop unrolling, for -O3 -Otime. This can give significant performance benefits at a small code size cost, but at the risk of a longer build time.

    * More aggressive inlining and automatic inlining for -O3 -Otime.

    * Multifile compilation by default.

  • Looking at a manual for a totally different compiler for a totally different architecture probably isn't too helpful - especially when the correct manual is readily to hand:


    http://www.keil.com/support/man/docs/c51/c51_optimize.htm

    Note also the Related Knowledgebase Articles linked at the bottom of the page...