[心得] bss section

作者: descent (「雄辯是銀,沉默是金」)   2016-12-14 20:16:28
之前在《bss section (0) ( https://goo.gl/jlDiYt )》留下來的問題要在這篇解答。
先來看看 b.c, 這程式很簡單, b.c L6 int i; 會放在 bss, p() 會把 c=i+1 用
itoa() 轉成 string 印出。預期得到 1, 因為在 C 中, 教科書都告訴我們 int i; 雖然
沒有初值, 但 c 會初始為 0, 這不是憑空得來的, 裡頭有魔法, 讓我們來惡搞一下 bss
吧!
這是 bare-metal 程式, 和一般程式的編譯以及執行方式有些不同。
b.c
1 asm(".code16gcc\n");
2
3 typedef unsigned char u8;
4 typedef unsigned int u32;
5
6 int i;
7 char* itoa(int n, char* str);
8
9 void print(const char *s)
10 {
11 while(*s)
12 {
13 __asm__ __volatile__ ("int $0x10" : : "a"(0x0E00 | *s), "b"(7));
14 s++;
15 }
16 }
17
18 int p()
19 {
20 int c=i+1;
21 char arr[6]="a";
22 char *s = arr;
23
24 //const char *str="test";
25 //volatile u8 *video_addr = (u8*)0xB8000;
26 //asm("movw $0xb000 %gs");
27 //*video_addr = 'a';
28 s = itoa(c, s);
29 print(s);
30 return c;
31 }
32
33 #if 1
34 char* itoa(int n, char* str)
35 {
36 char digit[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
37 char* p=str;
38 char* head=str;
39 int radix = 10;
40
41 // if(!p || radix < 2 || radix > 36)
42 // return p;
43 if (n==0)
44 {
45 *p++='0';
46 *p=0;
47 return str;
48 }
50 if (radix == 10 && n < 0)
51 {
52 *p++='-';
53 n= -n;
54 }
56 while(n)
57 {
58 *p++=digit[n%radix];
59 n/=radix;
60 }
61 *p=0;
63 for (
作者: wtchen (沒有存在感的人)   2016-12-14 20:20:00
請問一下,上次看了你用sd開機的code,請問你SDRAMinitialization的部份放在哪?不然要怎麼從sd載入到SDRAM
作者: descent (「雄辯是銀,沉默是金」)   2016-12-14 21:42:00
stm32用sram,不需要初始化就可以使用
作者: wtchen (沒有存在感的人)   2016-12-14 22:01:00
感謝
作者: art1 (人,原來不是人)   2016-12-14 22:30:00
程式沒有眼睛,請問要怎麼看見全域變數或看不見區域變數?

Links booklink

Contact Us: admin [ a t ] ucptt.com