This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/MSP430FR5994: 8-bit loop counter treated as 16-bit

Part Number: MSP430FR5994

Tool/software: TI C/C++ Compiler

I have used this loop construct with an 8-bit loop counter. If the value for count is passed as 0 I would expect the loop to execute 256 times. It actually executes 65536 times when the optimiser is turned on (-O2).

static uint8_t buffer[256];

void do_loop(uint8_t count)
{
    uint8_t *p = buffer;

    do *p++ = 0;
    while (--count != 0);
}

Looking at the list file I'm seeing instructions like this (only code lines and comments retained):

 285            ;*****************************************************************************
 286            ;* FUNCTION NAME: do_loop                                                    *
 287            ;*                                                                           *
 288            ;*   Regs Modified     : SP,SR,r12,r15                                       *
 289            ;*   Regs Used         : SP,SR,r12,r15                                       *
 290            ;*   Local Frame Size  : 0 Args + 0 Auto + 0 Save = 0 byte                   *
 291            ;*****************************************************************************
 292            do_loop:
 293            ;* --------------------------------------------------------------------------*
 294            ;* r12   assigned to $O$L1
 295            ;* r12   assigned to count
 302            ;* r15   assigned to p
 311 000000 4C4C          MOV.B     r12,r12               ; [] 
 313 000002 008F!         MOVX.A    #buffer+0,r15         ; [] |44| 
     000004 0000 
 314            ;* --------------------------------------------------------------------------*
 315            ;*   BEGIN LOOP $C$L1
 316            ;*
 317            ;*   Loop source line                : 45
 318            ;*   Loop closing brace source line  : 48
 319            ;*   Known Minimum Trip Count        : 1
 320            ;*   Known Maximum Trip Count        : 256
 321            ;*   Known Max Trip Count Factor     : 1
 322            ;* --------------------------------------------------------------------------*
 323 000006       $C$L1:    
 325 000006 00AF          ADDA      #1,r15                ; [] |47| 
     000008 0001 
 326 00000a 43CF          MOV.B     #0,-1(r15)            ; [] |47| 
     00000c FFFF 
 328 00000e 831C          SUB.W     #1,r12                ; [] |49| 
 329 000010 23FA          JNE       $C$L1                 ; [] |49| 
 330                                                      ; [] |49| 
 331            ;* --------------------------------------------------------------------------*
 336 000012 0110          RETA      ; [] 

So even though the loop counter is initially loaded as 8-bit, the decrement is on a 16-bit variable. If the initial value is 0 it gets decremented to 0xffff instead of 0xff as would be expected.

It looks a lot like the instruction on line 328 should be SUB.B instead of SUB.W

Changing the test to while ((--count & 0xff) != 0) makes no difference either.

This was compiled using Compiler 16.9.8.LTS. I have also tried using 18.1.4.LTS and the result is the same.

-vmspx --data_model=restricted -O2 --use_hw_mpy=F5 --include_path="C:/ti/ccsv8/ccs_base/msp430/include" --include_path="C:/ti/ccsv8/tools/compiler/ti-cgt-msp430_16.9.8.LTS/include" --advice:power=all --advice:hw_config=all --define=__MSP430FR5994__ --define=_MPU_ENABLE --printf_support=nofloat --diag_warning=225 --diag_wrap=off --display_error_number --silicon_errata=CPU21 --silicon_errata=CPU22 --silicon_errata=CPU40 --asm_listing

  • Regarding the loop, you say ...

    Simon Large said:
    It actually executes 65536 times

    I agree that is wrong.

    Thank you for reporting this problem, and submitting a concise test case.  I can reproduce the same result.  I filed CODEGEN-5574 in the SDOWP system to have this investigated.  You are welcome to follow it with the SDOWP link below in my signature.

    Thanks and regards,

    -George