title: Assembly return address $ra doesn't get back where it should
body: <p>So i'm new to the  assembly language and mips . Im trying to use a procedure (or function). I checked with debug the values are ok but the problem comes when the program should get out of the function and get back in the &quot;main&quot; .</p>
  <p>This is the body of the main (plus the stored data)  :</p>
  <pre><code>.data
  N:.word 3
  vet: .space 12
  max: .word 0
  msg:.asciiz &quot;Inserisci elemento vettore ---&gt; &quot;
  msg2: .asciiz &quot;Il valore massimo è : &quot;
  .text
  
  la $s0,vet
  la $s1,N
  lw $s1,0($s1)
  la $s2,max
  
  add $t0,$zero,$zero
  FOR: 
      slt $t1,$t0,$s1
      beq $t1,$zero,ENDFOR
      
      sll $t2,$t0,2
      add $t2,$t2,$s0
      
       li $v0,4
       la $a0,msg
       syscall
       
       li $v0,5
       syscall
      
       sw $v0,0($t2) 
       addi $t0,$t0,1
       j FOR
      
  ENDFOR :
      
       add $a0,$s0,$zero      #VALORE INDIRIZZO BASE SALVATO IN A0
       add $a1,$s1,$zero      #VALORE N SALVATO A1
      
       jal trovaMax
       
  
       sw $v0,0($s2)          
       
       li $v0,4
       la $a0,msg2
       syscall
       
       li $v0,1
       syscall
       
       li $v0,10
       syscall     
  </code></pre>
  <p>So after i call the function trovamax with jal it goes in the following function</p>
  <pre><code>trovaMax: 
      #PER PRIMA COSA ALLOCHIAMO LO SPAZIO PER LO STACK POINTER
      
       addi $sp,$sp,-8
       sw $s0,4($sp)
       sw $s1,0($sp)
       
       add $s0,$zero,$zero        #I=0
       add $s1,$zero,$zero        #max=0
       
       FOR2: 
       slt $t1,$s0,$a1
       beq $t1,$zero,ENDFOR
       
       sll $t2,$s0,2
       add $t2,$t2,$a0
       lw  $t3,0($t2)     
       slt $t1,$s1,$t3
       beq $t1,$zero,ENDIF
       add $s1,$t3,$zero
       ENDIF:
       addi $s0,$s0,1
       j FOR2
       ENDFOR2:
       #DOPO AVER SVILUPPATO IL CORPO DELLA FUNZIONE DOBBIAMO SALVARE IL VAL. RITORNO IN V0 COSI DA POTER RESTITUIRE QUESTO ALLA FUNZIONE CHIAMANTE
       
       add $v0,$s1,$zero
       #ripristino lo stato dello stack pointer 
       
       lw $s1,0($sp)
       lw $s0,4($sp)
       addi $sp,$sp,8
       jr $ra
  </code></pre>
  <p>So the problem comes after the procedure does what it is supposed to , after using <code>jr $ra</code> it is supposed to go back into the main (since ra saves the address of the calling function ?)
  here :</p>
  <pre><code> sw $v0,0($s2)
  </code></pre>
  <p>Instead checking debug i can see it starts reading again</p>
  <pre><code>    add $a0,$s0,$zero
      add $a1,$s1,$zero
      jal trovaMax
  </code></pre>
  <p>And of course it calls again the function and all.
  So I wonder why  it doesn't just go back returning the value.</p>