Question

MIPS program to loop 10 times and print the loop counter using Procedure call

main:
# initialize values to 2 registers
addi $a1,$zero,10     # $s1= $zero+10 
addi $a2,$zero,0      # $s2= $zero+0
jal AddMeth           # call procedure 
# Print out the loop counter
li $v0,1          # print integer
add $a0,$v1,$zero     # $a0 = $s2+$zero , load return value into argument
syscall  


AddMeth:
Loop:   beq $a1,$a2,Exit  # goto Exit if $s1=$s2
    addi $a2,$a2,1    # $s2 = $s2+1 
    j Loop        # goto Loop

Exit: 
add $v1,$zero,$s2
jr $ra
Was it helpful?

Solution

The (emulated) CPU has no idea that you meant for the program to terminate after the print integer syscall. You need to tell it that that's what you want explictly, otherwise it will just keep on executing the next instruction (so it will end up in an infinite loop where it executes AddMeth and then the print integer syscall over and over).

The exit syscall in SPIM/MARS is number 10, i.e:

li $v0, 10
syscall

You also seem to have mixed up $a2 and $s2 in a couple of places.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top