title: MIPS : parsing and modifying a string
body: <p>I have to write a program in MIPS where I have to open a file whose name is entered by the user and parse it,using the MARS simulator .
  However, since the syscall 8 (which reads a string from user input) follows the semantics of the C function fgets, I have to remove the line feed \n character from the string before I can try and open the file.</p>
  
  <p>I am able to parse the string (here I print the ASCII code of every character for debugging purpose ) . 
  However when I try to change the line feed character into a NULL character with the line </p>
  
  <pre><code>sb $zero 0($t1)
  </code></pre>
  
  <p>Mars encounters an exception during execution :</p>
  
  <blockquote>
    <p>"Runtime exception at 0x00400050: address out of range 0x00000000"</p>
  </blockquote>
  
  <p>If I comment out this line , the program runs fine and prints every  ascii code of the string.</p>
  
  <pre><code>.data
  
  prompt : .asciiz "enter filename:\n"
  lf : .asciiz "\n"
  space : .asciiz " "
  
  .text 
  
  la $a0 prompt      # asking user for filename
  jal print_string
  jal read_string
  
  la $t0 ($a0)   #copying the string address 
  lb $t1 0($t0)  #loading the first  byte
  
  parsing_string : #loop
  beq $t1 0 remove_line_feed  # reaching the NULL character '\0'
  
  move $a0  $t1   #printing the ascii code
  li $v0 1 
  syscall
  
  la $a0 space  #printing a space
  li $v0 4
  syscall  
  
  add $t0 $t0 1   # loading  
  lbu $t1 ($t0)   # the next byte
  
  j parsing_string
  
  remove_line_feed :
  sb $zero 0($t1)   # runtime exception if i uncomment  this line 
  j end
  
  end : 
  
  
  li $v0 10     # syscall 10 : end program 
  syscall
  
  
  
  read_string :   #read a string in $a0
  
  li $a1 100  # $a1 = maximum number of characters to read        
  li $v0 8    # syscall 8  : fgets()
  syscall 
  jr $ra
  
  
  print_string  :  #print string in $a0
  li $v0 4      # syscall 4 : print string
  syscall
  jr $ra
  </code></pre>
  
  <p>I don't understand what I'm doing wrong here, at this point of the program  the register  $t1 should  contain the address where the line feed character is written .
  However  I may have misunderstood how the instructions save byte and load byte actually work.  </p>
  
  <p>Any help or suggestions are greatly appreciated :)</p>