如何从MIPS中的主函数返回值? [英] How do I return a value from the main function in MIPS?
问题描述
说我想在MIPS中编写以下C程序:
Say I want to write the following C program in MIPS:
int main () {
return 5;
}
当我在 MARS 中尝试以下MIPS代码时:
When I try the following MIPS code in MARS:
main: ADDI $v0, $zero, 5 # $v0 = 5
JR $ra # return from main()
我收到无效的程序计数器"错误.这显然是因为您无法跳出MARS的主要功能.所以我尝试像这样重写它:
I get a 'invalid program counter' error. This is apparently because you cannot jump out of the main function in MARS. So I tried rewriting it like so:
main: ADDI $v0, $zero, 5 # $v0 = 5
li $v0, 10 # load 10(exit) for syscall
syscall # exit
执行此操作后,$ v0寄存器包含值10,而不是5.这是可以理解的,因为我必须重写$ v0寄存器才能使syscall正常工作.那么,我的问题是,应在哪里保存值5以便将其正确返回给此应用程序的调用者?
After executing this, the $v0 register contains the value 10, not 5. Which is understandable since I had to overwrite the $v0 register in order for syscall to work. My question, then, is where would I save the value 5 in order for it to be returned to the caller of this application correctly?
推荐答案
使用系统调用17 :
exit2 (terminate with value)
----------------------------
$v0 = 17
$a0 = termination result
请注意如果MIPS程序在MARS图形界面(GUI)的控制下运行,则$ a0中的退出代码将被忽略."
这篇关于如何从MIPS中的主函数返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!