[NEWBY] - 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:

[code]
; 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 .asm
; ld -o .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
nop
mov eax,0
inc eax
mov ebx,eax
nop

SECTION .bss ; Section containing uninitialised variables
[/code]

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:

[code]
; 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!

_start:
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

[/code]

Could anybody tell me what I'm doing wrong? Please note that the first program gives a segmentation fault even if i write a simple instruction such as mov eax,4 or something like that. Does the program need to do something useful not to get a segmentation error?

Thanks.

M.

Comments

  • Maybe because you didn't have the program terminate properly?

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

    equivalent to DOS

    [code]
    mov ax, 4c00h ;return to DOS with return of zero
    int 21h

    [/code]

    I think I have that same book. It is a good primer for assembler. But it uses NASM which IRC, is not being updated and is old. Use FASM. It is 10 times better.
  • Thanks, but actually the author answered. The reason is that the example was just that...An example and later in the book he explains why the program would give segmentation fault and how to correct the problem which is, as you correctly put it, a lack of instructions to return to the OS. I'm actually reading the second version of that book, which was printed just now and the programming is in Linux.

    Thanks for your help,

    Marco
  • Yea I have the 2nd edition too. I didn't bother with the Linux stuff much except the part on calling C function from assembler. That was was handy info.
  • Are you into OS and stuff? I'm studying assembly and want to go onto C to learn operating systems because then I want to write my own and my own programming language. It's a long path, but it's the dream of my life!
  • I haven't delved into system programming much. But good luck in your endeavors.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories