嘗試使用行內匯編將變數保存在 arm 暫存器中。
unsigned int lma_offset = 0x1234; // typically calculated, hardcoded for example
__asm volatile ("MOV R10, %[input]"
: [input] "=r" (lma_offset)
);
在我的情況下,這會將 lma_offset更改為 0xd100,而不是設定暫存器。我究竟做錯了什么?
PS:當我宣告 lma_offset 因為const它給出了編譯器錯誤,因為 lma_offset 被用作輸出。所以很明顯出了點問題,我仍然找不到正確的語法。
uj5u.com熱心網友回復:
根據 Erics 的評論,供將來參考
const unsigned int lma_offset = 0x10000;
__asm__ volatile ("MOV R10, %[input]"
: // no C variable outputs
: [input] "r" (lma_offset)
: "R10" // tell the compiler R10 is modified
);
使用 double:并用“r”替換“=r”確實可以解決問題。
也可以要求編譯器在 R10 中為 asm 陳述句提供該常量,方法是使用暫存器區域變數強制"r"輸入為 pick r10。(那么我們可以省略多余的mov r10, r10)。
register unsigned r10 __asm__("r10") = lma_offset; // picks r10 for "r" constraints, no other *guaranteed* effects
__asm__ volatile ("@ inline asm went here" // empty template, actually just a comment you can see if looking at the compiler's asm output
: // no C variable outputs
: [input] "r" (lma_offset)
: // no clobbers needed
);
將暫存器寫入某個輸出 C 變數時,會導致
unsigned int lma_offset = 0x0;
__asm__ volatile ("MOV %[output], R11"
: [output] "=r" (lma_offset)
// no clobbers needed; reading a register doesn't step on the compiler's toes
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/472526.html
