title: Using Printf in x86_64 (NASM)
body: <p>im a newbie in Assembly language. I came across an <a href="http://godlytalias.blogspot.co.ke/2012/04/nasm-program-to-find-discriminant-and.html" rel="nofollow noreferrer">Assembly code</a> that computes a quadratic equation and is run on Ubuntu 64 bit.I'm trying to use printf to print out the integers but it's not working. The code is ;</p>
  
  <pre><code>global _main
  section .text
  
  
  
  extern printf,scanf
  
  print:
  mov eax,4
  mov ebx,1
  int 0x80
  ret
  
  main:
  
  mov ecx,inputa
  mov edx,linputa
  call print
  
  push a
  push scan
  call scanf
  
  mov ecx,inputb
  mov edx,linputb
  call print
  
  push b
  push scan
  call scanf
  
  mov ecx,inputc
  mov edx,linputc
  call print
  
  push c
  push scan
  call scanf
  
  fld qword[b]
  fmul st0
  fld qword[a]
  fmul qword[c]
  mov word[const],4
  fimul word[const]
  fchs
  fadd st1
  fst qword[disc]
  
  push dword[disc+4]
  push dword[disc]
  push dis
  call printf
  
  ftst
  fstsw ax
  sahf
  ja real_roots
  sahf
  je one_root
  
  imag_roots:
  fchs
  fsqrt
  fld qword[b]
  fchs
  fadd st1
  fld qword[a]
  mov word[const],2
  fimul word[const]
  fxch st1
  fdiv st1
  fstp qword[x1]
  fld qword[disc]
  fchs
  fsqrt
  fld qword[b]
  fadd st1
  fchs
  fld qword[a]
  mov word[const],2
  fimul word[const]
  fxch st1
  fdiv st1
  fstp qword[x2]
  
  push dword[x2+4]
  push dword[x2]
  push dword[x1+4]
  push dword[x1]
  push imagroot
  call printf
  jmp over
  
  real_roots:
  fsqrt
  fld qword[b]
  fchs
  fadd st1
  fld qword[a]
  mov word[const],2
  fimul word[const]
  fxch st1
  fdiv st1
  fstp qword[x1]
  fld qword[disc]
  fsqrt
  fld qword[b]
  fadd st1
  fchs
  fld qword[a]
  mov word[const],2
  fimul word[const]
  fxch st1
  fdiv st1
  fstp qword[x2]
  
  push dword[x2+4]
  push dword[x2]
  push dword[x1+4]
  push dword[x1]
  push realroot
  call printf
  
  jmp over
  
  one_root:
  fsqrt
  fld qword[b]
  fchs
  fadd st1
  fld qword[a]
  mov word[const],2
  fimul word[const]
  fxch st1
  fdiv st1
  fstp qword[x1]
  
  
  push dword[x1+4]
  push dword[x1]
  push oneroot
  call printf
  
  
  over:
  mov eax,1
  mov ebx,0
  int 0x80
  
  section .bss
  x1 resq 1
  x2 resq 1
  const resw 1
  a resq 1
  b resq 1
  c resq 1
  disc resq 1
  
  section .data
  scan dd "%lf",0
  oneroot dd "Root = %f",10,0
  realroot dd "Root 1 = %f    &amp;   Root 2 = %f ",10,0
  imagroot dd "Root 1 = %fi   &amp;   Root 2 = %fi ",10,0
  
  
  inputa dd "INPUT THE CO-EFFICIENT a ( a(x^2) + bx + c ) ",10
  linputa equ $-inputa
  inputb dd "INPUT THE CO-EFFICIENT b ( a(x^2) + bx + c ) ",10
  linputb equ $-inputb
  inputc dd "INPUT THE CO-EFFICIENT c ( a(x^2) + bx + c ) ",10
  linputc equ $-inputc
  dis dd "DISCRIMINANT  =  %f ",10,0
  </code></pre>
  
  <p>I assembled it using <code>nasm -f elf qua.asm</code> and tried linking it with <code>ld -m elf_i386 -s -o qua qua.o</code></p>
  
  <p>It gives me the following error </p>
  
  <blockquote>
    <p>ld: warning: cannot find entry symbol _start; defaulting to
    0000000008048080 qua.o: In function <code>main': qua.asm:(.text+0x27):
    undefined reference to</code>scanf' qua.asm:(.text+0x45): undefined
    reference to <code>scanf' qua.asm:(.text+0x63): undefined reference to
    </code>scanf' qua.asm:(.text+0xa6): undefined reference to <code>printf' qua.o:
    In function</code>imag_roots': qua.asm:(.text+0x13b): undefined reference
    to <code>printf' qua.o: In function</code>real_roots': qua.asm:(.text+0x1be):
    undefined reference to <code>printf' qua.o: In function</code>one_root':
    qua.asm:(.text+0x201): undefined reference to `printf'</p>
  </blockquote>
  
  <p>i tried linking it with gcc <code>gcc -m32 -o qua qua.o</code></p>
  
  <p>and i got an error </p>
  
  <blockquote>
    <p>/usr/bin/ld: cannot find crt1.o: No such file or directory
    /usr/bin/ld: cannot find crti.o: No such file or directory
    /usr/bin/ld: skipping incompatible
    /usr/lib/gcc/x86_64-linux-gnu/5/libgcc.a when searching for -lgcc
    /usr/bin/ld: cannot find -lgcc /usr/bin/ld: skipping incompatible
    /usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so when searching for -lgcc_s
    /usr/bin/ld: cannot find -lgcc_s /usr/bin/ld: cannot find -lc
    /usr/bin/ld: skipping incompatible
    /usr/lib/gcc/x86_64-linux-gnu/5/libgcc.a when searching for -lgcc
    /usr/bin/ld: cannot find -lgcc /usr/bin/ld: skipping incompatible
    /usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so when searching for -lgcc_s
    /usr/bin/ld: cannot find -lgcc_s /usr/bin/ld: cannot find crtn.o: No
    such file or directory collect2: error: ld returned 1 exit status</p>
  </blockquote>
  
  <p>How do you go about this? </p>