Search code examples
linuxassemblyx86-64att

Segmentation error upon calling scanf in x86_64 AT&T


I am quite new to Assembly and I am trying to create a program that uses scanf to receive a number from the user. It then outputs "Result: (the number)" I keep getting a segmentation error upon running the code. This is the code I have got now:

.global main
mystring: .asciz"input\n"
formatstring: .asciz" %d"
resultstring: .asciz "Result: %ld\n"

main:
    movq    $0, %rax    
    movq    $mystring, %rdi
    call    printf
    call    inout
    movq    $0, %rax
    movq    $resultstring, %rdi
    call    printf
    jmp end

inout:
    pushq   %rbp
    subq    $8, %rsp
    leaq    -8(%rbp), %rsi
    movq    $formatstring, %rdi
    movq    $0, %rax
    call    scanf
    popq    %rbp    
    ret


end:
    movq    $0, %rdi
    call    exit

I suspect there is something wrong with the 'inout' method. Any solutions to make this program working?


Solution

  • leaq    -8(%rbp), %rsi
    

    In this instruction you are referring to the %rbp register but you forgot to actually initialize it!