I have this assembly code which gives me a segmentation fault and I do not know why.
.global _main
_main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $0, -4(%rbp)
callq _getint
pushq %rax
callq _getint
pushq %rax
leaq str(%rip), %rdi
popq %rsi
movb $0, %al
callq _printf
leaq str(%rip), %rdi
popq %rsi
movb $0, %al
callq _printf
xorl %eax, %eax
addq $16, %rsp
popq %rbp
retq
The _getint function is the assembly you would get with gcc if you compiled:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long getint()
{
char *end;
char buf[100];
long res;
do {
if (!fgets(buf, sizeof(buf), stdin)) break;
buf[strcspn(buf, "\n")] = '\0';
res = strtol(buf, &end, 10);
} while (end != buf + strlen(buf));
return res;
}
I would really appricieate help on this topic.