1. 程式人生 > >韋東山ARM第一期作業(四)程式碼重定位

韋東山ARM第一期作業(四)程式碼重定位

文章目錄


01 - 作業所在路徑

  ARM裸機1期加強版\原始碼文件圖片\文件圖片\第013課_程式碼重定位

02 - 作業描述

2.1 - 作業1

  使用printf函式列印以下資訊:

volatile unsigned int *dest = (volatile unsigned int *
)&__code_start; volatile unsigned int *end = (volatile unsigned int *)&__bss_start; volatile unsigned int *src = (volatile unsigned int *)0;

  a. 列印dest, end, src的值
  b. 列印dest, end, src所指向的地址的值

2.2 - 作業2

  修改連結指令碼,把程式重定位到0x32000000,再觀察上述列印結果

03 - 作業解答

3.1 - 作業1解答

使用printf函式列印以下資訊:
volatile unsigned int *dest = (volatile unsigned int *)&__code_start;
volatile unsigned int *end = (volatile unsigned int *)&__bss_start;
volatile unsigned int *src = (volatile unsigned int *)0;
a. 列印dest, end, src的值
b. 列印dest, end, src所指向的地址的值

  在main()中新增程式碼

volatile unsigned int *dest = (volatile unsigned int *)&code_start;
volatile unsigned int *end = (volatile unsigned int *)&bss_start;
volatile unsigned int *src = (volatile unsigned int *)0;
	
printf("dest=0x%x, end=0x%x, src=0x%x",dest,end,src);
puts("\n\r");
printf("*dest=0x%d, *end=0x%d, *src=0x%d"
,*dest,*end,*src); puts("\n\r");

  輸出:

dest=0x30000000, end=0x30001310, src=0x0
*dest=0x-476052397, *end=0x0, *src=0x-476052397

3.2 - 作業2解答

修改連結指令碼,把程式重定位到0x32000000,再觀察上述列印結果

  修改連結指令碼到0x32000000後的輸出

dest=0x32000000, end=0x32001310, src=0x0
*dest=0x-476052397, *end=0x0, *src=0x-476052397

  由結果可知,ls連結腳步中的變數,在C語言中使用&取出變數的值,而把這個值當成地址操作後,就會直接操作sdram