0
我有幾個結構/聯合,例如: 使用UNION/struct成員爲GCC內聯彙編說法
union
{
uint8_t X;
struct __attribute__((packed, aligned(1)))
{
uint8_t A : 1;
uint8_t B : 1;
uint8_t C : 1;
uint8_t D : 1;
uint8_t E : 1;
uint8_t F : 1;
uint8_t G : 1;
uint8_t H : 1;
};
}DATA;
我需要訪問他們的內聯彙編的元素,例如:
asm volatile
(
"\
mov %1, %%eax \t\n\
inc %%eax \t\n\
mov $0, %0 \t\n\
"
:"=d"(u->X)
:"d"(temp)
);
應用程序段錯誤在最後一行。
mov (%rdx),%eax
inc %eax
mov $0x0,%dl
mov %dl,0x1(%rax)
我試圖創建一個鏡像指針,並通過它訪問結構,但它沒有效果,只是沒有段錯誤。所以我使用temp變量。是否有可能,或者我嘗試做一件奇怪的事情?
非常感謝,這是完全正確的。我通過向asm模板的clobber部分添加了eax來解決了這個問題 – pugnator
將一個已命名的寄存器移動到clobber列表中是「幾乎從不」正確的修復(還有其他方法 - 使用in/out操作數,「+ d」 (u-> X)',或者使用_aliases_/_early clobbers_,'「=&d」(u-> X):「0」(temp)')。你的代碼的結果,無論_how_結構化,在功能上都等同於'u-> X = 0;' - 你在那裏做了什麼? –