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
  • This has been discussed many times.
    The compiler is allowed to emit ANY code as long as the emitted code generates the correct output. Your example generates NO output whatsoever, and the emitted code does so as well. That's optimization at work. In fact, I would expect the emitted code to consist of a single instruction RET.
    Try lowering optimization level.

Reply
  • This has been discussed many times.
    The compiler is allowed to emit ANY code as long as the emitted code generates the correct output. Your example generates NO output whatsoever, and the emitted code does so as well. That's optimization at work. In fact, I would expect the emitted code to consist of a single instruction RET.
    Try lowering optimization level.

Children