I am new to x86 assembly programming. Just to familiarize i wrote a small factorial function and and a function that prints the result. I am confused while running the program it throws segmentation fault.I have used nasm to compile it.Here is the code:
global _start
section .code
_start:
mov rax, 4;
call .factorial;
call .print;
.factorial:
cmp rax, 1;
je .return1
cmp rax, 0;
je .return1
dec rax;
call .factorial
imul rax, rbx;
mov rbx, rax;
inc rax;
ret
.return1:
mov rbx, 1;
.print:
mov edx, 4;
mov rsi, rbx
mov rax, 1;
mov rdi, 1;
syscall
mov rsi, 0;
mov rax, 60;
syscall
ret
section .data
section .bss
I am using kali-Linux under wsl 64-bit Here is the nasm command i used:
nasm program.asm -o program.obj
ld -o program program.obj
./program