No. Some registers contain important information.
To
answer the question, you could look at the
code for main
to determine which registers
contain information and so
can not be altered.
Do this
every place in main
that sub
is called,
because different registers are likely to be in use
at different places.
Now write sub
using only those registers
that are not in use before any call.
This is tedious and error prone.
Worse, if you make a change to main
,
you now might have to
re-code
sub
using different registers.
One of the goals in writing a subroutine is to create a module that is independent of the rest of the code. We have not achieved that, yet.
Another issue is how data is passed into and out of the subroutine. Often data is in registers, and the results are in registers. Which registers?
By software convention (not by hardware) registers have been assigned different roles:
$t0 - $t9
— The subroutine is free to change these registers.$s0 - $s7
— The subroutine must not change these registers.$a0 - $a3
— These registers contain arguments for the subroutine.
The subroutine can change them.$v0 - $v1
— These registers contain values returned from the subroutine.Is the following code fragment correct?
add $t0,$s5,$s3 # calculate an important sum in $t0 jal somesub # call a subroutine nop # branch delay mul $s4,$t0,$v1 # multiply the sum