gpt4 book ai didi

c - movq 指令出现段错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:22 27 4
gpt4 key购买 nike

考虑以下短程序。

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语法中必须在立即数前加上$。我找到了提醒 here仔细检查 AT&T 语法时。

asm("movq $100000000, %rcx;"
"startofloop: ; "
"sub $0x1, %rcx; "
"jne startofloop; ");

movq 0x5F5E100, %rcx(数字上没有$)是从绝对地址0x5F5E100

加载

关于c - movq 指令出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36023681/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com