3

I wrote a small program using NASM and try to run it, but I got an error "Segmentation fault". I use Linux (Debian) and 64-bit processor.

My program:

global _start

section .text

_start: mov eax, 16
    add eax, 24

How I assemble and link:

nasm -f elf main.asm 
ld -m elf_i386 main.o -o main
./main

Result when run:

Segmentation fault

I also tried assembling and linking this way:

nasm main.asm && sudo chmod +x main
./main 

And get this error:

bash: ./main: cannot execute binary file: Exec format error

Why am I getting a segmentation fault? And how can I fix it?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
No Name
  • 39
  • 1
  • 6
    you don't use `int 0x80` to do an exit system call to exit your program so it eventually crashes after reading beyond the end of your program in memory. Try adding this to bottom `mov eax,1` `xor ebx, ebx` `int 0x80` . _EBX_ is the return value from the program (I just set it to 0 in this example). _EAX_=1 is the syscall number for `sys_exit`. See this 32-bit [syscall table](http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html) . The second thing you tried is plain wrong. Assemble/link the original way for 32-bit programs. – Michael Petch Jul 03 '16 at 20:24
  • Your program has to include an exit to the operating system. – DisappointedByUnaccountableMod Jul 03 '16 at 21:01
  • @MichaelPetch please move your comment to the answer. – pah Jul 04 '16 at 02:14

0 Answers0