movq 指令出现段错误?
Segfault on movq instruction?
考虑以下短程序。
int main(){
asm("movq 0x5F5E100, %rcx;"
"startofloop: ; "
"sub 0x1, %rcx; "
"jne startofloop; ");
}
这个程序编译得很好,但是当它是 运行 时,它在初始 movq
指令上出现段错误。
我肯定遗漏了一些明显的东西,但我希望这里有人能为我指出。
我 运行正在使用 Debian 8,内核为 3.16.0-4-amd64,以防相关。
为了将来参考,这是编译器生成的内容。
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
#APP
# 2 "asm_fail.c" 1
movq 0x5F5E100, %rcx;startofloop: ; sub 0x1, %rcx; jne startofloop;
# 0 "" 2
#NO_APP
原来是太久没写asm了,忘记了AT&T语法中必须在立即数前加上$
。我在仔细检查 AT&T 语法时发现了提醒 here。
asm("movq 0000000, %rcx;"
"startofloop: ; "
"sub [=10=]x1, %rcx; "
"jne startofloop; ");
movq 0x5F5E100, %rcx
(数字上没有$
)是从绝对地址加载0x5F5E100
考虑以下短程序。
int main(){
asm("movq 0x5F5E100, %rcx;"
"startofloop: ; "
"sub 0x1, %rcx; "
"jne startofloop; ");
}
这个程序编译得很好,但是当它是 运行 时,它在初始 movq
指令上出现段错误。
我肯定遗漏了一些明显的东西,但我希望这里有人能为我指出。
我 运行正在使用 Debian 8,内核为 3.16.0-4-amd64,以防相关。
为了将来参考,这是编译器生成的内容。
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
#APP
# 2 "asm_fail.c" 1
movq 0x5F5E100, %rcx;startofloop: ; sub 0x1, %rcx; jne startofloop;
# 0 "" 2
#NO_APP
原来是太久没写asm了,忘记了AT&T语法中必须在立即数前加上$
。我在仔细检查 AT&T 语法时发现了提醒 here。
asm("movq 0000000, %rcx;"
"startofloop: ; "
"sub [=10=]x1, %rcx; "
"jne startofloop; ");
movq 0x5F5E100, %rcx
(数字上没有$
)是从绝对地址加载0x5F5E100