title: Use scanf with MIPS
body: <p>This questions is tangentially related to this question I posted: <a href="https://stackoverflow.com/questions/40054962/why-isnt-syscall-working-for-mips?noredirect=1#comment67398097_40054962">Why isn&#39;t syscall working for MIPS</a>.</p>
  
  <p>I am attempting to use scanf in my MIPS assembly to read users input from the command line.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    .option pic0
  
      .rdata # read-only data
  
      .align  2
  
  fromatInt:
      .asciz "%d"
      .asciz "%c"
  
      .align 2
  
  resultFormat:
      .asciz  "(%d/%u)\n"
  
      .text
      .align  2
  
  scanFormat:
      .word 1
  
      .text
      .align 2
  
  
          .global print
  
  print:
  
      addiu $sp, $sp, -4  
      sw    $ra, ($sp)    
  
      move $a1, $a0
      la   $a0, resultFormat
      jal  printf
      move $a0, $a1
  
      lw    $ra, ($sp)    
      addiu $sp, $sp, +4  
  
      jr    $ra
  
      .global main
  
  main:
  
      la    $a0, fromatInt
      la    $a1, scanFormat
      jal   scanf
      lw    $s0, scanFormat
  
      move  $a0, $s0
      jal   print
  
      jal exit
  </code></pre>
  
  <p>When I run the a.out file I get a segfault. And, when I run gdb this is the result I get when stepping through:</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x77e821b0 in _IO_vfscanf_internal (s=0x77fa5bc0 &lt;_IO_2_1_stdin_&gt;, 
      format=&lt;optimized out&gt;, argptr=0x7fff6614, errp=0x0) at vfscanf.c:1826
  1826    vfscanf.c: No such file or directory.
  </code></pre>
  
  <p>We are using a real MIPS processor, otherwise I would use syscall to print and scan. I also don't understand the linux kernel syscall enough to use it effectively. Any help is much appreciated.</p>