title: Why is this assembly program segfaulting when I call printf?
body: <p>The following code is giving me a segmentation fault because of calling printf:</p>
  
  <pre><code>            .text
      .global main
  main:    
      push %r13
      push %r14
      push %r15
      jmp rest
  f:
      pop %rbx
      movq $0, %rax
      push %rbx
      movq $1, %r9
      pushq %r9
      mov $format,%rdi
      popq %rsi
      movq %rsi, %r15
      pop %rbx
      call printf
      push %rbx
      movq $1, %r9
      pushq %r9
      mov $format,%rdi
      popq %rsi
      movq %rsi, %r15
      pop %rbx
      call printf
      push %rbx
      ret
  func:
      pop %rbx
      movq $0, %rax
      push %rbx
      call f
      push %rax
      popq %rsi
      movq %rsi, %r15
      .data
      xfunc:    .quad   0
      .text
      mov %r15,xfunc
      ret
  rest:
      call func
      push %rax
      mov $format,%rdi
      popq %rsi
      movq %rsi, %r15
      call printf
      mov $0,%rax
      pop %r15
      pop %r14
      pop %r13
      ret
      .data
  format:    .string "%lu\n"
  </code></pre>
  
  <p>If i take out the second call printf for printing variable if in label f, there is no segmentation fault. Also if I call the label f directly from rest there is no segmentation fault.</p>