Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Why am I getting segmentation fault?

Why am I getting segmentation fault?

Why am I getting segmentation fault?

Hi all,

I'm an Assembly newby and am studying Jeff Duntemann's book on programming Assembly on Linux. As much as I love Assembly and I have to learn it if I want to do anything significant in IT since it's part of a journey, I am finding difficult to get started because there are so many variables.

My environment is Ubuntu (Linux Debian) 9.10 64-bit and I am using NASM as compiler. Following Duntemann's suggestion, I created a sort of "sandbox" program where I can simply run few instructions and see the effect that these have on registers.

This is the simple program I'm writing:

;  Executable name :
;  Version         : 1.0
;  Created date    :
;  Last update     :
;  Author          : Marco Tedone
;  Description     : A template to create sandbox programs
;  Build using these commands:
;    nasm -f elf64 -g -F stabs <your-asm-name>.asm
;    ld -o <your-asm-name> <your-asm-name>.o

SECTION .data            ; Section containing initialised data
SECTION .text            ; Section containing code

global _start            ; Linker needs this to find the entry point!
_start:              ; Write your instructions between the two noops
mov eax,0
inc eax
mov ebx,eax

SECTION .bss            ; Section containing uninitialised variables


The program compiles fine but when I run it I get segmentation fault. Why?

Also here there is another program (which actually does something useful) which runs fine:

;  Executable name : EATSYSCALL
;  Version         : 1.0
;  Created date    : 1/7/2009
;  Last update     : 2/18/2009
;  Author          : Jeff Duntemann
;  Description     : A simple program in assembly for Linux, using NASM 2.05,
;    demonstrating the use of Linux INT 80H syscalls to display text.
;  Build using these commands:
;    nasm -f elf -g -F stabs eatsyscall.asm
;    ld -o eatsyscall eatsyscall.o

SECTION .data            ; Section containing initialised data
    EatMsg: db "Eat at Marco's  !",10
    EatLen: equ $-EatMsg    
SECTION .bss            ; Section containing uninitialized data    

SECTION .text            ; Section containing code

global     _start            ; Linker needs this to find the entry point!
    nop            ; This no-op keeps gdb happy...
    mov eax,4        ; Specify sys_write call
    mov ebx,1        ; Specify File Descriptor 1: Standard Output
    mov ecx,EatMsg        ; Pass offset of the message
    mov edx,EatLen        ; Pass the length of the message
    int 80H            ; Make kernel call

    MOV eax,1        ; Code for Exit Syscall
    mov ebx,0        ; Return a code of zero    
    int 80H            ; Make kernel call

RE: Why am I getting segmentation fault?

The only difference I can see is in the code that runs, you have a space between global and _start, where you do not in the code that doesn't run.

RE: Why am I getting segmentation fault?

Um, you need to terminate program properly by calling exit system call.
Use same 3 lines at end as in your second example.
What are you trying to achieve actually?

RE: Why am I getting segmentation fault?

Hi guys, thanks for your answer. Actually the author explained me that the "sandbox" code above was incomplete, since it missed the three lines to return control to the OS. What I am trying to achieve is to have a kind of "blueprint" program which I can use as a template to create other, simple programs (e.g. if I want to see the value in some registers, etc). I solved the issue now.



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close