Branches and Jumps

Branches use PC-relative mode to find their target address. Jumps use direct mode.

We need to see how these address modes differ.

Here is some code with branches and jumps sprinkled around in it.

The column of comments contains plausible addresses for each of the instructions.

Notice that they all have the same target address: 0x00400048

        add  $3  $4  $5     # 0x00400000
        beq  $1  $2  place  # 0x00400004
        add  $3  $4  $5     # 0x00400008
        add  $3  $4  $5     # 0x0040000C
        j    place          # 0x00400010
        add  $3  $4  $5     # 0x00400014
        add  $3  $4  $5     # 0x00400018
        add  $3  $4  $5     # 0x0040001C
        beq  $1  $2  place  # 0x00400020
        add  $3  $4  $5     # 0x00400024
        add  $3  $4  $5     # 0x00400028
        add  $3  $4  $5     # 0x0040002C
        j    place          # 0x00400030
        add  $3  $4  $5     # 0x00400034
        add  $3  $4  $5     # 0x00400038
        add  $3  $4  $5     # 0x0040003C
        add  $3  $4  $5     # 0x00400040
        add  $3  $4  $5     # 0x00400044
place:  add  $3  $4  $5     # 0x00400048
      

We will assemble the branches into hexadecimal.

We will simulate this by hand.

So the hexadecimal is 0x10220010. If that's a problem for you, consult the "Real Branches" web page.

So the hexadecimal is 0x10220009. If that's still a problem for you, consult the "Real Branches" web page again.

Now let's do it as the assembler does it

Now let's assemble the jumps into hexadecimal.

So the hexadecimal is 0x08100012. If that's a problem for you, consult the "Jumps Use Direct Mode" web page.

Here are some things that should jump out at you:

Notice that the example code for this page begins at address 0x00400000, which is the same place that MARS begins the text segment.

That means that you can copy this code and paste it into MARS to verify all the calculations that we did here.