DSUK
DSUK

Reputation: 284

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

if i compile this using $ gcc -m32 -gstabs -ofoo foo.s, the program will segfault and when i run it in gdb the output is:

Program received signal SIGSEGV, Segmentation fault. 0xf7e56e29 in vfprintf () from /lib/i386-linux-gnu/libc.so.6

Upvotes: 1

Views: 473

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283634

Where's your NUL-terminated string? A %s format specifier needs to be accompanied by a pointer to a NUL-terminated string.

If you don't provide one, printf will treat the data on the stack as a pointer anyway, and cause a segfault when it treats a non-pointer value as a pointer.

Upvotes: 1

Related Questions