I am trying to accept inputs in a dynamically allocated int array but it gives a segmentation fault.
I checked the address returned by calloc. It is valid.
Also I used scanf to accept the value of size of the array and it worked fine.
.text
int_str: .asciz "%d"
.global main
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
/* accept the value of data here
* this one works fine
*/
/* allocate memory */
movq $4, %rdi
movslq -4(%rbp), %rsi
call calloc@plt
movq %rax, -12(%rbp)
/* for loop starts here */
movq $0, %r15
start_loop_1:
cmpl -4(%rbp), %r15d
jnl end_loop_1
leaq int_str(%rip), %rdi
movq -12(%rbp), %rsi
leaq (%rsi, %r15, 4), %rsi
pushq %r15
xorl %eax, %eax
call scanf@plt // <========= gives SIGSEGV here
popq %r15
incq %r15
jmp start_loop_1
end_loop_1:
movq -12(%rbp), %rdi
call free@plt
addq $16, %rsp
popq %rbp
ret