0

Introduction:

Hello everyone, I want to let you know that this is my first question on this website.

I am pretty new to programming in NASM-Assembly and programming in Assembly in general, so I hope this is a simple problem that can be answered easily:

Problem:

I was trying to make a simple program where the result is just an already defined number.

global _start
 
section .text
 
_start: 
        mov rax, 60
        mov rdi, 0
        ret

(That's my code. Just moved a random number to RAX and for no reason decided to also move a number into RDI)

But then, when trying to run my program, I get this:

[... Syscalls]$ nasm -f elf64 -o test.o test.asm

[... Syscalls]$ ld test.o -o test

[... Syscalls]$ ./test

Speicherzugriffsfehler (Speicherabzug geschrieben)

(Translation: "Memory access error". I looked up some translations and seemingly you could also translate it to "segmentation fault". Also: Don't get confused by the "Syscalls" in brackets, it's just the name of my folder.)

()

Details:

So why does this problem occur? I have two other different kinds of programs where this doesn't happen: First are the programs, where I work with the wrappers that my university provides, but those are only for specific functions. Second are the programs where I work with system calls (syscall). That's why I am confused why this error appears when trying to create and run the program like I did in the pictures.

Things I already tried:

When I looked up what to do, I got the answer that I should debug my program and find exactly where my problem is. Honestly, I tried to get a debugger-software and debug my program, but I had several problems that I won't get into now. I gave up that solution pretty quickly because I didn't want to get into trying to fix new problems while still having the old ones. Also, I don't think that a Debugger would help anyway because it's none of the singe lines in the code that produce this outcome. It's just whenever I try to run a program without a wrapper or system calls.

The Question:

So is it just impossible to run an assembly-program like that? If yes, why? If no, what can I do to run this program and don't have memory access errors anymore?

Thank you for every answer in advance.

  • 2
    You can't `ret` from `_start`. You need an exit syscall. And yes, a debugger would have told you it's a single line, the `ret` that is crashing :) – Jester Jan 23 '23 at 13:51
  • 1
    Using a debugger is the single most important thing you need for learning assembly. Many different bugs will lead to segmentation fault (SIGSEGV), so a debugger to see what's going on will save you huge amounts of time guessing, and single stepping while you watch the code execute and registers change will help you see what's going on in any code you want to look at. See the bottom of https://stackoverflow.com/tags/x86/info for GDB tips for Linux. – Peter Cordes Jan 23 '23 at 15:50

0 Answers0