Tool/software: TI C/C++ Compiler
It's an issue in the assembly code generated by the compiler for ARM TM4C123GH6PM in some cases. I already tried some versions of the TI compiler, and figured out that all are buggy.
Try to do these steps below :-
1) Define a 2D array.
2) Define a pointer to array with a variable number of columns.
3) Read any value in the array using the pointer in a comparison statement (if statement).
const int32_t ai32Arr2D[5][5] = { { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 }, }; int32_t i32ColNum = 5; const int32_t (*pai32Arr2D)[i32ColNum] = ai32Arr2D; /************************/* This will fail */**************************/ if (pai32Arr2D[2][1] == 1) { DEBUG_PRINT("Correct %u", pai32Arr2D[2][1]); } else { DEBUG_PRINT("Not correct %u", pai32Arr2D[2][1]); } /************************/* This will succeed */**************************/ int32_t i32Temp = pai32Arr2D[2][1]; if (i32Temp == 1) { DEBUG_PRINT("Correct %u", pai32Arr2D[2][1]); } else { DEBUG_PRINT("Not correct %u", pai32Arr2D[2][1]); }
The assembly code :-
323 const int32_t (*pai32Arr2D)[i32ColNum] = ai32Arr2D;
0000077e: 981A ldr r0, [sp, #0x68]
00000780: 9019 str r0, [sp, #0x64]
00000782: F8CDD06C str.w sp, [sp, #0x6c]
325 int32_t i32Temp = pai32Arr2D[2][1];
00000786: 9919 ldr r1, [sp, #0x64]
00000788: 981B ldr r0, [sp, #0x6c]
0000078a: EB0000C1 add.w r0, r0, r1, lsl #3
0000078e: 6840 ldr r0, [r0, #4]
00000790: 901C str r0, [sp, #0x70]
327 if (pai32Arr2D[2][1] == 1)
00000792: 9819 ldr r0, [sp, #0x64]
00000794: 9A19 ldr r2, [sp, #0x64]
00000796: 991B ldr r1, [sp, #0x6c]
00000798: 0040 lsls r0, r0, #1
0000079a: 4350 muls r0, r2, r0
0000079c: EB010180 add.w r1, r1, r0, lsl #2
000007a0: 6848 ldr r0, [r1, #4]
000007a2: 2801 cmp r0, #1
000007a4: D108 bne $C$L42