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

useless R7

bit CheckPacket(void)
{
        unsigned char idata *pBuf = g_UART0Buf;
        unsigned char len, checksum;

        if (*pBuf == PACKET_START_ID / 256)
        {
                pBuf++;
                if (*pBuf == PACKET_START_ID % 256)
                {
                        pBuf++;
                        len = *pBuf;
                        checksum = *pBuf;
                        pBuf++;
                        len++;
                        do
                        {
                                checksum += *pBuf;
                                pBuf++;
                                len--;
                        } while (len);
                        if(!checksum)
                        {
                                return 1;
                        }
                }
        }
        return 0;
}
             ; FUNCTION CheckPacket (BEGIN)
                                           ; SOURCE LINE # 13
                                           ; SOURCE LINE # 14
                                           ; SOURCE LINE # 15
                 E     MOV     R7,#LOW g_UART0Buf
;---- Variable 'pBuf' assigned to Register 'R7' ----
                                           ; SOURCE LINE # 18
                       MOV     R0,AR7
                       MOV     A,@R0
                       XRL     A,#07H
                 R     xJNZ    ?C0001
                                           ; SOURCE LINE # 19
                                           ; SOURCE LINE # 20
                       INC     R7
                                           ; SOURCE LINE # 21
                       INC     R0
                       MOV     A,@R0
                       XRL     A,#0EH
                 R     xJNZ    ?C0001
                                           ; SOURCE LINE # 22
                                           ; SOURCE LINE # 23
                       INC     R7
                                           ; SOURCE LINE # 24
                       INC     R0
                       MOV     A,@R0
                       MOV     R6,A
;---- Variable 'len' assigned to Register 'R5' ----
                       MOV     R5,A
                                           ; SOURCE LINE # 25
;---- Variable 'checksum' assigned to Register 'R6' ----
                                           ; SOURCE LINE # 26
                       INC     R7
                                           ; SOURCE LINE # 27
                       INC     R5
             ?C0005:
                                           ; SOURCE LINE # 29
                                           ; SOURCE LINE # 30
                       MOV     R0,AR7
                       MOV     A,@R0
                       ADD     A,R6
                       MOV     R6,A
                                           ; SOURCE LINE # 31
                       INC     R7
                                           ; SOURCE LINE # 32
                 R     DJNZ    R5,?C0005
                                           ; SOURCE LINE # 33
                                           ; SOURCE LINE # 34
                 R     xJNZ    ?C0001
                                           ; SOURCE LINE # 35
                                           ; SOURCE LINE # 36
                       SETB    C
                       RET
                                           ; SOURCE LINE # 37
                                           ; SOURCE LINE # 38
                                           ; SOURCE LINE # 39
             ?C0001:
                                           ; SOURCE LINE # 40
                       CLR     C
                                           ; SOURCE LINE # 41
             ?C0007:
                       RET
             ; FUNCTION CheckPacket (END)

Parents
  • Why do you care about the compiler generated code at all?

    You should only expect the generated code is working correctly, not how the assembler instruction sequence is.

    It is NOT the job of a compiler to generate smaller and faster code than a professional COULD do in assembler.

    If you like the smallest, fastest and smartest code, then write it in assembler yourself. This attitude may be O.K. as a hobbyist, where time costs 'nothing', but is totally inacceptable when working for a company.

    If the generated code

    1) fits into the available code memory space and
    2) if the code is fast enough (based on the product requirements)

    then there is nothing to worry about, and no need to waste time and money.

Reply
  • Why do you care about the compiler generated code at all?

    You should only expect the generated code is working correctly, not how the assembler instruction sequence is.

    It is NOT the job of a compiler to generate smaller and faster code than a professional COULD do in assembler.

    If you like the smallest, fastest and smartest code, then write it in assembler yourself. This attitude may be O.K. as a hobbyist, where time costs 'nothing', but is totally inacceptable when working for a company.

    If the generated code

    1) fits into the available code memory space and
    2) if the code is fast enough (based on the product requirements)

    then there is nothing to worry about, and no need to waste time and money.

Children
  • If I write in this:

    bit CheckPacket(void)
    {
            unsigned char idata *pBuf = g_UART0Buf;
            unsigned char tmp, len, checksum;
    
            tmp = *pBuf;
            if (tmp == PACKET_START_ID / 256)
            {
                    pBuf++;
                    tmp = *pBuf;
                    if (tmp == PACKET_START_ID % 256)
                    {
                            pBuf++;
                            len = *pBuf;
                            checksum = *pBuf;
                            pBuf++;
                            len++;
                            do
                            {
                                    checksum += *pBuf;
                                    pBuf++;
                                    len--;
                            } while (len);
                            if(!checksum)
                            {
                                    return 1;
                            }
                    }
            }
            return 0;
    }
    


    the compiler output will be:

                 ; FUNCTION CheckPacket (BEGIN)
                                               ; SOURCE LINE # 13
                                               ; SOURCE LINE # 14
                                               ; SOURCE LINE # 15
    ;---- Variable 'pBuf' assigned to Register 'R0' ----
                     E     MOV     R0,#LOW g_UART0Buf
                                               ; SOURCE LINE # 18
                           MOV     A,@R0
    ;---- Variable 'tmp' assigned to Register 'R7' ----
                                               ; SOURCE LINE # 19
                           XRL     A,#07H
                     R     xJNZ    ?C0001
                                               ; SOURCE LINE # 20
                                               ; SOURCE LINE # 21
                           INC     R0
                                               ; SOURCE LINE # 22
                           MOV     A,@R0
                                               ; SOURCE LINE # 23
                           XRL     A,#0EH
                     R     xJNZ    ?C0001
                                               ; SOURCE LINE # 24
                                               ; SOURCE LINE # 25
                           INC     R0
                                               ; SOURCE LINE # 26
                           MOV     A,@R0
                           MOV     R7,A
    ;---- Variable 'len' assigned to Register 'R6' ----
                           MOV     R6,A
                                               ; SOURCE LINE # 27
    ;---- Variable 'checksum' assigned to Register 'R7' ----
                                               ; SOURCE LINE # 28
                           INC     R0
                                               ; SOURCE LINE # 29
                           INC     R6
                 ?C0005:
                                               ; SOURCE LINE # 31
                                               ; SOURCE LINE # 32
                           MOV     A,@R0
                           ADD     A,R7
                           MOV     R7,A
                                               ; SOURCE LINE # 33
                           INC     R0
                                               ; SOURCE LINE # 34
                     R     DJNZ    R6,?C0005
                                               ; SOURCE LINE # 35
                                               ; SOURCE LINE # 36
                     R     xJNZ    ?C0001
                                               ; SOURCE LINE # 37
                                               ; SOURCE LINE # 38
                           SETB    C
                           RET
                                               ; SOURCE LINE # 39
                                               ; SOURCE LINE # 40
                                               ; SOURCE LINE # 41
                 ?C0001:
                                               ; SOURCE LINE # 42
                           CLR     C
                                               ; SOURCE LINE # 43
                 ?C0007:
                           RET
                 ; FUNCTION CheckPacket (END)
    


    6 bytes smaller than previous, and the pointer only use R0, but I don't know why.

  • 6 bytes smaller than previous, and the pointer only use R0, but I don't know why.

    You'd have your answers if you had the source code to the compiler. But it is closed-source, so there you have it.
    Obviously, it's not a professional project you are working on, since you can spend so much time on such a minor matter. Your interest would be justified if you were working on improving the compiler.
    Just leave it. Believe me, your time can be spent much more productively.