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

KeilC51 can not compile this code correctly

hi: I have a code below:

     char pop_value = 0X80;
     if(pop_value == 0X80)
     {
         pop_value = pop_value;//point 1

     }
     pop_value = pop_value;//point 2

I would have thought the code would jump to point 1 after logical judge, but the simulating result exceed my expectations that the code jumped to point 2. I watch the Disassembly code :

    82:             pop_value = 0X80; 
C:0x0A68    7853     MOV      R0,#pop_value(0x53)
C:0x0A6A    7680     MOV      @R0,#P0(0x80)
     83:             if(pop_value == 0X80) 
C:0x0A6C    E6       MOV      A,@R0
C:0x0A6D    FF       MOV      R7,A
C:0x0A6E    FD       MOV      R5,A
C:0x0A6F    33       RLC      A
C:0x0A70    95E0     SUBB     A,ACC(0xE0)
C:0x0A72    FC       MOV      R4,A
C:0x0A73    ED       MOV      A,R5
C:0x0A74    6480     XRL      A,#P0(0x80)
C:0x0A76    4C       ORL      A,R4
C:0x0A77    7002     JNZ      C:0A7B
    84:             {
    85:             pop_value = pop_value;
    86: 
C:0x0A79    A607     MOV      @R0,0x07
    87:             }
C:0x0A7B    22       RET
                 PUTCHAR:


I can not find the error happened.so I also compile this C code in another project. the code can jump to point 1 after logical judge ,nor jump to point 2.I watch the the Disassembly code at once:

    61:      item = 0X80; 
C:0x0A94    752400   MOV      0x24,#malloc_mempool(0x00)
C:0x0A97    752580   MOV      0x25,#P0(0x80)
    62:      if( item== 0x80) 
C:0x0A9A    E525     MOV      A,0x25
C:0x0A9C    6480     XRL      A,#P0(0x80)
C:0x0A9E    4524     ORL      A,0x24
C:0x0AA0    7006     JNZ      C:0AA8
    63:      {
    64:      item =item; 
C:0x0AA2    852424   MOV      0x24,0x24
C:0x0AA5    852525   MOV      0x25,0x25
    65:      }


my keiC51 version is V8.16. I don't know why the same C code has different assemble code in different project. The important thing is that the compiler make a wrong logical judge.

Parents Reply Children
  • Looks like that the compiler optimisation is smarter than the programmer.

    Yes, but still not as smart as it should be. It should have optimized away everything except the first line. If the variable pop_value isn't used anywhere in a non-trivial way, it should have optimized away the first line too. Bottom line: the source code should compile into nothing.

  • Yes, but still not as smart as it should be. It should have optimized away everything except the first line.
    maybe not, but Keil, in their infinite wisdom, do all optimizing in the linker.

    Erik

  • maybe not, but Keil, in their infinite wisdom, do all optimizing in the linker

    Not all --- not by a rather wide margin. I'm pretty sure none of the linker-based optimazation are relevant to the example under study. This should be optimized to nothing before the linker is even run.

  • maybe not, but Keil, in their infinite wisdom, do all optimizing in the linker

    Not all --- not by a rather wide margin. I'm pretty sure none of the linker-based optimazation are relevant to the example under study. This should be optimized to nothing before the linker is even run.

    I still wonder why the making a switch an indirect jump instead of a combersome calculation (when the state values are 'organized') is not done in the compiler but in the linker

    Erik