I came across the instruction jalr $t1, $t2
instruction, which supposedly sets $t1 to the return address and jumps to $t2. However, there seems to be some ambiguity as to which operation happens first. For instance, the following works differently in MARS and SPIM:
.text
main:
la $t0, func
jalr $t0, $t0
# ...
li $v0, 10
syscall # Exit program
func:
# ...
jr $t0
In MARS, $t0
is first set to pc + 4
(return address) and then jumps to $t0
, so that the code under func
is never run. However, SPIM seems to do the oppsite: jump first and then set the value of $t0
to (the previous) pc + 4
; thus func
is called and runs as usual.
My question is therefore, which simulator implements correct behavior in this case?