2
我有一些靜態的,常量的數據,我需要能夠在運行時檢索。 我需要將這些數據放在哪裏,我該如何訪問它?放哪裏以及如何訪問靜態,常量數據?
我試過把數據放在.text
和.data
中,並且使用ld r24, X
。我也嘗試使用GDB的print
命令。但是,通過所有這些方法,我總是看到0的結果。
嘗試1:.data
:
Sections:
Idx Name Size VMA LMA File off Algn
0 .data 00000010 00800100 000000d2 00000146 2**0
CONTENTS, ALLOC, LOAD, DATA
1 .text 000000d2 00000000 00000000 00000074 2**1
CONTENTS, ALLOC, LOAD, READONLY, CODE
2 .stab 000006cc 00000000 00000000 00000158 2**2
CONTENTS, READONLY, DEBUGGING
3 .stabstr 00000054 00000000 00000000 00000824 2**0
CONTENTS, READONLY, DEBUGGING
Contents of section .data:
800100 01010101 01000000 00000000 00000100 ................
Disassembly of section .text:
...
c2: a0 50 subi r26, 0x00 ; 0
c4: bf 4f sbci r27, 0xFF ; 255
c6: 8c 91 ld r24, X
在運行時,這給了我
=> 0x000000c6 <_ZN8external6decode17hbbb21fc0c1cdbaf3E+18>: 8c 91 ld r24, X
(gdb) i r r26 r27
r26 0x3 3
r27 0x1 1
(gdb) ni
0x000000c8 in external::decode::hbbb21fc0c1cdbaf3()
=> 0x000000c8 <_ZN8external6decode17hbbb21fc0c1cdbaf3E+20>: 08 95 ret
(gdb) i r r24
r24 0x0 0
(gdb) p *0x103
$8 = 0
(gdb) p *0x800103
$9 = 0
嘗試2:.text
:
Sections:
Idx Name Size VMA LMA File off Algn
0 .data 00000000 00800100 000000e1 00000155 2**0
CONTENTS, ALLOC, LOAD, DATA
1 .text 000000e1 00000000 00000000 00000074 2**1
CONTENTS, ALLOC, LOAD, READONLY, CODE
2 .stab 000006cc 00000000 00000000 00000158 2**2
CONTENTS, READONLY, DEBUGGING
3 .stabstr 00000054 00000000 00000000 00000824 2**0
CONTENTS, READONLY, DEBUGGING
Disassembly of section .text:
...
c2: ae 52 subi r26, 0x2E ; 46
c4: bf 4f sbci r27, 0xFF ; 255
c6: 8c 91 ld r24, X
...
000000d2 <_etext>:
d2: 01 01 movw r0, r2
d4: 01 01 movw r0, r2
d6: 01 00 .word 0x0001 ; ????
...
e0: 01 00 Address 0x00000000000000e0 is out of bounds.
.word 0xffff ; ????
然後牛逼運行:
0x000000c6 in external::decode::hbbb21fc0c1cdbaf3()
=> 0x000000c6 <_ZN8external6decode17hbbb21fc0c1cdbaf3E+18>: 8c 91 ld r24, X
(gdb) i r r26 r27
r26 0xd5 213
r27 0x0 0
(gdb) ni
0x000000c8 in external::decode::hbbb21fc0c1cdbaf3()
=> 0x000000c8 <_ZN8external6decode17hbbb21fc0c1cdbaf3E+20>: 08 95 ret
(gdb) i r r24
r24 0x0 0
(gdb) p *0xd5
$1 = 0
(gdb) p *0x8000d5
$2 = 0
放置數據的部分不應有所作爲。嘗試用C編譯器編譯一個類似的程序,並觀察它如何訪問這些數據。 – fuz
@fuz:我添加了一個自我回答;使用'lpm',我能夠得到這個工作,但只有方法2。你可以擴展爲什麼選擇章節應該不重要嗎? – Cactus
看來,AVR有代碼和數據分開的地址空間。你的程序是否包含通常的存根將數據段複製到RAM中? – fuz