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

Compiler produces inefficient assembly code?

I and a co-worker are programming an 8051 uController in C. Last week we were struggling with a poor perfomance time of our program. So I talked to the assembly guy and we figured that our way of software timer handling was too slow. We were using 16 bit variables. Now I tweaked the timer software a bit and it runs about 4x faster, which is fast enough.

Digging a little deeper in the produced assembly code we found a 'persistent nuisance'. As a test I wrote these lines in code:

uint8 j;
        for(j=10;j--;){
                rightTorqueArray[j] = j; }


The array is an unsigned char array but when we observe the assembly

        MOV     R7,#0AH
?C0001:
        MOV     R6,AR7
        DEC     R7
        MOV     A,R6
        JZ      ?C0002
;               rightTorqueArray[j] = j; }
                        ; SOURCE LINE # 52
        MOV     A,#LOW (rightTorqueArray)
        ADD     A,R7
        MOV     DPL,A
        CLR     A
        ADDC    A,#HIGH (rightTorqueArray)
        MOV     DPH,A
        MOV     A,R7
        MOVX    @DPTR,A
        SJMP    ?C0001
?C0002:

We noticed that the array is adressed with LOW and HIGH so apparantly it is treated as a 16 bit variable. But my assembly-nese is not so well, so please correct me if I am wrong.

I set the Code Optimalization at level 8: reuse Common entry code and the emphasis at Favor speed.

The assembly was produced as a .SRC file using #pragma SRC on top of the C-file.

Parents
  • We noticed that the array is adressed with LOW and HIGH so apparantly it is treated as a 16 bit variable.

    If that "we" included aforementioned "assembly guy", you need a better person to fill that job. No, your array is not "treated as a 16-bit variable". It's treated as an array living in a 16-bit address space, because of either a) how you explicitly declared that array, or b) your choice of memory model, which had the same effect implicitly. Since you didn't show your array's declaration, nobody know which it is.

    That declaration is the actual reason this loop is so slow. You basically got exactly what you asked for --- you just didn't know how to ask for what you actually wanted.

    The assembly was produced as a .SRC file using #pragma SRC on top of the C-file.

    That may have been counter-productive. #pragma SRC has a side effect of turning off some optimizations, because those are not really expressible in assembly.

    You really should look at list files from normal compilation, and/or post-linking, "absolute" list file instead.

Reply
  • We noticed that the array is adressed with LOW and HIGH so apparantly it is treated as a 16 bit variable.

    If that "we" included aforementioned "assembly guy", you need a better person to fill that job. No, your array is not "treated as a 16-bit variable". It's treated as an array living in a 16-bit address space, because of either a) how you explicitly declared that array, or b) your choice of memory model, which had the same effect implicitly. Since you didn't show your array's declaration, nobody know which it is.

    That declaration is the actual reason this loop is so slow. You basically got exactly what you asked for --- you just didn't know how to ask for what you actually wanted.

    The assembly was produced as a .SRC file using #pragma SRC on top of the C-file.

    That may have been counter-productive. #pragma SRC has a side effect of turning off some optimizations, because those are not really expressible in assembly.

    You really should look at list files from normal compilation, and/or post-linking, "absolute" list file instead.

Children