I have to do a very simple task in assembly: read input from the user (name) and then output this input.
The way I have to do it - to use function gets()
. I know that this function should never be used but teacher specified the task to do it using only gets
function.
The problem I face - whenever I call this function and enter my input it gives me Segmentation fault. I make buffer size equal to 24 bytes just for example.
It doesn't matter how much memory I give this function on stack - always the same mistake. One interesting thing: if I leaq -(k*24)(%rsp), %rsp
and then mov %rsp, %rdi
and then calling gets
will allow me to enter as many inputs as I want and never stop. It happens with only number 24 and any k
you want.
Why could it happen?
Thank you.
Function name
.string1:
.string "Please enter your name: "
.string2:
.string "Hello, %s"
.globl name
name:
push %rbx
push %rbp #calee saved regs
leaq -24(%rsp),%rsp # name_str[24]
mov %rsp, %rbp #save rsp
movq $.string1, %rdi
xorl %eax,%eax
call printf
movq %rsp, %rdi
call gets
movq $.string2, %rdi
mov %rax, %rsi
xorl %eax, %eax
call printf
ret
UPDATE1: Sorry for not mentioning earlier: I have main written in C
, it just simply calls the function:
#include <stdlib.h>
void name(void);
void main()
{
name();
return;
}
UPDATE2: I'm using Linux 64bit
and GCC 4.8.4
(quite old, I know)
UPDATE3: Using gcc -o exec main.c name.s
to link.