Sei sulla pagina 1di 1

Some Additional Notes

Considering some questions that we got in previous years about how to terminate your MIPS programs
on simulators without errors, I'll try to explain what you need to do. Keep in mind that your main function
will be called by the simulator as you call any other function in your code (think of it as THE ACTUAL
MAIN function is provided by the simulator, while your code is just some other functions that is called). So
there are some rules you need to follow, namely, you need to consider what you need to do to make sure
the return address is retrieved properly when your code terminates.

You have two options for termination. The first one: before you type any code, you should first save the
return address $ra on stack, and as the last step restore it and execute jr $ra. An example:

.text
.globl main
main:
addi $sp, $sp, -4 # make room on stack to save $ra
sw $ra, 0($sp) # Save $ra on the stack
# Your code....
done:
lw $ra, 0($sp) # restore #ra
addi $sp, $sp, 4 # deallocate stack space
jr $ra # return

The second option: you can execute the 'exit' syscall as shown below:

.text
.globl main
main:
# Your code....
done:
li $v0, 10
syscall

Potrebbero piacerti anche