2014-03-27 32 views
0
msg4: .asciiz "\nAverage is: " 
     main 

jal AverageFunction 
la $a0, msg4 
li $v0, 4 
syscall 
move $a0, $t 
li $v0, 1 
syscall 

    sumfunction: 

la $t3, array 
sum: 
bge $t4, $t1, done 
lw $t0, 0($t3) 
add $t5, $t5, $t0 
addi $t3, $t3, 4 
addi $t4, $t4, 1 
b sum 
done: 
jr $ra 

AverageFunction: 
    jal sumfunction 
    div $t6, $t5, $t1 
    jr $ra 

当我运行这个程序时什么都不打印。我需要从另一个函数调用函数,返回主-------------Mips函数调用

回答

1

AverageFunction通话sumfunction它覆盖$ra用新的返回地址(即指令的地址以下jal sumfunction )。因此,当AverageFunction尝试返回时,它会以无限循环结束。

你需要以某种方式保存旧的返回地址,然后恢复它。一种方法是使用堆栈:

AverageFunction: 
    addi $sp,$sp,-4 # "push" operations pre-decrement the stack pointer 
    sw $ra,($sp)  # Save the current return address on the stack 

    jal sumfunction 
    div $t6, $t5, $t1 

    lw $ra, ($sp)  # Restore the old return address 
    addi $sp,$sp,4  # "pop" operations post-increment the stack pointer 
    jr $ra 
+0

谢谢你,迈克尔 – user3237662