We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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)
Are you trying to say that the value of R7 is not being used, but the register is being modified? The function can be implemented using only R0 register, but compiler use R7 and R0. R7 is useless.
The more you look at C51 output, the more you'll feel this way. Since you cannot improve the compiler, just stop looking at its output. You'll save yourself some disappointment.
What Optimisation level are you using?
If I set optimisation level to 8 or 11, the output is the same.
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.
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.
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.