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

Caller Function Did Not Keep R3

Hi,

I have a "test_fun1()" function call to a inline ASM "test_fun2()". I found that the caller, "test_fun1()" did not keep the R3 before jump to "test_fun2()", which cause the result of the  "test_fun1()" is not correct (inline ASM "test_fun2()" use R3).

The same code works fine with Keil MDK-ARM. It also works when I change the Core from M3 to M0. 

Does the "test_fun2()" have any problem/violation?

I will be appreciated if anyone can give some suggestions. Thanks.

Following are some information and the code (It has some differences from the original application code, I create a simple ASM version to reproduces the issue). 

GNU Arm Version: 9 2020-q2-update (gcc-arm-none-eabi-9-2020-q2-update-win32.exe)

ARM_CORE = cortex-m3
ARCH_OPTION = -mcpu=$(ARM_CORE) -mthumb -mthumb-interwork
C_OPTION = -gdwarf-2 -MD -Wall -Os -mapcs-frame -ffunction-sections -fdata-sections
L_OPTION = -Wl,-Map=$(TARGET_NAME).map -Wl,--gc-sections --specs=nano.specs

Caller_R3_source_code.zip

main.c

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int result;
int main(void)
{
extern int test_fun1(int a, int b, int c, int d);
extern int gTest;
result = test_fun1(1, 2, 3, 4);
if (gTest != 4)
{
while (1); // R3 Error here
}
while (1) // OK
{
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

test.c

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void test_fun2(int a, int b, int c);
int gTest = 0;
int test_fun1(int a, int b, int c, int d)
{
int uStart;
uStart = gTest;
if (a > 0)
{
test_fun2(uStart, uStart + c, 5);
}
uStart += d;
gTest = uStart;
return 1;
}
#if defined (__CC_ARM)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

disasm.txt

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$t
main
0x00000198: b508 .. PUSH {r3,lr}
0x0000019a: 2203 ." MOVS r2,#3
0x0000019c: 2304 .# MOVS r3,#4
0x0000019e: 2102 .! MOVS r1,#2
0x000001a0: 2001 . MOVS r0,#1
0x000001a2: f000f815 .... BL test_fun1 ; 0x1d0
0x000001a6: 4b04 .K LDR r3,[pc,#16] ; [0x1b8] = 0x20000020
0x000001a8: 6018 .` STR r0,[r3,#0]
0x000001aa: 4b04 .K LDR r3,[pc,#16] ; [0x1bc] = 0x2000001c
0x000001ac: 681b .h LDR r3,[r3,#0]
0x000001ae: 2b04 .+ CMP r3,#4
0x000001b0: d000 .. BEQ 0x1b4 ; main + 28
0x000001b2: e7fe .. B 0x1b2 ; main + 26
0x000001b4: e7fe .. B 0x1b4 ; main + 28
0x000001b6: bf00 .. NOP
$d
0x000001b8: 20000020 .. DCD 536870944
0x000001bc: 2000001c ... DCD 536870940
$t
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

0