0

I wrote this code (this is only a part of it):

beq $t4 ,$0 ,__less3
    add $s2,$t3,$0 # s2=t3
    add $s3,$t2,$0 # s3=t2
    j __next1

 __less3:
    add $s2,$t2,$0 # s2=t2
    add $s3,$t3,$0 # s3=t3
 __next1:
    slt $t4, $t1, $t0 # t4=(t1<t0)
    beq $t4 ,$0 ,__les1sk

The problem is that when the simulation gets to line 4 and need to do the jump, it does line 7 (the next command) and only then it jumps to line 10. I know it because that register $s2 changes to $t2 instead of remaining with the value from $t3.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

MIPS, the processor that PCSpim simulates, employs "delayed branching": it executes the instruction immediately following the branch before branching to the jump target (hence "delays" the jump). This is an efficiency measure; since the processor has gone through most of the work for that "extra" instruction, that effort won't be wasted. Compilers account for this when producing code.

I believe that there is an option in the simulator to disable delayed branching.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101