$ cat foo.s
.code32
.section .data
output:
    .asciz "The Value is %s\n"
values:
    .int 10, 15, 20,25, 30, 35, 40, 45, 50, 55, 60
.section .text
.globl main
main:
    movl    $0, %edi
loop:
    movl    values(, %edi, 4), %eax
    pushl   %eax
    pushl   $output
    call    printf
    addl    $8, %esp
    inc %edi
    cmpl    $11,    %edi
    jne loop
    movl    $0, %ebx
    movl    $1, %eax
    int $0x80

如果我使用$gcc-m32-gstabs-ofoo foo.s编译此文件,程序将关闭fault,当我在gdb中运行它时,输出是:
程序接收信号SIGSEGV,分段故障。
vfprintf()中的0xf7e56e29,来自/lib/i386 linux gnu/libc.so.6

最佳答案

你的以nul结尾的字符串在哪里?格式说明符需要附带指向以nul结尾的字符串的指针。
如果不提供一个,%s无论如何都会将堆栈上的数据视为指针,并在将非指针值视为指针时导致segfault。

关于linux - 在printf的汇编程序段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20830937/

10-11 07:12