I am trying to get scanf to work. I've been looking at past posts and I am not sure what I am doing wrong. I am able to enter a number but it always segfaults before the next line executes. I think the issue lies on me not understanding how to align the stack to be in multiples of 16.
I keep getting "no such file or directory" a thousand times before it lets the user ask for input. I am not sure if this relates to the issue but just something to point out.
Keep in mind, I am also calling scanf in a loop.
I currently want to store 3 things in the stack: 2 arguments (of the current function, not scanf's) + 1 integer (place to store user input).
Here is what I have so far:
sub $48, %rsp # allocate space for rdi, rsi, input in multiples of 16
mov %rdi, 24(%rsp) # store first arg
mov %rsi, 8(%rsp) # store second arg
push $0 # %rsp stores the user input, initialized as 0.
...
loop:
mov $0, %rax
mov $stringFormat, %rdi
mov %rsp, %rsi
call scanf
...
Here is what the stack looks like before scanf
(gdb) x/12w $rsp
0x7fffffffdfa0: 0 0 4199021 0
0x7fffffffdfb0: 800 0 4198944 0
0x7fffffffdfc0: 5 0 4198480 0