2

Say I want to write the following C program in MIPS:

int main () {
  return 5;
}

When I try the following MIPS code in MARS:

main:   ADDI $v0, $zero, 5     # $v0 = 5
        JR $ra                 # return from main()

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

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?

4

1 回答 1

4

使用系统调用 17

exit2 (terminate with value)
----------------------------
$v0 = 17
$a0 = termination result

请注意,“如果 MIPS 程序在 MARS 图形界面 (GUI) 的控制下运行,则 $a0 中的退出代码将被忽略。”

于 2013-09-10T20:24:39.430 回答