0

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

Programmerabc
  • 329
  • 2
  • 10
  • 1
    The `syscall` system call interface is for Linux, not for Windows. Are you running this under WSL? – Nate Eldredge Aug 28 '21 at 16:38
  • You do know that the `write` system call expects a *pointer* to the data to be written, not the data itself? You have to store the data somewhere in memory. And if you pass a pointer to a 64-bit integer, you'll get it written out as raw binary; if you want decimal or hex you have to write quite a bit more code to do the conversion. See https://stackoverflow.com/questions/13166064/how-do-i-print-an-integer-in-assembly-level-programming-without-printf-from-the and https://stackoverflow.com/questions/8031831/outputting-integers-in-assembly-on-linux. – Nate Eldredge Aug 28 '21 at 16:40
  • Btw, the semicolons at line end don't do anything; they are just parsed as empty comments. – Nate Eldredge Aug 28 '21 at 16:47
  • 2
    Another problem is that the recursive call to `factorial` clobbers registers rax and rbx. You can't treat registers like local variables. You have to save them to the stack if you want to use them again after recursing. – Nate Eldredge Aug 28 '21 at 16:53
  • @NateEldredge I have tried this both in wsl also but this still gives sementation fault – Programmerabc Aug 28 '21 at 17:04
  • 1
    I am not very familiar with WSL but you might want to explain how you are building and running it, in case there is a problem with that. Otherwise you have quite a few bugs and design problems as I noted above. I'd suggest getting a debugger working so that you can step through the code and see what it actually does. – Nate Eldredge Aug 28 '21 at 17:09
  • 1
    You may want to take a step-by-step approach: first write a program which just exits successfully. Then add printing "Hello, World!". Then add printing the value of a register in decimal. Then add computing Fibonacci. Then add computing factorial. If any of those fail, ask a separate question. – pts Aug 28 '21 at 17:33
  • (But instead of asking a separate question right away, look for an existing Q&A first. There are ones about each of those topics, e.g. [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894)) – Peter Cordes Aug 29 '21 at 15:12

0 Answers0