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.

EK-TM4C1294XL: Is this a C-code or a compiler error?

Part Number: EK-TM4C1294XL

I have the little code snippet below:

    TotalScanDepth=FootprintDistance * ScanDepth;

    if (Rectangular)
        OuterStart=(TotalScanDepth % 2) - FootprintDistance;
    else
        OuterStart=TotalScanDepth % 2 - FootprintDistance % 2;

FootprintDistance is a local constant (for that routine). Value = 16.

ScanDepth is a variable, current value = 4

Rectangular is currently true

So I expext TotalScanDepth to end up being 64. Works!

But OuterStart ends up at -16? I expected +16!

Below is what the disassembler shows (with my comments on the actual register values):

00019de6:   4618                mov        r0, r3                        //r0 = 4
 810              OuterStart=(TotalScanDepth % 2) - FootprintDistance;
00019de8:   F1BC0F00            cmp.w      r12, #0
 807          TotalScanDepth=FootprintDistance * ScanDepth;
00019dec:   EA4F1000            lsl.w      r0, r0, #4                    //r0 = 64
 810              OuterStart=(TotalScanDepth % 2) - FootprintDistance;
00019df0:   D006                beq        $C$L14
00019df2:   EB0071D0            add.w      r1, r0, r0, lsr #31           //r1 = r0 = 64
00019df6:   F0210101            bic        r1, r1, #1                    //nothing happens at all ?
00019dfa:   1A40                subs       r0, r0, r1                    //r0 = 0
00019dfc:   3810                subs       r0, #0x10                     //r0 = -16
00019dfe:   E000                b          $C$L15

TotalScanDepth is overwritten to -16, but it is not used after this.

So, what am I, or the compiler, doing wrong?

Sorry, this editor really does not want you to edit your post!
  • I changed my code to:

    if (Rectangular)
        OuterStart=(TotalScanDepth >> 1) - FootprintDistance;
    else
        OuterStart=TotalScanDepth % 2 - FootprintDistance % 2;

    And now I get the +16 I expected!

    So what is the difference between

        OuterStart=(TotalScanDepth % 2) - FootprintDistance;
    

    and

        OuterStart=(TotalScanDepth >> 1) - FootprintDistance;
    

    Aha.... % is modulo, not integer div.!

    I work with too many languages!

  • Hello Ja,

    Glad it was a quick and easy fix. Thanks for sharing your comments, maybe another community member will find it helpful. It really can be confusing jumping between languages nowadays as we learn so many different ones...