Here is another program to illustrate the problem I'm seeing. The
problem is evident independent of gdb.
#include <io.h>
int main()
{
unsigned long i;
P4DIR |= 0x0e; /* set P4.1-3 to output direction */
P4OUT = 0; /* set the three LEDs to off */
i = 123;
if (i == 123)
P4OUT |= 0x04; /* turn on P4.2 */
for(; i<10000; i++)
{
if ((i%1000)==0)
P4OUT ^= 0x02; /* toggle P4.1 */
}
if (i == 10000)
P4OUT |= 0x08; /* turn on P4.3 */
LPM0;
}
What I would expect to see is that P4.1 LED toggles 10 times, and P4.2
and P4.3 LEDs turn and stay on. I would expect to see this behavior if i
is on the stack or in a register. I do see the expected behavior if I
declare i as a global variable. I don't see the expected behavior with
the above program. With the above program none of the LEDs turn on at
all.
Attached is the .lst file. I don't know assembler and I'm new to the
msp430 so I appreciate the help. I don't even understand the first line:
31 0000 3140 0000 mov #(__stack+4), r1
What is at stack+4? Shouldn't the stack be growing 'down'?
(gdb) p &i
$1 = (long unsigned int *) 0x3104
(gdb) p i
$2 = 1518354610
(gdb) x /x 0x3100
0x3100 <_reset_vector__>: 0x31004031
(gdb) x /x 0x3104
0x3104 <__low_level_init>: 0x5a8040b2
(gdb)
The command I used to generate the lst file is as follows:
msp430-gcc -c -g -O0 -W -Wa,-a,-ad -mmcu=msp430x2618
-I/opt/mspgcc4/msp430-gcc-4.4.2/include/ main.c > main.lst
Thanks for the comments so far.
Carl
On Mon, 2010-02-22 at 12:53 +0100, JMGross wrote:
> Chances are, that the optimizer has replaced the 'variable' by a register.
> There's no reason to reserve space on the stack, increase it there and then
> clean up the stack if the return value needs to be in R15 for return anyway.
> Also, since the scope of the variable is limited to the function and the
> address(!) of the variable is never forwarded anywhere, declaring it volatile
> has no effect at all.
> I _think_ there is a compiler flag to disable register variables. But
> normally, the compiler uses them, in any optimisation stage.
>
> My guess is that GDB gets the symbol, but cannot handle it properly and just
> assumes it somewhere at the start of .text. Or the linker has put it at start
> of .txt since it isn't used anywhere.
> A look into the .lst file should make things clear.
> This is always my reference if something goes wrong.
> My bet is that this address is never accessed anywhere and all is done
> directly in a register.
>
> JMGross
>
> ----- Ursprüngliche Nachricht -----
> Von: Carl
> An: GCC for MSP430 - http://mspgcc.sf.net
> Gesendet am: 20 Feb 2010 22:16:22
> Betreff: Re: [Mspgcc-users] stack variable in flash?
>
> If I declare it as a global it is where I expect at the beginning of RAM
> (0x1100). But if it is on the stack in the main function, even if I
> declare it volatile, use asm(""); // __asm__ __volatile__ (""); or pass
> it to a function, the variable address is in flash at 0x3102. It doesn't
> make sense.
>
> Now if I use the program:
>
> #include <io.h>
>
> int f1()
> {
> volatile int i = 5;
> i += 2;
> return i;
> }
>
> int main()
> {
> while(1)
> {
> f1();
> }
> }
>
> The variable i address is on the stack in RAM at 0x30f8 which makes
> sense. But if I change the compile option to use -02. Then the function
> stack variable, i, goes back to being located in flash at 0x3102. And
> it's value is not initialed to 5 nor is 2 added to it. The value of i is
> constant = 12544 (0x3100.)
>
>
> ------------------------------------------------------------------------------
> Download Intel® Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> Mspgcc-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
GAS LISTING /tmp/ccoBrKZM.s page 1
1 .file "main.c"
2 .arch msp430x2618
3
4 /* Hardware multiplier registers: */
5 __MPY=0x130
6 __MPYS=0x132
7 __MAC=0x134
8 __MACS=0x136
9 __OP2=0x138
10 __RESLO=0x13a
11 __RESHI=0x13c
12 __SUMEXT=0x13e
13
21 .Ltext0:
22 .p2align 1,0
23 .global main
25 /***********************
26 * Function `main'
27 ***********************/
28 main:
29 .LFB0:
30 .LM1:
31 0000 3140 0000 mov #(__stack+4), r1
32 0004 0441 mov r1, r4
33 .LCFI0:
34 /* prologue ends here (frame size = 4) */
35 .L__FrameSize_main=0x4
36 .L__FrameOffset_main=0x10
37 .LM2:
38 0006 5F42 1E00 mov.b &0x001E, r15
39 000a 7FD0 0E00 bis.b #14, r15
40 000e C24F 1E00 mov.b r15, &0x001E
41 .LM3:
42 0012 C243 1D00 mov.b #0, &0x001D
43 .LM4:
44 0016 B440 7B00 mov #123, @r4
44 0000
45 001c 8443 0200 mov #lhi(123), 2(r4)
46 .LM5:
47 0020 B490 7B00 cmp #123, @r4
47 0000
48 0026 2020 jne .L3
49 0028 8493 0200 cmp #lhi(123), 2(r4)
50 002c 1D20 jne .L3
51 .LM6:
52 002e 5F42 1D00 mov.b &0x001D, r15
53 0032 6FD2 bis.b #4, r15
54 0034 C24F 1D00 mov.b r15, &0x001D
55 .LM7:
56 0038 173C jmp .L3
57 .L5:
58 .LM8:
59 003a 2C44 mov @r4, r12
60 003c 1D44 0200 mov 2(r4), r13
61 0040 3E40 E803 mov #1000, r14
62 0044 0F43 mov #lhi(1000), r15
63 0046 0A4E mov r14, r10
GAS LISTING /tmp/ccoBrKZM.s page 2
64 0048 0B4F mov r15, r11
65 004a B012 0000 call #__udivmodsi4
66 004e 0E93 cmp #0, r14
67 0050 0720 jne .L4
68 0052 0F93 cmp #lhi(0), r15
69 0054 0520 jne .L4
70 .LM9:
71 0056 5F42 1D00 mov.b &0x001D, r15
72 005a 6FE3 xor.b #2, r15
73 005c C24F 1D00 mov.b r15, &0x001D
74 .L4:
75 .LM10:
76 0060 9453 0000 add #1, @r4
77 0064 8463 0200 addc #lhi(1), 2(r4)
78 .L3:
79 0068 2E44 mov @r4, r14
80 006a 1F44 0200 mov 2(r4), r15
81 006e 3E80 1027 sub #10000, r14
82 0072 0F73 subc #lhi(10000), r15
83 0074 E22B jlo .L5
84 .LM11:
85 0076 B490 1027 cmp #10000, @r4
85 0000
86 007c 0820 jne .L6
87 007e 8493 0200 cmp #lhi(10000), 2(r4)
88 0082 0520 jne .L6
89 .LM12:
90 0084 5F42 1D00 mov.b &0x001D, r15
91 0088 7FD2 bis.b #8, r15
92 008a C24F 1D00 mov.b r15, &0x001D
93 .L6:
94 .LM13:
95 /* #APP */
96 ; 17 "main.c" 1
97 008e 32D0 1000 bis #16, r2
98 ; 0 "" 2
99
100 /* epilogue: frame size = 4 */
101 .LM14:
102 /* #NOAPP */
103 0092 2152 add #4, r1
104 .LCFI1:
105 0094 3040 0000 br #__stop_progExec__
106 .LFE0:
107 .Lfe1:
109 ;; End of function
110
142 .Letext0:
GAS LISTING /tmp/ccoBrKZM.s page 3
DEFINED SYMBOLS
*ABS*:00000000 main.c
/tmp/ccoBrKZM.s:5 *ABS*:00000130 __MPY
/tmp/ccoBrKZM.s:6 *ABS*:00000132 __MPYS
/tmp/ccoBrKZM.s:7 *ABS*:00000134 __MAC
/tmp/ccoBrKZM.s:8 *ABS*:00000136 __MACS
/tmp/ccoBrKZM.s:9 *ABS*:00000138 __OP2
/tmp/ccoBrKZM.s:10 *ABS*:0000013a __RESLO
/tmp/ccoBrKZM.s:11 *ABS*:0000013c __RESHI
/tmp/ccoBrKZM.s:12 *ABS*:0000013e __SUMEXT
/tmp/ccoBrKZM.s:28 .text:00000000 main
UNDEFINED SYMBOLS
__stack
__udivmodsi4
__stop_progExec__