As @jaquadro mentions, you can specify a command line entry point for the linker (or use a script link): gcc -Wall -Wextra -nostdlib -Wl,-eMyEntry minimal.S && ./a.out
The reason your segfaults program is because, since you are not using the standard library, there is nowhere to go back to ( retq ). Instead, call exit using the correct syscall (in this case it is 60, which is placed in rax , the first (and only) parameter is placed in rdi .
Example:
.text
.globl MyEntry
MyEntry:
# Use Syscall 60 (exit) to exit with error code 42
movq $ 60,% rax
movq $ 42,% rdi
syscall
Related question on how to make system calls on x86_64
source share