Assembly segmentation fault after making a system

2019-01-04 14:54发布

I was experimenting and have the following assembly code, which works very well, except that I get a "Segmentation fault (core dumped)" message right before my program ends:

GLOBAL _start

%define ___STDIN 0
%define ___STDOUT 1
%define ___SYSCALL_WRITE 0x04

segment .data
segment .rodata
    L1 db "hello World", 10, 0
segment .bss
segment .text
_start:
    mov eax, ___SYSCALL_WRITE
    mov ebx, ___STDOUT
    mov ecx, L1
    mov edx, 13
    int 0x80

It doesn't matter whether or not I have ret at the end; I still get the message.

What's the problem?

I'm using x86 and nasm.

1条回答
唯我独甜
2楼-- · 2019-01-04 15:45

As n.m. said in the comments, the issue is that you aren't exiting the program, so execution runs off into garbage code and you get a segfault.

What you need is:

%define ___SYSCALL_EXIT 1

// ... at the end of _start:
    mov eax, ___SYSCALL_EXIT
    mov ebx, 0
    int 0x80
查看更多
登录 后发表回答