0

I can't figure out how to call scanf properly. My current code adds 0x30 to my answer for some reason.

This is my current code

.file "get_int.s"
.section .rodata
.text
.globl get_int
    .type get_int, @function
scanf_line:
    .string "Test %d"
get_int:
    pushq %rbp          #save caller's rbp
    movq %rsp, %rbp     #set function's frame pointerr

    movq $0, %rax            #initalize return to 0
    movq $scanf_line, %rdi   #push scanf_line into 1st arg
    call scanf               #call scanf C function

exit:

    leave
    ret
.size get_int, .-get_int

If I were to enter decimal 5 I would get back 0x35 or decimal 53 in the rsi register.

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
Nathan
  • 1
  • You didn't pass a 2nd arg. If you compiled `scanf("%d");` with a C compiler, you'd get warnings about not matching the format string... The only reason it doesn't segfault is that RSI probably still holds a valid pointer when you call, probably `argv` from main. – Peter Cordes Nov 04 '21 at 01:47
  • Also, your comments aren't accurately describing the parts that are correct. Setting AL before a call isn't the return value, it's the number of args passed in XMM registers. (Always 0 for scanf since it takes pointers, not floats.) And `mov` to a register isn't a push. The first 6 args go in registers, not on the stack, so "pushed" isn't a good description. – Peter Cordes Nov 04 '21 at 01:49

0 Answers0