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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…