Click here to Skip to main content
15,889,820 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
section .data
hell db "Hello",10
bye db "Bye",10

section .text

global _start
_start:
mov r10,1
cmp r10,1
je _hell

cmp r10,0
jne _bye

mov rax,60
mov rdi,0
syscall

_hell:
mov rax,1
mov rdi,1
mov rsi,hell
mov rdx,6
syscall
ret

_bye:
mov rax,1
mov rdi,1
mov rsi,bye
mov rdx,4
syscall
ret


the output that it gives:

Hello
Segmentation fault (core dumped)


What I have tried:

I have no clue why I am getting this error.Why is it giving the Segmentation fault (core dumped) error and how to fix it?

steps which I have followed to compile and run:

(compiling)$ nasm -f elf64 -o jump.o jump.asm
(linking)$ ld -o jump jump.o
(running)$ ./jump

English is not my native language excuse me please.
Posted
Updated 20-Dec-20 1:43am

Quote:
I have no clue why I am getting this error.Why is it giving the Segmentation fault (core dumped) error and how to fix it?

I fear you need to go to debugger, and for 'core dumped' I think it is postmortem debugger.
The first thing you will see is the position of crash, it help narrow the search for solution.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
[no name] 20-Dec-20 7:18am    
No vote for this, but sorry this becomes strange from your side.
Patrice T 20-Dec-20 7:36am    
Hi,
First, I am not Linux user and I don't know NASM.
When I have problem in my code, either in BASIC, ASM, C/C++ and other languages, the debugger have always been of great help, and it works since 40 years for me.

I recommend debugger because it is not in fashion these days. It looks like today newbies are learning programming without learning debugger, which I can't understand.

Can you elaborate "this becomes strange from your side" ?
[no name] 20-Dec-20 7:51am    
Exaclty the points you mentioned. "You are not Linux" and you don't know "Asm". So why you even then write a "very general debugger" suggestion and this as a solution? It is _no where_ a solution it is only a very general suggestion. Sorry this looks for me like rep hunting.
Patrice T 20-Dec-20 8:02am    
I see your point.
I done this answer because I think that even the general suggestion of debugger is valuable.
[no name] 20-Dec-20 8:10am    
Don't bother anyway about my intervention. As you should know me meanwhile more as one who does usually only upvote, this was only a hint how you can prevent from downvotes ;)
C++
_hell:
mov rax,1
mov rdi,1
mov rsi,hell
mov rdx,6
syscall
ret

You jump into this code from _start. After the syscall you then do a ret, but you have nowhere to return to since this code was not called as a subroutine. You need to use a call mechanism to run these small routines, not jump straight into them.

See NASM Assembly Language Tutorials - asmtutor.com[^] for a sample.
 
Share this answer
 
v2
Comments
[no name] 20-Dec-20 20:36pm    
@Richard MacCutchan thanks I got it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900