Real Branches

Let's do a couple of examples of real branches in this bit of code:

.text

          bne   $20  $21  forth
          add   $10  $11  $12  #<- PC is here when executing the bne
          add   $10  $11  $12
back:     add   $10  $11  $12  # here is -9 instructions from the beq PC
          add   $10  $11  $12
          add   $10  $11  $12
          add   $10  $11  $12
          add   $10  $11  $12
          add   $10  $11  $12
forth:    add   $10  $11  $12  # here is 8 instructions from the bne PC
          add   $10  $11  $12
          beq   $5   $6   back
          add   $10  $11  $12  #<- PC is here when executing the beq
      

Copy this code and paste it into MARS to confirm that we have right answers here

The assembler gets the same result, but of course, it can't count instructions the way we do. Instead, it will subtract the PC value from the address of the branch target. This will give it the byte offset, so it will divide that by 4 to get the word offset. Let's do one of them that way by hand:

SEE HOW EASY THIS IS?

There is a caveat that goes with this: