[英]assembly program segfaults at printf
$ 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
您的NUL終止的字符串在哪里? %s
格式說明符需要附帶一個指向NUL終止的字符串的指針。
如果不提供,則printf
無論如何都將堆棧上的數據視為指針,並且在將非指針值視為指針時會導致段錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.