Segmentation fault in Assembly and string

badnack

I am trying to make a simple program in assembler, but I do not understand why, I get a fault. I' ve a 64 bit machine running Ubuntu 12.04, and "as" as a assembly compiler. My goal merely is to print the string "Hello" on screen.

I wrote this:

#print.s
.section .data
.globl StringToPrint

 StringToPrint: .asciz "Hello"

 .globl _start

  _start:       
    movq $4, %rax
    movq $1, %rbx
    movq $StringToPrint, %rcx
    movq $5, %rdx
    int $0x80
 _done:    
    ret

But that's what I get:

$ as print.s -o print.o
$ ld print.o -o print
$ ./print
Hello[1]    10679 segmentation fault (core dumped)  ./print

Why do you think this happens? Any idea?

boleto

Here is the fix :

#print.s
.section .data

.globl StringToPrint
    StringToPrint: .asciz "Hello"   

.globl _start

  _start:

        movl    $5, %edx             # string length
        movl    $StringToPrint, %ecx # pointer to string to write
        movl    $1, %ebx             # file handle (stdout)
        movl    $4, %eax             # system call number (sys_write)
        int     $0x80                # Passes control to interrupt vector

        #sys_exit (return_code)  
        movl    $1, %eax             #System call number 1: exit()
        movl    $0, %ebx             #Exits with exit status 0
        int     $0x80                #Passes control to interrupt vector 

As Michael has already said you need to call sys_exit to avoid segmentation fault .

Edit :
Here is good to mention that int 0x80 invokes 32-bit system calls .
Using int 0x80 for syscall on x64 systems is used for backward compatibility to allow 32-bit applications to run .

On 64-bit systems will be correct to use syscall instruction .
Here is a working version :

.section .data
StringToPrint: .asciz "Hello"

.section .text
.globl _start

_start:

        movq    $1, %rax                # sys_write
        movq    $1, %rdi                # stdout
        movq    $StringToPrint, %rsi    # pointer to string to write
        movq    $5, %rdx                # string length
        syscall

        movq    $60, %rax               # sys_exit
        movq    $0, %rdi                # exit code
        syscall

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Segmentation fault on reverse string function

분류에서Dev

Assembly of Powerpc encountered Program received signal SIGSEGV Segmentation fault

분류에서Dev

string segmentation fault when reading lines from text file

분류에서Dev

A* Implementation in C, Segmentation fault

분류에서Dev

Segmentation Fault - GNU C

분류에서Dev

Access Violation (Segmentation Fault)

분류에서Dev

Malloc to struct; segmentation fault

분류에서Dev

Segmentation Fault in hsearch

분류에서Dev

Strcpy Segmentation Fault C

분류에서Dev

Segmentation Fault? Why?

분류에서Dev

Strange segmentation fault in code

분류에서Dev

Segmentation Fault on return statement

분류에서Dev

glGenBuffers crashing with Segmentation fault

분류에서Dev

Struct causing segmentation fault

분류에서Dev

Resetting Variable : Segmentation fault

분류에서Dev

Segmentation fault in sorting algorithm

분류에서Dev

Segmentation Fault While Sorting - Malloc

분류에서Dev

Fractional Knapsack Algorithm segmentation fault

분류에서Dev

Segmentation fault on reboot Ubuntu 12.04

분류에서Dev

C Segmentation fault using strtok

분류에서Dev

python Segmentation fault (core dumped)

분류에서Dev

Segmentation fault in sigaction signal handler

분류에서Dev

Segmentation fault with flex bison and yyparse

분류에서Dev

Why is the segmentation Fault error occuring

분류에서Dev

Depth first Minimax Segmentation Fault

분류에서Dev

while ... readdir causing segmentation fault

분류에서Dev

Graph adjacency matrix segmentation fault

분류에서Dev

Running bash does "segmentation fault core dumped"

분류에서Dev

Segmentation fault in recursive Binary Search Algorithm in C