Why I get the error:
Segmentation fault (core dumped)
Here is the assembly code:
.intel_syntax noprefix
    
.data
    message: .asciz "Hello World!\n"
.text
.global main
main:
    lea rdi, message
    call printf
    ret
Why I get the error:
Segmentation fault (core dumped)
Here is the assembly code:
.intel_syntax noprefix
    
.data
    message: .asciz "Hello World!\n"
.text
.global main
main:
    lea rdi, message
    call printf
    ret
 
    
     
    
    System V ABI requires you to align your stack at 16-byte before you call a function. In order to make it easy, the ABI guarantees that "On function entry, if you sub your stack pointer with 8 * n (n is an odd number), your stack will be 16-byte aligned".
If you don't follow this calling convention, other libraries may crash because they can't align their stack frame properly if they need to use instructions that need special alignment, like movdqa for example.
ammarfaizi2@integral:/tmp/test_asm$ cat test.S
.intel_syntax noprefix
    
.data
    message: .asciz "Hello World!\n"
.text
.global main
main:
    sub rsp, 8
    xor eax, eax
    lea rdi, [rip + message]
    call printf
    add rsp, 8
    ret
ammarfaizi2@integral:/tmp/test_asm$ gcc test.S -o test
ammarfaizi2@integral:/tmp/test_asm$ ./test
Hello World!
ammarfaizi2@integral:/tmp/test_asm$ 
If you call a function and the next thing you do is ret, you can simplify the code with tail call. It uses jmp to the target function to be called. Make sure you undo the current function stack frame before jump if you setup it before.
To support PIE and PIC, consider to use RIP relative addressing to access static storage. It also improves security. Compilers these days usually compile the target to PIE by default.
This part is the example of accessing static storage with RIP relative addressing:
lea rdi, [rip + message]
ammarfaizi2@integral:/tmp/test_asm$ cat test.S
.intel_syntax noprefix
    
.data
    message: .asciz "Hello World!\n"
.text
.global main
main:
    xor eax, eax
    lea rdi, [rip + message]
    jmp printf
ammarfaizi2@integral:/tmp/test_asm$ gcc test.S -o test
ammarfaizi2@integral:/tmp/test_asm$ ./test
Hello World!
ammarfaizi2@integral:/tmp/test_asm$ 
Added xor eax, eax for safety. See: glibc scanf Segmentation faults when called from a function that doesn't align RSP
