title: Mips Procedure: Infinite for loop and Exception occurred at PC
body: <p>Seeing the topics on the site, however I was not able to solve my problem. I noticed that the code inside the procedure falls into an infinite for and in QTSpim I saw that the registers involved are <code>$s0</code>, <code>$s1</code>, <code>$t2</code>, and <code>$t4</code>. </p>
  
  <p>After a few cycles then the message <code>Exception occurred at PC</code> then <code>expection 7 Bad data address</code>.
  Here is the code, I can not figure out where mistake, I tried to write it without procedure and it works perfectly.</p>
  
  <pre><code>.data
  string: .asciiz "Hello Simon"
  string2: .asciiz ""
      .text
      .globl main
  
  Delete_space:
  addi $sp, $sp, -16
  sw $s0, 0($sp)
  sw $s1, 4($sp)
  
  add $t8, $a0, $zero
  lenght_string:
  lb $t0, 0($t8)
  beqz $t0, for #t8 contain the lenght of string
  add $t8, 1
  j lenght_string
  for:
  add $s0, $zero, $zero #i=0
  add $s1, $zero, $zero #j=0
  
  condition:
  slt $t1, $s0, $t8 #i&lt; lenght_string
  beq $t1, $zero, endfor
  
  
  consequence:
  add $t2, $t8, $s0
  lb $t3, 0($t2)
  bne $t3, ' ', op1
  beq $t3, ' ', else
  addi $s0, $s0, 1
  addi $s1, $s1, 1
  j condition
  op1:
  add $t4, $a1, $s1
  sb $t3, 0($t4)
  addi $s0, $s0, 1
  addi $s1, $s1, 1
  j condition
  else:
  sub $s1, $s1, 1
  addi $s0, $s0, 1
  addi $s1, $s1, 1
  j condition
  endfor:
  
  #end 
  lw $s1, 4($sp)
  lw $s0, 0($sp)
  addi $sp, $sp, 16
  jr $ra
  
  main:
  la $a0, string
  la $a1, string2
  jal Delete_space
  
  la $a0, string2
  li $v0, 4
  syscall
  
  li $v0, 10
  syscall
  </code></pre>