title: Segmentation fault ASM on linux at printf
body: <p>The following is a program from a book (Introduction to 64 Bit Intel Assembly Language Programming for Linux, by Seyfarth, 2012), chap 9. The fault (in gdb) is:</p>
  
  <blockquote>
    <p>Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7aa10a5 in __printf_size (fp=0x400400, info=0x0, 
       args=) at printf_size.c:199
    199    printf_size.c: No such file or directory.</p>
  </blockquote>
  
  <p>Until this chapter, I successfully used the following to "produce an object file", as recommended,</p>
  
  <pre><code>yasm -f elf64 -g dwarf2 -l exit.lst exit.asm
  </code></pre>
  
  <p>and then,</p>
  
  <pre><code>ld -o prgm prgm.o
  </code></pre>
  
  <p>This is the program as copied from the book(l 10 push rbp; I had firstly rem'd the ; but had the same result):</p>
  
  <pre><code>    segment .text
      global  main
      extern  printf
  
  ;   void print_max  ( long a, long b )
  ;   {
  a   equ 0
  b   equ 8
  print_max:
      push    rbp;     ;normal stack frame
      mov     rbp, rsp
  ;   leave space for a, b and max
      sub     rsp, 32
  ;   int max;
      max equ 16
      mov [rsp+a], rdi ; save a
      mov [rsp+b], rsi ; save b
  ;   max = a;
      mov [rsp+max], rdi
  ;   if ( b &gt; max ) max = b;
      cmp rsi, rdi
      jng skip
      mov [rsp+max], rsi
  skip:
      ;   printf ( "max(%1d,%1d ) = %1d\n",
      ;                a, b, max );
      segment .data
  fmt db  'max(%1d,%1d) = %1d',0xa,0
      segment .text
      lea rdi, [fmt]
      mov rsi, [rsp+a]
      mov rdx, [rsp+b]
      mov rcx, [rsp+max]
      call printf
   ; }
      leave
      ret
  
  main:
      push    rbp
      mov     rbp, rsp
  ;   print_max ( 100, 200 );
      mov     rdi, 100    ;first parameter
      mov     rsi, 200    ;second parameter
      call    print_max
      xor     eax, eax    ;to return 0
      leave
      ret
  </code></pre>
  
  <p>After a similar segmentation fault with a previous program in this chap ("Hello World" example), I used </p>
  
  <pre><code>gcc -o prgm prgm.o
  </code></pre>
  
  <p>which had worked until this program.</p>