printf中的汇编程序段错误

时间:2013-12-29 22:44:27

标签: linux assembly x86

$ 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编译它,该程序将是段错误的,当我在gdb中运行它时输出是:

编程接收信号SIGSEGV,分段故障。 来自/lib/i386-linux-gnu/libc.so.6的vfprintf()中的0xf7e56e29

1 个答案:

答案 0 :(得分:1)

你的NUL终止字符串在哪里? %s格式说明符需要伴随指向NUL终止字符串的指针。

如果你没有提供,printf无论如何都会将堆栈上的数据视为指针,并在将非指针值视为指针时导致段错误。

相关问题