0

I am very new at assembly and I am not sure how to call scanf.

I am creating a function that has 2 arguments:

max_inputs: maximum number of inputs user can enter

value_to_stop: a number that, when entered, stops the user from inputting any more integers

Let's not worry about what the function does, just know that it has two arguments.

I am aware that calling a function uses rdi and rsi to store the first two arguments. When I call scanf inside of a function, I am assuming it would also check rdi and rsi. I am also aware that before calling scanf, rax needs to be 0, rdi is the string format, rsi is the address that stores the input. And apparently the stack has to be in multiples of 16 bytes, whatever that means.

How do I keep the stack to be in multiples of 16? Is this how I would call scanf? I know this is only partially correct. Is it possible to store the user's input without creating another global variable?

.data 
stringFormat: .string "%d"

.global functionName
functionName:
.
.
.
push %rdi # for preservation of max_inputs
push %rsi # for preservation of value_to_stop

mov $0, %rax
mov $stringFormat, %rdi
mov $0, %rsi
call scanf
.
.
.

%rsi would hold the integer. %rax would hold the number of inputs read

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
  • `scanf` puts the result into memory, no matter if you call it from C or assembly. That said, the variable can be local, does not need to be a global. You keep the stack pointer aligned by only changing it by a multiple of 16, including any return addresses used by a `call`. – Jester Feb 14 '22 at 12:10

0 Answers0