0

I'm trying to call a function from a .s file in a .c file, compiled with gcc, in the following way:

print.s

.section .data
  str1: .string "Print anything\n"
.section .text
.globl print
print:
  movq $0, %rax
  movq $str1, %rdi
  call printf

main.c

int main(void){
  print();
  
  return 0;
}

and compilling/ running like this:

run.sh

as print.s -o print.o
gcc -g -c main.c -o main.o
gcc -static main.o print.o -o print
./print

For some reason, the program prints the string "Print anything\n", then quickly segfaults. Why is this happening?

  • 5
    You forgot the `ret`? Also you misalign the stack but since you zeroed `rax` and the crash is after the print that is not the current problem. – Jester Apr 11 '22 at 19:36
  • Use `jmp printf` instead if you want to tail-call and have printf return to your caller. – Peter Cordes Apr 12 '22 at 03:27

0 Answers0