Function 函数调用
运行此程序时不会打印任何内容。当Function 函数调用,function,loops,mips,Function,Loops,Mips,运行此程序时不会打印任何内容。当AverageFunction调用sumfunction时,我需要从另一个函数调用函数并返回到main-----------------当AverageFunction调用sumfunction时,它会用新的返回地址(即jal sumfunction后面的指令地址)覆盖$ra。因此,当AverageFunction尝试返回时,它将以无限循环结束 您需要以某种方式保存旧的返回地址,然后将其还原。一种方法是使用堆栈: msg4: .asciiz "\nAverage
AverageFunction
调用sumfunction
时,我需要从另一个函数调用函数并返回到main-----------------当AverageFunction
调用sumfunction时,它会用新的返回地址(即jal sumfunction
后面的指令地址)覆盖$ra
。因此,当AverageFunction
尝试返回时,它将以无限循环结束
您需要以某种方式保存旧的返回地址,然后将其还原。一种方法是使用堆栈:
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
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