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
  • So,how can i choose the optimization level to satisfy my require? Is there any principias?

    If your requirement is that the code works properly then don't change anything.

    If your requirement is that the compiler generates the object code you expected then reduce the optimisation level to zero and define all the variables as volatile.

    I assume the latter suggestion is your requirement, but if it is something else then please explain.

Reply
  • So,how can i choose the optimization level to satisfy my require? Is there any principias?

    If your requirement is that the code works properly then don't change anything.

    If your requirement is that the compiler generates the object code you expected then reduce the optimisation level to zero and define all the variables as volatile.

    I assume the latter suggestion is your requirement, but if it is something else then please explain.

Children
No data