<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x
    查看: 17184|回復: 1
    打印 上一主題 下一主題

    [提問] 嵌入式ARM開發環境下,設置堆棧指針和清理BSS段的意義

    [復制鏈接]
    跳轉到指定樓層
    樓主
    發表于 2018-5-18 09:30:48 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
      以前稍微寫過操作系統上的C程序,感受不出來:BSS段,堆棧的意義。到了在單片機上寫程序也沒有考慮這些問題。但是到了ARM上環境似乎沒有那么簡單了,C的環境要自己來創建,不然就不能用。這也深刻的感受到了C語言中原來難以理解的概念。

    裸機建立C語言環境-設置堆棧指針

    這個是使用C語言的首要條件,不過這個就是指定一個sp指針就可以了,很簡單的。ldr sp, =4096。

    裸機建立C語言環境-清理BSS段

    如果C語言中用到的全局變量或者靜態變量,這個編譯的時候是把它們放到了BSS段,這個段在內存中。怎么建成的?手動寫一個鏈接腳本,添加__bss_start __bss_end變量來表示BSS段的開始和結束。如下:
    SECTIONS {
    . = 0x00000000;
    .text : { *(.text) }
    .rodata ALIGN(4) : AT((LOADADDR(.text)+SIZEOF(.text)+3)&~(0x03)) {*(.rodata*)}
    .data ALIGN(4) : AT((LOADADDR(.rodata)+SIZEOF(.rodata)+3)&~(0x03)) { *(.data) }
    __bss_start = .;
    .bss ALIGN(4) : { *(.bss) *(COMMON) }
    __bss_end = .;
    }

    這樣在應用程序中清理__bss_start到__bss_end之間內在中的內容。這樣全局變量就可以用了,否則會出現異常。我遇到的具體表現為:全局變量的值無法更改。代碼可以學習u-boot中匯編方法清理:
    /*
    * These are defined in the board-specific linker script.
    */
    .globl _bss_start
    _bss_start:
    .word __bss_start
    .globl _bss_end
    _bss_end:
    .word __bss_end
    /*
    * 清BSS段
    */
    clear_bss:
    ldr r0, _bss_start /* find start of bss segment */
    ldr r1, _bss_end /* stop here */
    mov r2, #0x00000000 /* clear */
    clbss_l:str r2, [r0] /* clear loop... */
    add r0, r0, #4
    cmp r0, r1
    ble clbss_l
    mov pc, lr
    /* end_of clear_bss */
    也可以用C語言來實現:
    void clean_bss(void)
    {
    extern int __bss_start, __bss_end;
    int *p = &__bss_start;
    for (; p < &__bss_end; p++)
    *p = 0;
    }

    總結:就是往這段內存中寫0.

    以下課程可免費試聽C語言、電子、PCB、STM32、Linux、FPGA、Python、安卓等。
    想學習的你和我聯系預約就可以免費聽課了。宋工Q35--24-65--90-88   Tel/WX:173--17--95--19--08

    您需要登錄后才可以回帖 登錄 | 立即注冊

    本版積分規則

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷