Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why am I getting segmentation fault?

Status
Not open for further replies.

alzamabar

Programmer
Feb 28, 2010
2
GB
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 <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
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]
 
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.
 
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?
 
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.

Thanks.

M.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top